|
42 | 42 | import org.scijava.log.LogService; |
43 | 43 | import org.scijava.plugin.Parameter; |
44 | 44 | import org.scijava.prefs.PrefService; |
| 45 | +import org.scijava.util.LastRecentlyUsed; |
45 | 46 |
|
46 | 47 | /** |
47 | 48 | * The default implementation of a {@link ScriptInterpreter}. |
@@ -363,4 +364,113 @@ private static <T> T callMethod(final Object object, final String methodName, |
363 | 364 | return null; |
364 | 365 | } |
365 | 366 |
|
| 367 | + // -- Helper classes -- |
| 368 | + |
| 369 | + /** Container for a script language's interpreter history. */ |
| 370 | + private static class History { |
| 371 | + |
| 372 | + @SuppressWarnings("unused") |
| 373 | + protected static final long serialVersionUID = 2L; |
| 374 | + |
| 375 | + private static final String PREFIX = "History."; |
| 376 | + private final int MAX_ENTRIES = 1000; |
| 377 | + |
| 378 | + private final PrefService prefs; |
| 379 | + private final String name; |
| 380 | + private final LastRecentlyUsed<String> entries = |
| 381 | + new LastRecentlyUsed<>(MAX_ENTRIES); |
| 382 | + private String currentCommand = ""; |
| 383 | + private int position = -1; |
| 384 | + |
| 385 | + /** |
| 386 | + * Constructs a history object for a given scripting language. |
| 387 | + * |
| 388 | + * @param name the name of the scripting language |
| 389 | + */ |
| 390 | + public History(final PrefService prefs, final String name) { |
| 391 | + this.prefs = prefs; |
| 392 | + this.name = name; |
| 393 | + } |
| 394 | + |
| 395 | + /** |
| 396 | + * Read back a persisted history. |
| 397 | + */ |
| 398 | + public void read() { |
| 399 | + entries.clear(); |
| 400 | + for (final String item : prefs.getIterable(getClass(), PREFIX + name)) { |
| 401 | + entries.addToEnd(item); |
| 402 | + } |
| 403 | + } |
| 404 | + |
| 405 | + /** |
| 406 | + * Persist the history. |
| 407 | + * |
| 408 | + * @see PrefService |
| 409 | + */ |
| 410 | + public void write() { |
| 411 | + prefs.putIterable(getClass(), entries, PREFIX + name); |
| 412 | + } |
| 413 | + |
| 414 | + /** |
| 415 | + * Adds the most recently issued command. |
| 416 | + * |
| 417 | + * @param command the most recent command to add to the history |
| 418 | + */ |
| 419 | + public void add(final String command) { |
| 420 | + entries.add(command); |
| 421 | + position = -1; |
| 422 | + currentCommand = ""; |
| 423 | + } |
| 424 | + |
| 425 | + public boolean replace(final String command) { |
| 426 | + if (position < 0) { |
| 427 | + currentCommand = command; |
| 428 | + return false; |
| 429 | + } |
| 430 | + return entries.replace(position, command); |
| 431 | + } |
| 432 | + |
| 433 | + /** |
| 434 | + * Navigates to the next (more recent) command. |
| 435 | + * <p> |
| 436 | + * This method wraps around, i.e. it returns {@code null} when there is no |
| 437 | + * more-recent command in the history. |
| 438 | + * </p> |
| 439 | + * |
| 440 | + * @return the next command |
| 441 | + */ |
| 442 | + public String next() { |
| 443 | + position = entries.next(position); |
| 444 | + return position < 0 ? currentCommand : entries.get(position); |
| 445 | + } |
| 446 | + |
| 447 | + /** |
| 448 | + * Navigates to the previous (i.e less recent) command. |
| 449 | + * <p> |
| 450 | + * This method wraps around, i.e. it returns {@code null} when there is no |
| 451 | + * less-recent command in the history. |
| 452 | + * </p> |
| 453 | + * |
| 454 | + * @return the previous command |
| 455 | + */ |
| 456 | + public String previous() { |
| 457 | + position = entries.previous(position); |
| 458 | + return position < 0 ? currentCommand : entries.get(position); |
| 459 | + } |
| 460 | + |
| 461 | + @Override |
| 462 | + public String toString() { |
| 463 | + final StringBuilder builder = new StringBuilder(); |
| 464 | + int pos = -1; |
| 465 | + for (;;) { |
| 466 | + pos = entries.previous(pos); |
| 467 | + if (pos < 0) break; |
| 468 | + if (builder.length() > 0) builder.append(" -> "); |
| 469 | + if (this.position == pos) builder.append("["); |
| 470 | + builder.append(entries.get(pos)); |
| 471 | + if (this.position == pos) builder.append("]"); |
| 472 | + } |
| 473 | + return builder.toString(); |
| 474 | + } |
| 475 | + } |
366 | 476 | } |
0 commit comments