|
| 1 | +--- |
| 2 | +date: '2026-02-26T10:00:00+01:00' |
| 3 | +draft: false |
| 4 | +title: 'Ghost Text Suggestions' |
| 5 | +weight: 9 |
| 6 | +--- |
| 7 | + |
| 8 | +Ghost text suggestions display inline, dimmed text ahead of the cursor as you type, showing what Æsh predicts you want to enter. Unlike [tab completion](completers), which requires pressing Tab, ghost text appears automatically and can be accepted by pressing the right arrow key. |
| 9 | + |
| 10 | +## How It Works |
| 11 | + |
| 12 | +As you type, ghost text suggestions appear automatically: |
| 13 | + |
| 14 | +``` |
| 15 | +myapp> ca|che ← "che" appears dimmed after cursor |
| 16 | +myapp> connect --ho|st= ← "st=" appears dimmed after cursor |
| 17 | +myapp> git co|mmit ← "mmit" appears dimmed after cursor |
| 18 | +``` |
| 19 | + |
| 20 | +- **Right arrow** accepts the suggestion |
| 21 | +- **Keep typing** to narrow or dismiss the suggestion |
| 22 | +- Only shown when there is exactly one unambiguous match |
| 23 | + |
| 24 | +## CommandSuggestionProvider |
| 25 | + |
| 26 | +`CommandSuggestionProvider` uses the command registry to suggest command names, subcommand names, and option names as you type. It implements the `SuggestionProvider` interface from Æsh Readline. |
| 27 | + |
| 28 | +### What It Suggests |
| 29 | + |
| 30 | +| Context | Example Input | Suggestion | |
| 31 | +|---------|--------------|------------| |
| 32 | +| Command names | `ca` | `che` (from `cache`) | |
| 33 | +| Subcommand names | `git co` | `mmit` (from `commit`) | |
| 34 | +| Option names | `connect --ho` | `st=` (from `--host`) | |
| 35 | + |
| 36 | +Suggestions are only shown when there is a single unambiguous match. If the input `c` could match both `cache` and `connect`, no suggestion is shown. |
| 37 | + |
| 38 | +For options that accept a value, the suggestion appends `=` after the option name. Boolean options (flags) do not get the trailing `=`. |
| 39 | + |
| 40 | +### Setup |
| 41 | + |
| 42 | +Create a `CommandSuggestionProvider` from your command registry and attach it to the console: |
| 43 | + |
| 44 | +```java |
| 45 | +import org.aesh.command.impl.completer.CommandSuggestionProvider; |
| 46 | +import org.aesh.console.ReadlineConsole; |
| 47 | + |
| 48 | +ReadlineConsole console = new ReadlineConsole(settings); |
| 49 | + |
| 50 | +// Create suggestion provider from the console's registry |
| 51 | +CommandSuggestionProvider<?> provider = |
| 52 | + new CommandSuggestionProvider<>(console.getCommandRegistry()); |
| 53 | + |
| 54 | +console.setSuggestionProvider(provider); |
| 55 | +console.start(); |
| 56 | +``` |
| 57 | + |
| 58 | +### With AeshConsoleRunner |
| 59 | + |
| 60 | +If you use `AeshConsoleRunner`, you can access the underlying `ReadlineConsole` to set up suggestions: |
| 61 | + |
| 62 | +```java |
| 63 | +ReadlineConsole console = new ReadlineConsole(settings); |
| 64 | +console.setPrompt(new Prompt("myapp> ")); |
| 65 | +console.setSuggestionProvider( |
| 66 | + new CommandSuggestionProvider<>(console.getCommandRegistry())); |
| 67 | +console.start(); |
| 68 | +``` |
| 69 | + |
| 70 | +## CompositeSuggestionProvider |
| 71 | + |
| 72 | +You can combine multiple suggestion sources using `CompositeSuggestionProvider`. The first provider that returns a non-null suggestion wins. This lets you layer history-based suggestions with command-based suggestions: |
| 73 | + |
| 74 | +```java |
| 75 | +import org.aesh.readline.CompositeSuggestionProvider; |
| 76 | +import org.aesh.readline.SuggestionProvider; |
| 77 | + |
| 78 | +// History-based suggestions (checked first) |
| 79 | +SuggestionProvider historyProvider = buffer -> { |
| 80 | + // Return matching history entry suffix, or null |
| 81 | + return findHistoryMatch(buffer); |
| 82 | +}; |
| 83 | + |
| 84 | +// Command registry suggestions (fallback) |
| 85 | +CommandSuggestionProvider<?> commandProvider = |
| 86 | + new CommandSuggestionProvider<>(console.getCommandRegistry()); |
| 87 | + |
| 88 | +// Combine: history takes priority, falls back to commands |
| 89 | +CompositeSuggestionProvider composite = |
| 90 | + new CompositeSuggestionProvider(historyProvider, commandProvider); |
| 91 | + |
| 92 | +console.setSuggestionProvider(composite); |
| 93 | +``` |
| 94 | + |
| 95 | +## Custom SuggestionProvider |
| 96 | + |
| 97 | +You can implement the `SuggestionProvider` interface directly for custom suggestion logic: |
| 98 | + |
| 99 | +```java |
| 100 | +import org.aesh.readline.SuggestionProvider; |
| 101 | + |
| 102 | +SuggestionProvider myProvider = buffer -> { |
| 103 | + // Return the suffix to append as ghost text, or null for no suggestion |
| 104 | + if (buffer.startsWith("hel")) { |
| 105 | + return "lo"; |
| 106 | + } |
| 107 | + return null; |
| 108 | +}; |
| 109 | + |
| 110 | +console.setSuggestionProvider(myProvider); |
| 111 | +``` |
| 112 | + |
| 113 | +The `suggest` method receives the current input buffer and returns the text to display after the cursor as ghost text. Return `null` when there is no suggestion. |
| 114 | + |
| 115 | +## Ghost Text vs Tab Completion |
| 116 | + |
| 117 | +| Feature | Ghost Text | Tab Completion | |
| 118 | +|---------|-----------|----------------| |
| 119 | +| **Trigger** | Automatic as you type | Press Tab | |
| 120 | +| **Display** | Inline dimmed text | List below prompt | |
| 121 | +| **Matches** | Single unambiguous only | Shows all matches | |
| 122 | +| **Accept** | Right arrow | Tab / Enter | |
| 123 | +| **Scope** | Commands, subcommands, options | Options, arguments, custom values | |
| 124 | + |
| 125 | +Both features complement each other. Ghost text gives fast inline suggestions for unambiguous matches, while tab completion helps explore all available options when there are multiple possibilities. |
0 commit comments