Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy activation of single letter aliases #23497

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.quarkus.deployment.console;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -589,11 +592,51 @@ public void run() {
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Map<Character, String> singleLetterAliases() {
try {
var manager = new AliasManager(Paths.get(System.getProperty("user.home")).resolve(ALIAS_FILE).toFile(), true);
Map<Character, String> ret = new HashMap<>();
for (String alias : manager.getAllNames()) {
if (alias.length() == 1) {
ret.put(alias.charAt(0), manager.getAlias(alias).get().getValue());
}
}
return ret;
} catch (IOException e) {
return Map.of();
}
}

@Override
public void runAlias(char alias) {
try {
AeshCommandRegistryBuilder<CommandInvocation> commandBuilder = AeshCommandRegistryBuilder.builder();
ConsoleCliManager.commands.forEach(commandBuilder::command);

CommandRegistry registry = commandBuilder
.create();
Settings settings = SettingsBuilder
.builder()
.enableExport(false)
.inputStream(new ByteArrayInputStream(new byte[] { (byte) alias, '\n' }))
.enableAlias(true)
.aliasManager(
new AliasManager(Paths.get(System.getProperty("user.home")).resolve(ALIAS_FILE).toFile(), true))
.connection(delegateConnection)
.commandRegistry(registry)
.build();
aeshConsole = new ReadlineConsole(settings);
aeshConsole.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void exitCliMode() {
if (aeshConsole == null) {
if (aeshConsole == null || delegateConnection == null) {
return;
}
aeshConsole.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ public class ConsoleStateManager {
@Override
public void accept(int[] ints) {
for (int i : ints) {
char key = (char) i;
if (readLineBuilder != null) {
if (i == '\n' || i == '\r') {
readLineConsumer.accept(readLineBuilder.toString());
readLineBuilder = null;
readLineConsumer = null;
} else {
readLineBuilder.append((char) i);
readLineBuilder.append(key);
}
} else {
if (i == '\n') {
System.out.println("");
}
Holder command = commands.get((char) i);
Holder command = commands.get(key);
if (command != null) {
if (command.consoleCommand.getReadLineHandler() != null) {
QuarkusConsole.INSTANCE.doReadLine();
Expand All @@ -72,6 +73,10 @@ public void accept(int[] ints) {
} else {
command.consoleCommand.getRunnable().run();
}
} else {
if (QuarkusConsole.INSTANCE.singleLetterAliases().containsKey(key)) {
QuarkusConsole.INSTANCE.runAlias(key);
}
}
}
}
Expand Down Expand Up @@ -203,11 +208,16 @@ private void printHelp() {
for (Holder i : commands.values()) {
contexts.add(i.context);
}
Set<Character> seen = new HashSet<>();

for (ConsoleContext ctx : contexts.stream().sorted(Comparator.comparing(ConsoleContext::getName))
.collect(Collectors.toList())) {
System.out.println("\n" + RED + "==" + RESET + " " + UNDERLINE + ctx.name + NO_UNDERLINE + "\n");
for (var i : ctx.internal) {
if (seen.contains(i.getKey())) {
continue;
}
seen.add(i.getKey());
if (i.getDescription() != null) {
if (i.getHelpState() == null) {
System.out.println(helpOption(i.getKey(), i.getDescription()));
Expand All @@ -220,7 +230,15 @@ private void printHelp() {
}
}
}

Map<Character, String> aliases = QuarkusConsole.INSTANCE.singleLetterAliases();
if (!aliases.isEmpty()) {
System.out.println("\n" + RED + "==" + RESET + " " + UNDERLINE + "User Defined Aliases" + NO_UNDERLINE + "\n");
for (Map.Entry<Character, String> entry : aliases.entrySet()) {
if (!seen.contains(entry.getKey())) {
System.out.println(helpOption(entry.getKey(), entry.getValue()));
}
}
}
}

static class Holder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.PrintStream;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.BiPredicate;
Expand Down Expand Up @@ -152,6 +153,20 @@ public void exitCliMode() {
//noop for the non-aesh console
}

/**
* Exposes single character aliases so they can be displayed in the help screen
*/
public Map<Character, String> singleLetterAliases() {
return Map.of();
}

/**
* runs a single letter alias
*/
public void runAlias(char alias) {

}

protected String stripAnsiCodes(String s) {
if (s == null) {
return null;
Expand Down