Skip to content

Commit

Permalink
Add editing-mode option to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr authored and hashhar committed Mar 11, 2022
1 parent 5297372 commit 396d249
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
22 changes: 22 additions & 0 deletions client/trino-cli/src/main/java/io/trino/cli/ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.trino.client.ClientSession;
import io.trino.client.auth.external.ExternalRedirectStrategy;
import okhttp3.logging.HttpLoggingInterceptor;
import org.jline.reader.LineReader;

import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -175,6 +176,9 @@ public class ClientOptions
@Option(names = "--disable-compression", description = "Disable compression of query results")
public boolean disableCompression;

@Option(names = "--editing-mode", paramLabel = "<editing-mode>", defaultValue = "EMACS", description = "Editing mode [${COMPLETION-CANDIDATES}] " + DEFAULT_VALUE)
public EditingMode editingMode;

public enum OutputFormat
{
ALIGNED,
Expand All @@ -189,6 +193,24 @@ public enum OutputFormat
NULL
}

public enum EditingMode
{
EMACS(LineReader.EMACS),
VI(LineReader.VIINS);

private final String keyMap;

EditingMode(String keyMap)
{
this.keyMap = keyMap;
}

public String getKeyMap()
{
return keyMap;
}
}

public ClientSession toClientSession()
{
return new ClientSession(
Expand Down
6 changes: 3 additions & 3 deletions client/trino-cli/src/main/java/io/trino/cli/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public boolean run()
clientOptions.progress);
}

runConsole(queryRunner, exiting);
runConsole(queryRunner, exiting, clientOptions.editingMode);
return true;
}
finally {
Expand Down Expand Up @@ -220,10 +220,10 @@ private String getPassword()
return reader.readLine("Password: ", (char) 0);
}

private static void runConsole(QueryRunner queryRunner, AtomicBoolean exiting)
private static void runConsole(QueryRunner queryRunner, AtomicBoolean exiting, ClientOptions.EditingMode editingMode)
{
try (TableNameCompleter tableNameCompleter = new TableNameCompleter(queryRunner);
InputReader reader = new InputReader(getHistoryFile(), commandCompleter(), tableNameCompleter)) {
InputReader reader = new InputReader(editingMode, getHistoryFile(), commandCompleter(), tableNameCompleter)) {
tableNameCompleter.populateCache();
String remaining = "";
while (!exiting.get()) {
Expand Down
4 changes: 3 additions & 1 deletion client/trino-cli/src/main/java/io/trino/cli/InputReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static io.trino.cli.TerminalUtils.isRealTerminal;
import static org.jline.reader.LineReader.BLINK_MATCHING_PAREN;
import static org.jline.reader.LineReader.HISTORY_FILE;
import static org.jline.reader.LineReader.MAIN;
import static org.jline.reader.LineReader.Option.HISTORY_TIMESTAMPED;
import static org.jline.reader.LineReader.SECONDARY_PROMPT_PATTERN;
import static org.jline.utils.AttributedStyle.BRIGHT;
Expand All @@ -39,7 +40,7 @@ public class InputReader
{
private final LineReader reader;

public InputReader(Path historyFile, Completer... completers)
public InputReader(ClientOptions.EditingMode editingMode, Path historyFile, Completer... completers)
throws IOException
{
reader = LineReaderBuilder.builder()
Expand All @@ -52,6 +53,7 @@ public InputReader(Path historyFile, Completer... completers)
.completer(new AggregateCompleter(completers))
.build();

reader.getKeyMaps().put(MAIN, reader.getKeyMaps().get(editingMode.getKeyMap()));
reader.unsetOpt(HISTORY_TIMESTAMPED);
}

Expand Down

1 comment on commit 396d249

@mosabua
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.