Skip to content

Commit

Permalink
Revert "Use Terminal to clear the screen without redraw, instead of u…
Browse files Browse the repository at this point in the history
…sing LineReaderImpl, as suggested in jline/jline3#610"

This reverts commit 6f7523a.
  • Loading branch information
MarkoMackic committed Oct 17, 2021
1 parent b97f03a commit a872bd8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.InfoCmp.Capability;

import picocli.CommandLine;
import picocli.CommandLine.Command;
Expand All @@ -45,14 +44,14 @@ public class PicocliCommands implements CommandRegistry {
/**
* Command that clears the screen.
* <p>
* <b>WARNING:</b> This subcommand needs a JLine {@code Terminal} to clear the screen.
* <b>WARNING:</b> This subcommand needs a JLine {@code LineReaderImpl} to clear the screen.
* To accomplish this, construct the {@code CommandLine} with a {@code PicocliCommandsFactory},
* and set the {@code Terminal} on that factory. For example:
* and set the {@code LineReaderImpl} on that factory. For example:
* <pre>
* PicocliCommandsFactory factory = new PicocliCommandsFactory();
* CommandLine cmd = new CommandLine(new MyApp(), factory);
* // create terminal
* factory.setTerminal(terminal);
* LineReaderImpl readerImpl = ... // create reader
* factory.setLineReader(readerImpl);
* </pre>
*
* @since 4.6
Expand All @@ -61,31 +60,31 @@ public class PicocliCommands implements CommandRegistry {
description = "Clears the screen", version = "1.0")
public static class ClearScreen implements Callable<Void> {

private final Terminal terminal;
private final LineReaderImpl reader;

ClearScreen(Terminal terminal) { this.terminal = terminal; }
ClearScreen(LineReaderImpl reader) { this.reader = reader; }

public Void call() throws IOException {
if (terminal != null) { terminal.puts(Capability.clear_screen); }
if (reader != null) { reader.clearScreen(); }
return null;
}
}

/**
* Command factory that is necessary for applications that want the use the {@code ClearScreen} subcommand.
* It allows chaining (or delegating) to a custom factory.
* It allows chaining (or delegrating) to a custom factory.
* <p>
* <b>WARNING:</b> If the application uses the {@code ClearScreen} subcommand, construct the {@code CommandLine}
* with a {@code PicocliCommandsFactory}, and set the {@code Terminal} on that factory. Applications need
* to call the {@code setTerminal} method with a {@code Terminal}; this will be passed to the {@code ClearScreen}
* with a {@code PicocliCommandsFactory}, and set the {@code LineReaderImpl} on that factory. Applications need
* to call the setLineReader method with a {@code LineReaderImpl}; this will be passed to the {@code ClearScreen}
* subcommand.
*
* For example:
* <pre>
* PicocliCommandsFactory factory = new PicocliCommandsFactory();
* CommandLine cmd = new CommandLine(new MyApp(), factory);
* // create terminal
* factory.setTerminal(terminal);
* LineReaderImpl readerImpl = ... // create reader
* factory.setLineReader(readerImpl);
* </pre>
*
* Custom factories can be chained by passing them in to the constructor like this:
Expand All @@ -98,27 +97,27 @@ public Void call() throws IOException {
*/
public static class PicocliCommandsFactory implements CommandLine.IFactory {
private CommandLine.IFactory nextFactory;
private Terminal terminal;
private LineReaderImpl reader;

public PicocliCommandsFactory() {
// nextFactory and terminal are null
// nextFactory and line reader are null
}

public PicocliCommandsFactory(IFactory nextFactory) {
this.nextFactory = nextFactory;
// nextFactory is set (but may be null) and terminal is null
// nextFactory is set (but may be null) and line reader is null
}

@SuppressWarnings("unchecked")
public <K> K create(Class<K> clazz) throws Exception {
if (ClearScreen.class == clazz) { return (K) new ClearScreen(terminal); }
if (ClearScreen.class == clazz) { return (K) new ClearScreen(reader); }
if (nextFactory != null) { return nextFactory.create(clazz); }
return CommandLine.defaultFactory().create(clazz);
}

public void setTerminal(Terminal terminal) {
this.terminal = terminal;
// terminal may be null, so check before using it in ClearScreen command
public void setLineReader(LineReaderImpl reader) {
this.reader = reader;
// reader may be null, so check before using it in ClearScreen command
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static void main(String[] args) {
.variable(LineReader.LIST_MAX, 50) // max tab completion candidates
.build();
builtins.setLineReader(reader);
factory.setTerminal(terminal);
factory.setLineReader((LineReaderImpl) reader);
TailTipWidgets widgets = new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TailTipWidgets.TipType.COMPLETER);
widgets.enable();
KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
Expand Down

0 comments on commit a872bd8

Please sign in to comment.