Skip to content

Commit 1154e0e

Browse files
committed
Add ShellBuilder.onReaderReady(BiConsumer<LineReader, CommandDispatcher>)
Add overloaded onReaderReady that also provides the CommandDispatcher, enabling setup of CommandTailTipWidgets which require both the reader and the dispatcher. The existing Consumer<LineReader> overload is preserved for backwards compatibility. Update ShellJobExample to use CommandTailTipWidgets instead of the deprecated TailTipWidgets.
1 parent e247886 commit 1154e0e

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

demo/src/main/java/org/jline/demo/examples/ShellJobExample.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
import org.jline.shell.impl.DefaultAliasManager;
1616
import org.jline.shell.impl.DefaultJobManager;
1717
import org.jline.shell.impl.SimpleCommandGroup;
18-
import org.jline.widget.TailTipWidgets;
19-
import org.jline.widget.TailTipWidgets.TipType;
18+
import org.jline.shell.widget.CommandTailTipWidgets;
2019

2120
/**
2221
* Example demonstrating job control, aliases, built-in commands, and syntax highlighting.
@@ -148,9 +147,9 @@ public static void main(String[] args) throws Exception {
148147
"demo", new EchoCommand(), new UpperCommand(), new SleepCommand(), new CountCommand()))
149148
.option(Option.INSERT_BRACKET, true)
150149
.option(Option.DISABLE_EVENT_EXPANSION, true)
151-
.onReaderReady(reader -> {
150+
.onReaderReady((reader, dispatcher) -> {
152151
try {
153-
new TailTipWidgets(reader, desc -> null, 5, TipType.COMPLETER);
152+
new CommandTailTipWidgets(reader, dispatcher, 5).enable();
154153
} catch (Exception e) {
155154
// ignore
156155
}

shell/src/main/java/org/jline/shell/ShellBuilder.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.IOException;
1313
import java.nio.file.Path;
1414
import java.util.*;
15+
import java.util.function.BiConsumer;
1516
import java.util.function.Consumer;
1617
import java.util.function.Supplier;
1718

@@ -40,7 +41,7 @@
4041
* .historyFile(path) // optional
4142
* .variable(name, value) // forwarded to LineReader
4243
* .option(Option.X, true) // forwarded to LineReader
43-
* .onReaderReady(reader -&gt; { ... })// optional callback
44+
* .onReaderReady((reader, dispatcher) -&gt; { ... })// optional callback
4445
* .build();
4546
* </pre>
4647
*
@@ -58,7 +59,7 @@ public class ShellBuilder {
5859
private Supplier<String> rightPromptSupplier;
5960
private final Map<String, Object> variables = new LinkedHashMap<>();
6061
private final Map<Option, Boolean> options = new LinkedHashMap<>();
61-
private Consumer<LineReader> onReaderReady;
62+
private BiConsumer<LineReader, CommandDispatcher> onReaderReady;
6263
private File initScript;
6364
private JobManager jobManager;
6465
private PipelineParser pipelineParser;
@@ -201,12 +202,35 @@ public ShellBuilder option(Option option, boolean value) {
201202
/**
202203
* Sets a callback invoked after the LineReader is created but before {@link Shell#run()}.
203204
* <p>
204-
* This is useful for setting up TailTipWidgets or other post-reader customizations.
205+
* This is useful for setting up post-reader customizations.
205206
*
206-
* @param onReaderReady the callback
207+
* @param onReaderReady the callback receiving the LineReader
207208
* @return this builder
209+
* @see #onReaderReady(BiConsumer)
208210
*/
209211
public ShellBuilder onReaderReady(Consumer<LineReader> onReaderReady) {
212+
this.onReaderReady = (reader, dispatcher) -> onReaderReady.accept(reader);
213+
return this;
214+
}
215+
216+
/**
217+
* Sets a callback invoked after the LineReader is created but before {@link Shell#run()}.
218+
* <p>
219+
* This is useful for setting up {@link org.jline.shell.widget.CommandTailTipWidgets}
220+
* or other post-reader customizations that need access to both the reader and the dispatcher.
221+
*
222+
* <pre>
223+
* Shell.builder()
224+
* .onReaderReady((reader, dispatcher) -&gt; {
225+
* new CommandTailTipWidgets(reader, dispatcher, 5).enable();
226+
* })
227+
* .build();
228+
* </pre>
229+
*
230+
* @param onReaderReady the callback receiving the LineReader and CommandDispatcher
231+
* @return this builder
232+
*/
233+
public ShellBuilder onReaderReady(BiConsumer<LineReader, CommandDispatcher> onReaderReady) {
210234
this.onReaderReady = onReaderReady;
211235
return this;
212236
}
@@ -460,7 +484,7 @@ public Shell build() throws IOException {
460484

461485
// Callback
462486
if (onReaderReady != null) {
463-
onReaderReady.accept(reader);
487+
onReaderReady.accept(reader, disp);
464488
}
465489

466490
return new Shell(term, ownTerminal, reader, disp, prompt, rightPromptSupplier, initScript, jobManager);

0 commit comments

Comments
 (0)