Skip to content

Commit

Permalink
jline-console: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 6, 2020
1 parent 1484c4d commit 786dcd8
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 223 deletions.
6 changes: 3 additions & 3 deletions console/src/main/java/org/jline/console/ArgDesc.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import org.jline.utils.AttributedString;

public class ArgDesc {
private String name;
private List<AttributedString> description = new ArrayList<AttributedString>();
private final String name;
private final List<AttributedString> description;

public ArgDesc(String name) {
this(name, new ArrayList<AttributedString>());
this(name, new ArrayList<>());
}

public ArgDesc(String name, List<AttributedString> description) {
Expand Down
6 changes: 1 addition & 5 deletions console/src/main/java/org/jline/console/CmdDesc.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ public List<ArgDesc> getArgsDesc() {
public boolean optionWithValue(String option) {
for (String key: optsDesc.keySet()) {
if (key.matches("(^|.*\\s)" + option + "($|=.*|\\s.*)")) {
if (key.contains("=")) {
return true;
} else {
return false;
}
return key.contains("=");
}
}
return false;
Expand Down
13 changes: 7 additions & 6 deletions console/src/main/java/org/jline/console/CmdLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public enum DescriptionType {
/**
* The part of the line from beginning till cursor ends to the closing parenthesis.
*/
SYNTAX};
private String line;
private String head;
private String tail;
private List<String> args;
private DescriptionType descType;
SYNTAX}

private final String line;
private final String head;
private final String tail;
private final List<String> args;
private final DescriptionType descType;

/**
* CmdLine class constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ default Object invoke(CommandSession session, String command, Object... args) th
throw new IllegalStateException("CommandRegistry method invoke(session, command, ... args) is not implemented!");
}

static class CommandSession {
class CommandSession {
private final Terminal terminal;
private final InputStream in;
private final PrintStream out;
Expand Down
16 changes: 8 additions & 8 deletions console/src/main/java/org/jline/console/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ static String plainCommand(String command) {
* @param file file where object should be written
* @param object object to persist
*/
public void persist(Path file, Object object);
void persist(Path file, Object object);

/**
* Read object from file
* @param file file from where object should be read
* @return object
* @throws IOException in case of error
*/
public Object slurp(Path file) throws IOException;
Object slurp(Path file) throws IOException;

/**
* Read console option value
Expand All @@ -134,7 +134,7 @@ static String plainCommand(String command) {
* @param defval default value
* @return option value
*/
public <T>T consoleOption(String option, T defval);
<T>T consoleOption(String option, T defval);

/**
* Executes command line that does not contain known command by the system registry.
Expand Down Expand Up @@ -235,7 +235,7 @@ default Object execute(File script) throws Exception {
*/
boolean isExecuting();

static class ExecutionResult {
class ExecutionResult {
final int status;
final Object result;

Expand All @@ -253,10 +253,10 @@ public Object result() {
}
}

static class WidgetCreator implements Widget {
private ConsoleEngine consoleEngine;
private Object function;
private String name;
class WidgetCreator implements Widget {
private final ConsoleEngine consoleEngine;
private final Object function;
private final String name;

public WidgetCreator(ConsoleEngine consoleEngine, String function) {
this.consoleEngine = consoleEngine;
Expand Down
48 changes: 24 additions & 24 deletions console/src/main/java/org/jline/console/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,103 +29,103 @@ public interface Printer {
* Applies: TABLE<br>
* Ignore columnsOut configuration.
*/
final static String ALL = "all";
String ALL = "all";
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* Display given columns on table.
*/
final static String COLUMNS = "columns";
String COLUMNS = "columns";
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* Exclude given columns on table.
*/
final static String EXCLUDE = "exclude";
String EXCLUDE = "exclude";
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* Include given columns on table.
*/
final static String INCLUDE = "include";
String INCLUDE = "include";
/**
* Value: Integer<br>
* Applies: MAP<br>
* Indention size.
*/
final static String INDENTION = "indention";
String INDENTION = "indention";
/**
* Value: Integer<br>
* Applies: MAP and TABLE<br>
* Maximum column width.
*/
final static String MAX_COLUMN_WIDTH = "maxColumnWidth";
String MAX_COLUMN_WIDTH = "maxColumnWidth";
/**
* Value: Integer<br>
* Applies: MAP<br>
* Maximum depth objects are resolved.
*/
final static String MAX_DEPTH = "maxDepth";
String MAX_DEPTH = "maxDepth";
/**
* Value: Integer<br>
* Applies: MAP and TABLE<br>
* Maximum number of lines to display.
*/
final static String MAXROWS = "maxrows";
String MAXROWS = "maxrows";
/**
* Value: Boolean<br>
* Applies: TABLE<br>
* Display one row data on table.
*/
final static String ONE_ROW_TABLE = "oneRowTable";
String ONE_ROW_TABLE = "oneRowTable";
/**
* Value: Boolean<br>
* Applies: TABLE<br>
* Display table row numbers.
*/
final static String ROWNUM = "rownum";
String ROWNUM = "rownum";
/**
* Value: Boolean<br>
* Applies: TABLE<br>
* Truncate table column names: property.field to field.
*/
final static String SHORT_NAMES = "shortNames";
String SHORT_NAMES = "shortNames";
/**
* Value: Boolean<br>
* Applies: MAP and TABLE<br>
* Ignore all options defined in PRNT_OPTIONS.
*/
final static String SKIP_DEFAULT_OPTIONS = "skipDefaultOptions";
String SKIP_DEFAULT_OPTIONS = "skipDefaultOptions";
/**
* Value: Boolean<br>
* Applies: TABLE<br>
* Display object structures and lists on table.
*/
final static String STRUCT_ON_TABLE = "structsOnTable";
String STRUCT_ON_TABLE = "structsOnTable";
/**
* Value: String<br>
* Use nanorc STYLE<br>
*/
final static String STYLE = "style";
String STYLE = "style";
/**
* Value: Boolean<br>
* Applies: MAP and TABLE<br>
* Use object's toString() method to get print value
* DEFAULT: object's fields are put to property map before printing
*/
final static String TO_STRING = "toString";
String TO_STRING = "toString";
/**
* Value: String<br>
* Applies: MAP and TABLE<br>
* Nanorc syntax style used to highlight values.
*/
final static String VALUE_STYLE = "valueStyle";
String VALUE_STYLE = "valueStyle";
/**
* Value: Integer<br>
* Applies: MAP and TABLE<br>
* Display width (default terminal width).
*/
final static String WIDTH = "width";
String WIDTH = "width";
//
// 2) additional PRNT_OPTIONS
//
Expand All @@ -134,42 +134,42 @@ public interface Printer {
* Applies: TABLE<br>
* These map values will be added to the table before all the other keys.
*/
final static String COLUMNS_IN = "columnsIn";
String COLUMNS_IN = "columnsIn";
/**
* Value: {@code List<String>}<br>
* Applies: TABLE<br>
* These map values will not be inserted to the table.
*/
final static String COLUMNS_OUT = "columnsOut";
String COLUMNS_OUT = "columnsOut";
/**
* Value: {@code Map<regex, function>}.<br>
* Applies: TABLE<br>
* If command result map key matches with regex the highlight function is applied
* to the corresponding map value. The regex = * is processed after all the other regexes and the highlight
* function will be applied to all map values that have not been already highlighted.
*/
final static String HIGHLIGHT_VALUE = "highlightValue";
String HIGHLIGHT_VALUE = "highlightValue";
/**
* Value: Double<br>
* Applies: MAP and TABLE<br>
* default value 0.8 i.e. if at least of 4 of the 5 results map keys match with reference key set the
* result will be printed out as a table.
*/
final static String MAP_SIMILARITY = "mapSimilarity";
String MAP_SIMILARITY = "mapSimilarity";
/**
* Value: {@code Map<class, function>}<br>
* Applies: MAP and TABLE<br>
* Overrides the ScriptEngine toMap() method.
*/
final static String OBJECT_TO_MAP = "objectToMap";
String OBJECT_TO_MAP = "objectToMap";
/**
* Value: {@code Map<class, function>}<br>
* Applies: MAP and TABLE<br>
* Overrides the ScriptEngine toString() method.
*/
final static String OBJECT_TO_STRING = "objectToString";
String OBJECT_TO_STRING = "objectToString";

final static List<String> BOOLEAN_KEYS = Arrays.asList(ALL, ONE_ROW_TABLE, ROWNUM, SHORT_NAMES, SKIP_DEFAULT_OPTIONS
List<String> BOOLEAN_KEYS = Arrays.asList(ALL, ONE_ROW_TABLE, ROWNUM, SHORT_NAMES, SKIP_DEFAULT_OPTIONS
, STRUCT_ON_TABLE, TO_STRING);

default void println(Object object) {
Expand Down
7 changes: 3 additions & 4 deletions console/src/main/java/org/jline/console/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Map;

import org.jline.builtins.ConsoleOptionGetter;
import org.jline.console.CmdLine;
import org.jline.reader.Completer;
import org.jline.terminal.Terminal;

Expand All @@ -36,7 +35,7 @@ public interface SystemRegistry extends CommandRegistry, ConsoleOptionGetter {
* @param command main command
* @param subcommandRegistry subcommand registry
*/
public void register(String command, CommandRegistry subcommandRegistry);
void register(String command, CommandRegistry subcommandRegistry);

/**
* Initialize consoleEngine environment by executing console script
Expand Down Expand Up @@ -141,8 +140,8 @@ static void remove() {
* Manage systemRegistry store
*/
class Registeries {
private static Registeries instance = new Registeries();
private Map<Long, SystemRegistry> systemRegisteries = new HashMap<>();
private static final Registeries instance = new Registeries();
private final Map<Long, SystemRegistry> systemRegisteries = new HashMap<>();

private Registeries () {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AbstractCommandRegistry() {}
public CmdDesc doHelpDesc(String command, List<String> info, CmdDesc cmdDesc) {
List<AttributedString> mainDesc = new ArrayList<>();
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(command.toString().toLowerCase()).append(" - ");
asb.append(command.toLowerCase()).append(" - ");
for (String s : info) {
if (asb.length() == 0) {
asb.append("\t");
Expand All @@ -57,7 +57,7 @@ public CmdDesc doHelpDesc(String command, List<String> info, CmdDesc cmdDesc) {
}

public <T extends Enum<T>> void registerCommands(Map<T,String> commandName, Map<T,CommandMethods> commandExecute) {
cmdRegistry = new EnumCmdRegistry<T>(commandName, commandExecute);
cmdRegistry = new EnumCmdRegistry<>(commandName, commandExecute);
}

public void registerCommands(Map<String,CommandMethods> commandExecute) {
Expand Down Expand Up @@ -127,10 +127,10 @@ private interface CmdRegistry {
}

private static class EnumCmdRegistry<T extends Enum<T>> implements CmdRegistry {
private Map<T,String> commandName;
private final Map<T,String> commandName;
private Map<String,T> nameCommand = new HashMap<>();
private Map<T,CommandMethods> commandExecute;
private Map<String,String> aliasCommand = new HashMap<>();
private final Map<T,CommandMethods> commandExecute;
private final Map<String,String> aliasCommand = new HashMap<>();

public EnumCmdRegistry(Map<T,String> commandName, Map<T,CommandMethods> commandExecute) {
this.commandName = commandName;
Expand All @@ -156,25 +156,22 @@ public Map<String, String> commandAliases() {
public <V extends Enum<V>> void rename(V command, String newName) {
if (nameCommand.containsKey(newName)) {
throw new IllegalArgumentException("Duplicate command name!");
} else if (!commandName.containsKey((T)command)) {
} else if (!commandName.containsKey(command)) {
throw new IllegalArgumentException("Command does not exists!");
}
commandName.put((T)command, newName);
doNameCommand();
}

public void alias(String alias, String command) {
if (!nameCommand.keySet().contains(command)) {
if (!nameCommand.containsKey(command)) {
throw new IllegalArgumentException("Command does not exists!");
}
aliasCommand.put(alias, command);
}

public boolean hasCommand(String name) {
if (nameCommand.containsKey(name) || aliasCommand.containsKey(name)) {
return true;
}
return false;
return nameCommand.containsKey(name) || aliasCommand.containsKey(name);
}

public SystemCompleter compileCompleters() {
Expand All @@ -187,7 +184,7 @@ public SystemCompleter compileCompleters() {
}

public T command(String name) {
T out = null;
T out;
if (!hasCommand(name)) {
throw new IllegalArgumentException("Command does not exists!");
}
Expand All @@ -209,8 +206,8 @@ public CommandMethods getCommandMethods(String command) {
}

private static class NameCmdRegistry implements CmdRegistry {
private Map<String,CommandMethods> commandExecute;
private Map<String,String> aliasCommand = new HashMap<>();
private final Map<String,CommandMethods> commandExecute;
private final Map<String,String> aliasCommand = new HashMap<>();

public NameCmdRegistry(Map<String,CommandMethods> commandExecute) {
this.commandExecute = commandExecute;
Expand All @@ -236,10 +233,7 @@ public void alias(String alias, String command) {
}

public boolean hasCommand(String name) {
if (commandExecute.containsKey(name) || aliasCommand.containsKey(name)) {
return true;
}
return false;
return commandExecute.containsKey(name) || aliasCommand.containsKey(name);
}

public SystemCompleter compileCompleters() {
Expand Down

0 comments on commit 786dcd8

Please sign in to comment.