Repository: karaf Updated Branches: refs/heads/master d3e8e38cd -> 5de27617e
KARAF-2772 Make some classes independent from jansi and jline Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/4e47afae Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/4e47afae Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/4e47afae Branch: refs/heads/master Commit: 4e47afae55180beed90196dd7ed4ae8f848d60b2 Parents: b5c0ec0 Author: Christian Schneider <[email protected]> Authored: Thu Feb 20 22:28:33 2014 +0100 Committer: Christian Schneider <[email protected]> Committed: Thu Feb 20 22:28:33 2014 +0100 ---------------------------------------------------------------------- .../felix/gogo/commands/CommandException.java | 10 +- .../commands/basic/DefaultActionPreparator.java | 212 +++++++------------ .../karaf/shell/commands/CommandException.java | 10 +- .../commands/basic/DefaultActionPreparator.java | 58 ++--- .../shell/commands/meta/ActionMetaData.java | 23 +- .../shell/console/completer/FileCompleter.java | 126 ++++++++++- .../karaf/shell/util/CommandSessionUtil.java | 28 +++ .../org/apache/karaf/shell/util/ShellUtil.java | 48 ++--- .../org/apache/karaf/shell/util/SimpleAnsi.java | 30 +++ 9 files changed, 312 insertions(+), 233 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/felix/gogo/commands/CommandException.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/felix/gogo/commands/CommandException.java b/shell/console/src/main/java/org/apache/felix/gogo/commands/CommandException.java index 3348eda..01c652a 100644 --- a/shell/console/src/main/java/org/apache/felix/gogo/commands/CommandException.java +++ b/shell/console/src/main/java/org/apache/felix/gogo/commands/CommandException.java @@ -18,7 +18,7 @@ */ package org.apache.felix.gogo.commands; -import org.fusesource.jansi.Ansi; +import org.apache.karaf.shell.util.SimpleAnsi; /** * Base class for exceptions thrown when executing commands. @@ -55,11 +55,9 @@ public class CommandException extends Exception { public String getNiceHelp() { return help != null ? help - : Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command: ") - .a(getMessage() != null ? getMessage() : getClass().getName()) - .fg(Ansi.Color.DEFAULT).toString(); + : SimpleAnsi.COLOR_RED + "Error executing command: " + + getMessage() != null ? getMessage() : getClass().getName() + + SimpleAnsi.COLOR_DEFAULT; } } http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java b/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java index 9522811..e099294 100644 --- a/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java +++ b/shell/console/src/main/java/org/apache/felix/gogo/commands/basic/DefaultActionPreparator.java @@ -18,6 +18,11 @@ */ package org.apache.felix.gogo.commands.basic; +import static org.apache.karaf.shell.util.SimpleAnsi.COLOR_DEFAULT; +import static org.apache.karaf.shell.util.SimpleAnsi.COLOR_RED; +import static org.apache.karaf.shell.util.SimpleAnsi.INTENSITY_BOLD; +import static org.apache.karaf.shell.util.SimpleAnsi.INTENSITY_NORMAL; + import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -31,7 +36,6 @@ import java.io.PrintStream; import java.util.regex.Matcher; import java.util.regex.Pattern; -import jline.Terminal; import org.apache.felix.gogo.commands.CommandException; import org.apache.felix.gogo.commands.Option; import org.apache.felix.gogo.commands.Action; @@ -41,7 +45,7 @@ import org.apache.felix.gogo.commands.converter.DefaultConverter; import org.apache.felix.gogo.commands.converter.GenericType; import org.apache.felix.service.command.CommandSession; import org.apache.karaf.shell.console.NameScoping; -import org.fusesource.jansi.Ansi; +import org.apache.karaf.shell.util.CommandSessionUtil; @Deprecated public class DefaultActionPreparator implements ActionPreparator { @@ -172,22 +176,14 @@ public class DefaultActionPreparator implements ActionPreparator { if (option == null) { Command command = action.getClass().getAnnotation(Command.class); if (command != null) { - throw new CommandException( - Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command ") - .a(command.scope()) - .a(":") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(command.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(" undefined option ") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(param) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .fg(Ansi.Color.DEFAULT) - .toString(), - "Undefined option: " + param + throw new CommandException(COLOR_RED + + "Error executing command " + + command.scope() + ":" + + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + + " undefined option " + + INTENSITY_BOLD + param + INTENSITY_NORMAL + + COLOR_DEFAULT, + "Undefined option: " + param ); } else { throw new CommandException("Undefined option: " + param); @@ -203,22 +199,14 @@ public class DefaultActionPreparator implements ActionPreparator { if (value == null) { Command command = action.getClass().getAnnotation(Command.class); if (command != null) { - throw new CommandException( - Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command ") - .a(command.scope()) - .a(":") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(command.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(" missing value for option ") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(param) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .fg(Ansi.Color.DEFAULT) - .toString(), - "Missing value for option: " + param + throw new CommandException(COLOR_RED + + "Error executing command " + + command.scope() + ":" + + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + + " missing value for option " + + INTENSITY_BOLD + param + INTENSITY_NORMAL + + COLOR_DEFAULT, + "Missing value for option: " + param ); } else { throw new CommandException("Missing value for option: " + param); @@ -239,19 +227,14 @@ public class DefaultActionPreparator implements ActionPreparator { if (argIndex >= orderedArguments.size()) { Command command = action.getClass().getAnnotation(Command.class); if (command != null) { - throw new CommandException( - Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command ") - .a(command.scope()) - .a(":") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(command.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(": too many arguments specified") - .fg(Ansi.Color.DEFAULT) - .toString(), - "Too many arguments specified" + throw new CommandException(COLOR_RED + + "Error executing command " + + command.scope() + ":" + + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + + ": too many arguments specified" + + INTENSITY_BOLD + param + INTENSITY_NORMAL + + COLOR_DEFAULT, + "Too many arguments specified" ); } else { throw new CommandException("Too many arguments specified"); @@ -278,22 +261,14 @@ public class DefaultActionPreparator implements ActionPreparator { if (option.required() && optionValues.get(option) == null) { Command command = action.getClass().getAnnotation(Command.class); if (command != null) { - throw new CommandException( - Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command ") - .a(command.scope()) - .a(":") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(command.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(": option ") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(option.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(" is required") - .fg(Ansi.Color.DEFAULT) - .toString(), + throw new CommandException(COLOR_RED + + "Error executing command " + + command.scope() + ":" + + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + + ": option " + + INTENSITY_BOLD + option.name() + INTENSITY_NORMAL + + " is required" + + COLOR_DEFAULT, "Option " + option.name() + " is required" ); } else { @@ -305,23 +280,15 @@ public class DefaultActionPreparator implements ActionPreparator { if (argument.required() && argumentValues.get(argument) == null) { Command command = action.getClass().getAnnotation(Command.class); if (command != null) { - throw new CommandException( - Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command ") - .a(command.scope()) - .a(":") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(command.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(": argument ") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(argument.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(" is required") - .fg(Ansi.Color.DEFAULT) - .toString(), - "Argument " + argument.name() + " is required" + throw new CommandException(COLOR_RED + + "Error executing command " + + command.scope() + ":" + + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + + ": argument " + + INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL + + " is required" + + COLOR_DEFAULT, + "Argument " + argument.name() + " is required" ); } else { throw new CommandException("Argument " + argument.name() + " is required"); @@ -337,25 +304,15 @@ public class DefaultActionPreparator implements ActionPreparator { } catch (Exception e) { Command command = action.getClass().getAnnotation(Command.class); if (command != null) { - throw new CommandException( - Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command ") - .a(command.scope()) - .a(":") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(command.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(": unable to convert option ") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(entry.getKey().name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(" with value '") - .a(entry.getValue()) - .a("' to type ") - .a(new GenericType(field.getGenericType()).toString()) - .fg(Ansi.Color.DEFAULT) - .toString(), + throw new CommandException(COLOR_RED + + "Error executing command " + + command.scope() + ":" + + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + + ": unable to convert option " + + INTENSITY_BOLD + entry.getKey().name() + INTENSITY_NORMAL + + " with value '" + entry.getValue() + "' to type " + + new GenericType(field.getGenericType()).toString() + + COLOR_DEFAULT, "Unable to convert option " + entry.getKey().name() + " with value '" + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), e @@ -377,25 +334,15 @@ public class DefaultActionPreparator implements ActionPreparator { } catch (Exception e) { Command command = action.getClass().getAnnotation(Command.class); if (command != null) { - throw new CommandException( - Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command ") - .a(command.scope()) - .a(":") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(command.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(": unable to convert argument ") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(entry.getKey().name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .a(" with value '") - .a(entry.getValue()) - .a("' to type ") - .a(new GenericType(field.getGenericType()).toString()) - .fg(Ansi.Color.DEFAULT) - .toString(), + throw new CommandException(COLOR_RED + + "Error executing command " + + command.scope() + ":" + + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + + ": unable to convert argument " + + INTENSITY_BOLD + entry.getKey().name() + INTENSITY_NORMAL + + " with value '" + entry.getValue() + "' to type " + + new GenericType(field.getGenericType()).toString() + + COLOR_DEFAULT, "Unable to convert argument " + entry.getKey().name() + " with value '" + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), e @@ -415,7 +362,7 @@ public class DefaultActionPreparator implements ActionPreparator { protected void printUsage(CommandSession session, Action action, Map<Option, Field> optionsMap, Map<Argument, Field> argsMap, PrintStream out) { Command command = action.getClass().getAnnotation(Command.class); if (command != null) { - Terminal term = session != null ? (Terminal) session.get(".jline.terminal") : null; + List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet()); Collections.sort(arguments, new Comparator<Argument>() { public int compare(Argument o1, Argument o2) { @@ -426,13 +373,13 @@ public class DefaultActionPreparator implements ActionPreparator { options.add(HELP); boolean globalScope = NameScoping.isGlobalScope(session, command.scope()); if (command != null && (command.description() != null || command.name() != null)) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DESCRIPTION").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "DESCRIPTION" + INTENSITY_NORMAL); out.print(" "); if (command.name() != null) { if (globalScope) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + command.name() + INTENSITY_NORMAL); } else { - out.println(Ansi.ansi().a(command.scope()).a(":").a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET)); + out.println(command.scope() + ":" + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL); } out.println(); } @@ -461,17 +408,18 @@ public class DefaultActionPreparator implements ActionPreparator { } } } - - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("SYNTAX").a(Ansi.Attribute.RESET)); + int width = CommandSessionUtil.getWidth(session); + out.println(INTENSITY_BOLD + "SYNTAX" + INTENSITY_NORMAL); out.print(" "); out.println(syntax.toString()); out.println(); if (arguments.size() > 0) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("ARGUMENTS").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "ARGUMENTS" + INTENSITY_NORMAL); for (Argument argument : arguments) { out.print(" "); - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(argument.name()).a(Ansi.Attribute.RESET)); - printFormatted(" ", argument.description(), term != null ? term.getWidth() : 80, out); + out.println(INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL); + + printFormatted(" ", argument.description(), width, out); if (!argument.required()) { if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) { try { @@ -491,15 +439,15 @@ public class DefaultActionPreparator implements ActionPreparator { out.println(); } if (options.size() > 0) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("OPTIONS").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "OPTIONS" + INTENSITY_NORMAL); for (Option option : options) { String opt = option.name(); for (String alias : option.aliases()) { opt += ", " + alias; } out.print(" "); - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(opt).a(Ansi.Attribute.RESET)); - printFormatted(" ", option.description(), term != null ? term.getWidth() : 80, out); + out.println(INTENSITY_BOLD + opt + INTENSITY_NORMAL); + printFormatted(" ", option.description(), width, out); if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) { try { if (Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) { @@ -517,9 +465,9 @@ public class DefaultActionPreparator implements ActionPreparator { out.println(); } if (command.detailedDescription().length() > 0) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DETAILS").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "DETAILS" + INTENSITY_NORMAL); String desc = loadDescription(action.getClass(), command.detailedDescription()); - printFormatted(" ", desc, term != null ? term.getWidth() : 80, out); + printFormatted(" ", desc, width, out); } } } http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/karaf/shell/commands/CommandException.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/commands/CommandException.java b/shell/console/src/main/java/org/apache/karaf/shell/commands/CommandException.java index 2d60e41..7bcaad5 100644 --- a/shell/console/src/main/java/org/apache/karaf/shell/commands/CommandException.java +++ b/shell/console/src/main/java/org/apache/karaf/shell/commands/CommandException.java @@ -18,7 +18,7 @@ */ package org.apache.karaf.shell.commands; -import org.fusesource.jansi.Ansi; +import org.apache.karaf.shell.util.SimpleAnsi; /** * Base class for exceptions thrown when executing commands. @@ -55,11 +55,9 @@ public class CommandException extends Exception { public String getNiceHelp() { return help != null ? help - : Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command: ") - .a(getMessage() != null ? getMessage() : getClass().getName()) - .fg(Ansi.Color.DEFAULT).toString(); + : SimpleAnsi.COLOR_RED + "Error executing command: " + + getMessage() != null ? getMessage() : getClass().getName() + + SimpleAnsi.COLOR_DEFAULT; } } http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/karaf/shell/commands/basic/DefaultActionPreparator.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/commands/basic/DefaultActionPreparator.java b/shell/console/src/main/java/org/apache/karaf/shell/commands/basic/DefaultActionPreparator.java index e685538..baa0b43 100644 --- a/shell/console/src/main/java/org/apache/karaf/shell/commands/basic/DefaultActionPreparator.java +++ b/shell/console/src/main/java/org/apache/karaf/shell/commands/basic/DefaultActionPreparator.java @@ -18,6 +18,10 @@ */ package org.apache.karaf.shell.commands.basic; +import static org.apache.karaf.shell.util.SimpleAnsi.COLOR_RED; +import static org.apache.karaf.shell.util.SimpleAnsi.INTENSITY_BOLD; +import static org.apache.karaf.shell.util.SimpleAnsi.INTENSITY_NORMAL; + import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.ArrayList; @@ -27,8 +31,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import jline.Terminal; - import org.apache.felix.gogo.commands.Action; import org.apache.felix.service.command.CommandSession; import org.apache.karaf.shell.commands.Argument; @@ -41,7 +43,8 @@ import org.apache.karaf.shell.commands.converter.GenericType; import org.apache.karaf.shell.commands.meta.ActionMetaData; import org.apache.karaf.shell.commands.meta.ActionMetaDataFactory; import org.apache.karaf.shell.console.NameScoping; -import org.fusesource.jansi.Ansi; +import org.apache.karaf.shell.util.CommandSessionUtil; +import org.apache.karaf.shell.util.SimpleAnsi; public class DefaultActionPreparator implements ActionPreparator { @@ -57,22 +60,14 @@ public class DefaultActionPreparator implements ActionPreparator { return true; } - String commandErrorSt = (command2 != null) ? Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Error executing command ") - .a(command2.scope()) - .a(":") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(command2.name()) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .fg(Ansi.Color.DEFAULT) - .a(": ") - .toString() : ""; + String commandErrorSt = (command2 != null) ? COLOR_RED + + "Error executing command " + command2.scope() + ":" + + INTENSITY_BOLD + command2.name() + INTENSITY_NORMAL + + SimpleAnsi.COLOR_DEFAULT + ": " : ""; for (Iterator<Object> it = params.iterator(); it.hasNext(); ) { Object param = it.next(); if (HelpOption.HELP.name().equals(param)) { - Terminal term = session != null ? (Terminal) session.get(".jline.terminal") : null; - int termWidth = term != null ? term.getWidth() : 80; + int termWidth = CommandSessionUtil.getWidth(session); boolean globalScope = NameScoping.isGlobalScope(session, actionMetaData.getCommand().scope()); actionMetaData.printUsage(action, System.out, globalScope, termWidth); return false; @@ -105,15 +100,9 @@ public class DefaultActionPreparator implements ActionPreparator { } } if (option == null) { - throw new CommandException(commandErrorSt + - Ansi.ansi() - .a("undefined option ") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(param) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .newline() - .a("Try <command> --help' for more information.") - .toString(), + throw new CommandException(commandErrorSt + + "undefined option " + INTENSITY_BOLD + param + INTENSITY_NORMAL + "\n" + + "Try <command> --help' for more information.", "Undefined option: " + param); } Field field = options.get(option); @@ -124,8 +113,8 @@ public class DefaultActionPreparator implements ActionPreparator { value = it.next(); } if (value == null) { - throw new CommandException(commandErrorSt + - Ansi.ansi().a("missing value for option ").bold().a(param).boldOff().toString(), + throw new CommandException(commandErrorSt + + "missing value for option " + INTENSITY_BOLD + param + INTENSITY_NORMAL, "Missing value for option: " + param ); } @@ -144,7 +133,7 @@ public class DefaultActionPreparator implements ActionPreparator { processOptions = false; if (argIndex >= orderedArguments.size()) { throw new CommandException(commandErrorSt + - Ansi.ansi().a("too many arguments specified").toString(), + "too many arguments specified", "Too many arguments specified" ); } @@ -169,7 +158,7 @@ public class DefaultActionPreparator implements ActionPreparator { for (Option option : options.keySet()) { if (option.required() && optionValues.get(option) == null) { throw new CommandException(commandErrorSt + - Ansi.ansi().a("option ").bold().a(option.name()).boldOff().a(" is required").toString(), + "option " + INTENSITY_BOLD + option.name() + INTENSITY_NORMAL + " is required", "Option " + option.name() + " is required" ); } @@ -177,7 +166,7 @@ public class DefaultActionPreparator implements ActionPreparator { for (Argument argument : orderedArguments) { if (argument.required() && argumentValues.get(argument) == null) { throw new CommandException(commandErrorSt + - Ansi.ansi().a("argument ").bold().a(argument.name()).boldOff().a(" is required").toString(), + "argument " + INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL + " is required", "Argument " + argument.name() + " is required" ); } @@ -191,8 +180,8 @@ public class DefaultActionPreparator implements ActionPreparator { value = convert(action, session, entry.getValue(), field.getGenericType()); } catch (Exception e) { throw new CommandException(commandErrorSt + - Ansi.ansi().a("unable to convert option ").bold().a(entry.getKey().name()).boldOff().a(" with value '").a(entry.getValue()).a("' to type ") - .a(new GenericType(field.getGenericType()).toString()).toString(), + "unable to convert option " + INTENSITY_BOLD + entry.getKey().name() + INTENSITY_NORMAL + " with value '" + + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), "Unable to convert option " + entry.getKey().name() + " with value '" + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), e @@ -208,9 +197,8 @@ public class DefaultActionPreparator implements ActionPreparator { value = convert(action, session, entry.getValue(), field.getGenericType()); } catch (Exception e) { throw new CommandException(commandErrorSt + - Ansi.ansi() - .a("unable to convert argument ").bold().a(entry.getKey().name()).boldOff().a(" with value '").a(entry.getValue()) - .a("' to type ").a(new GenericType(field.getGenericType()).toString()).toString(), + "unable to convert argument " + INTENSITY_BOLD + entry.getKey().name() + INTENSITY_NORMAL + " with value '" + + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), "Unable to convert argument " + entry.getKey().name() + " with value '" + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), e http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/karaf/shell/commands/meta/ActionMetaData.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/commands/meta/ActionMetaData.java b/shell/console/src/main/java/org/apache/karaf/shell/commands/meta/ActionMetaData.java index ae094ad..9fd5189 100644 --- a/shell/console/src/main/java/org/apache/karaf/shell/commands/meta/ActionMetaData.java +++ b/shell/console/src/main/java/org/apache/karaf/shell/commands/meta/ActionMetaData.java @@ -18,6 +18,9 @@ */ package org.apache.karaf.shell.commands.meta; +import static org.apache.karaf.shell.util.SimpleAnsi.INTENSITY_BOLD; +import static org.apache.karaf.shell.util.SimpleAnsi.INTENSITY_NORMAL; + import java.io.PrintStream; import java.lang.reflect.Field; import java.util.ArrayList; @@ -36,7 +39,7 @@ import org.apache.karaf.shell.commands.Option; import org.apache.karaf.shell.console.Completer; import org.apache.karaf.shell.util.IndentFormatter; import org.apache.karaf.shell.util.ShellUtil; -import org.fusesource.jansi.Ansi; +import org.apache.karaf.shell.util.SimpleAnsi; public class ActionMetaData { @@ -93,13 +96,13 @@ public class ActionMetaData { Set<Option> optionsSet = new HashSet<Option>(options.keySet()); optionsSet.add(HelpOption.HELP); if (command != null && (command.description() != null || command.name() != null)) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DESCRIPTION").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "DESCRIPTION" + INTENSITY_NORMAL); out.print(" "); if (command.name() != null) { if (globalScope) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + command.name() + INTENSITY_NORMAL); } else { - out.println(Ansi.ansi().a(command.scope()).a(":").a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET)); + out.println(command.scope() + ":" + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL); } out.println(); } @@ -129,15 +132,15 @@ public class ActionMetaData { } } - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("SYNTAX").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "SYNTAX" + INTENSITY_NORMAL); out.print(" "); out.println(syntax.toString()); out.println(); if (arguments.size() > 0) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("ARGUMENTS").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "ARGUMENTS" + INTENSITY_NORMAL); for (Argument argument : argumentsSet) { out.print(" "); - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(argument.name()).a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL); IndentFormatter.printFormatted(" ", argument.description(), termWidth, out); if (!argument.required()) { if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) { @@ -156,14 +159,14 @@ public class ActionMetaData { out.println(); } if (options.size() > 0) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("OPTIONS").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "OPTIONS" + INTENSITY_NORMAL); for (Option option : optionsSet) { String opt = option.name(); for (String alias : option.aliases()) { opt += ", " + alias; } out.print(" "); - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(opt).a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + opt + INTENSITY_NORMAL); IndentFormatter.printFormatted(" ", option.description(), termWidth, out); if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) { if (Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) { @@ -180,7 +183,7 @@ public class ActionMetaData { out.println(); } if (command.detailedDescription().length() > 0) { - out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DETAILS").a(Ansi.Attribute.RESET)); + out.println(INTENSITY_BOLD + "DETAILS" + INTENSITY_NORMAL); String desc = getDetailedDescription(); IndentFormatter.printFormatted(" ", desc, termWidth, out); } http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java b/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java index e6fe1e5..dbb3034 100644 --- a/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java +++ b/shell/console/src/main/java/org/apache/karaf/shell/console/completer/FileCompleter.java @@ -17,23 +17,131 @@ */ package org.apache.karaf.shell.console.completer; -import jline.console.completer.FileNameCompleter; +import java.io.File; +import java.util.List; + import org.apache.felix.service.command.CommandSession; import org.apache.karaf.shell.console.Completer; -import java.util.List; - /** - * A completer for file names + * A file name completer takes the buffer and issues a list of + * potential completions. + * <p/> + * This completer tries to behave as similar as possible to + * <i>bash</i>'s file name completion (using GNU readline) + * with the following exceptions: + * <p/> + * <ul> + * <li>Candidates that are directories will end with "/"</li> + * <li>Wildcard regular expressions are not evaluated or replaced</li> + * <li>The "~" character can be used to represent the user's home, + * but it cannot complete to other users' homes, since java does + * not provide any way of determining that easily</li> + * </ul> + * + * @author <a href="mailto:[email protected]">Marc Prud'hommeaux</a> + * @author <a href="mailto:[email protected]">Jason Dillon</a> + * @since 2.3 */ -public class FileCompleter implements Completer { - private FileNameCompleter completor = new FileNameCompleter(); +public class FileCompleter implements Completer +{ + private static String OS = System.getProperty("os.name").toLowerCase(); + + // TODO: Handle files with spaces in them + private static final boolean OS_IS_WINDOWS = isWindows(); + public FileCompleter(CommandSession session) { } - @SuppressWarnings({ "rawtypes", "unchecked" }) - public int complete(String buffer, int cursor, List candidates) { - return completor.complete(buffer, cursor, candidates); + public static boolean isWindows() { + return (OS.indexOf("win") >= 0); + + } + + public int complete(String buffer, final int cursor, final List<String> candidates) { + // buffer can be null + if (candidates == null) { + return 0; + } + + if (buffer == null) { + buffer = ""; + } + + if (OS_IS_WINDOWS) { + buffer = buffer.replace('/', '\\'); + } + + String translated = buffer; + + File homeDir = getUserHome(); + + // Special character: ~ maps to the user's home directory + if (translated.startsWith("~" + separator())) { + translated = homeDir.getPath() + translated.substring(1); + } + else if (translated.startsWith("~")) { + translated = homeDir.getParentFile().getAbsolutePath(); + } + else if (!(translated.startsWith(separator()))) { + String cwd = getUserDir().getAbsolutePath(); + translated = cwd + separator() + translated; + } + + File file = new File(translated); + final File dir; + + if (translated.endsWith(separator())) { + dir = file; + } + else { + dir = file.getParentFile(); + } + + File[] entries = dir == null ? new File[0] : dir.listFiles(); + + return matchFiles(buffer, translated, entries, candidates); + } + + protected String separator() { + return File.separator; + } + + protected File getUserHome() { + return new File(System.getProperty("user.home")); + } + + protected File getUserDir() { + return new File("."); + } + + protected int matchFiles(final String buffer, final String translated, final File[] files, final List<String> candidates) { + if (files == null) { + return -1; + } + + int matches = 0; + + // first pass: just count the matches + for (File file : files) { + if (file.getAbsolutePath().startsWith(translated)) { + matches++; + } + } + for (File file : files) { + if (file.getAbsolutePath().startsWith(translated)) { + CharSequence name = file.getName() + (matches == 1 && file.isDirectory() ? separator() : " "); + candidates.add(render(file, name).toString()); + } + } + + final int index = buffer.lastIndexOf(separator()); + + return index + separator().length(); + } + + protected CharSequence render(final File file, final CharSequence name) { + return name; } } http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/karaf/shell/util/CommandSessionUtil.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/util/CommandSessionUtil.java b/shell/console/src/main/java/org/apache/karaf/shell/util/CommandSessionUtil.java new file mode 100644 index 0000000..cd3d5c6 --- /dev/null +++ b/shell/console/src/main/java/org/apache/karaf/shell/util/CommandSessionUtil.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.karaf.shell.util; + +import org.apache.felix.service.command.CommandSession; + +public class CommandSessionUtil { + public static int getWidth(CommandSession session) { + Object cols = session.get("#COLUMNS"); + return (cols != null && cols instanceof Integer) ? (Integer)cols : 80; + } +} http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java b/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java index 3f3bf76..afa5828 100644 --- a/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java +++ b/shell/console/src/main/java/org/apache/karaf/shell/util/ShellUtil.java @@ -18,6 +18,11 @@ */ package org.apache.karaf.shell.util; +import static org.apache.karaf.shell.util.SimpleAnsi.COLOR_DEFAULT; +import static org.apache.karaf.shell.util.SimpleAnsi.COLOR_RED; +import static org.apache.karaf.shell.util.SimpleAnsi.INTENSITY_BOLD; +import static org.apache.karaf.shell.util.SimpleAnsi.INTENSITY_NORMAL; + import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -27,11 +32,10 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URL; -import jline.console.ConsoleReader; + import org.apache.felix.service.command.CommandSession; import org.apache.karaf.shell.commands.CommandException; import org.apache.karaf.shell.console.SessionProperties; -import org.fusesource.jansi.Ansi; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; @@ -125,28 +129,6 @@ public class ShellUtil { return level <= sbsl; } - /** - * Ask the user to confirm the access to a system bundle - * - * @param bundleId - * @param session - * @return true if the user confirm - * @throws IOException - */ - public static boolean accessToSystemBundleIsAllowed(long bundleId, CommandSession session) throws IOException { - for (; ; ) { - ConsoleReader reader = (ConsoleReader) session.get(".jline.reader"); - String msg = "You are about to access system bundle " + bundleId + ". Do you wish to continue (yes/no): "; - String str = reader.readLine(msg); - if ("yes".equalsIgnoreCase(str)) { - return true; - } - if ("no".equalsIgnoreCase(str)) { - return false; - } - } - } - public static String loadClassPathResource(Class<?> clazz, String path) { InputStream is = clazz.getResourceAsStream(path); if (is == null) { @@ -203,24 +185,20 @@ public class ShellUtil { if (t instanceof CommandException) { session.getConsole().println(((CommandException) t).getNiceHelp()); } else if (isCommandNotFound) { - String str = Ansi.ansi() - .fg(Ansi.Color.RED) - .a("Command not found: ") - .a(Ansi.Attribute.INTENSITY_BOLD) - .a(t.getClass().getMethod("getCommand").invoke(t)) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF) - .fg(Ansi.Color.DEFAULT).toString(); + String str = COLOR_RED + "Command not found: " + + INTENSITY_BOLD + t.getClass().getMethod("getCommand").invoke(t) + INTENSITY_NORMAL + + COLOR_DEFAULT; session.getConsole().println(str); } if (getBoolean(session, SessionProperties.PRINT_STACK_TRACES)) { - session.getConsole().print(Ansi.ansi().fg(Ansi.Color.RED).toString()); + session.getConsole().print(COLOR_RED); t.printStackTrace(session.getConsole()); - session.getConsole().print(Ansi.ansi().fg(Ansi.Color.DEFAULT).toString()); + session.getConsole().print(COLOR_DEFAULT); } else if (!(t instanceof CommandException) && !isCommandNotFound) { - session.getConsole().print(Ansi.ansi().fg(Ansi.Color.RED).toString()); + session.getConsole().print(COLOR_RED); session.getConsole().println("Error executing command: " + (t.getMessage() != null ? t.getMessage() : t.getClass().getName())); - session.getConsole().print(Ansi.ansi().fg(Ansi.Color.DEFAULT).toString()); + session.getConsole().print(COLOR_DEFAULT); } } catch (Exception ignore) { // ignore http://git-wip-us.apache.org/repos/asf/karaf/blob/4e47afae/shell/console/src/main/java/org/apache/karaf/shell/util/SimpleAnsi.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/util/SimpleAnsi.java b/shell/console/src/main/java/org/apache/karaf/shell/util/SimpleAnsi.java new file mode 100644 index 0000000..b4dc23b --- /dev/null +++ b/shell/console/src/main/java/org/apache/karaf/shell/util/SimpleAnsi.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.karaf.shell.util; + +import org.fusesource.jansi.Ansi; +import org.fusesource.jansi.Ansi.Color; + +public class SimpleAnsi { + public static String COLOR_RED = Ansi.ansi().fg(Color.RED).toString(); + public static String COLOR_DEFAULT = Ansi.ansi().fg(Color.DEFAULT).toString(); + + public static String INTENSITY_BOLD = Ansi.ansi().bold().toString(); + public static String INTENSITY_NORMAL = Ansi.ansi().boldOff().toString(); +}
