private static boolean startsWithTerminalString(String criteria, List<TerminalString> completionList) { for (TerminalString completion : completionList) if (!completion.getCharacters().startsWith(criteria)) return false; return true; }
@Override public int compareTo(TerminalString terminalString) { return this.characters.compareTo(terminalString.getCharacters()); } }
private static int[] calculateColumnSizes(List<TerminalString> displayList, int numColumns, int numRows, int termWidth) { int[] columnSizes = new int[numColumns]; for (int i = 0; i < displayList.size(); i++) { int columnIndex = (i / numRows) % numColumns; int stringSize = displayList.get(i).getCharacters().length() + 2; if (columnSizes[columnIndex] < stringSize && stringSize <= termWidth) { columnSizes[columnIndex] = stringSize; } } return columnSizes; }
@Override public List<String> getFormattedCompletionCandidates() { List<String> fixedCandidates = new ArrayList<String>(completionCandidates.size()); for(TerminalString c : completionCandidates) { if(!ignoreOffset && offset < cursor) { int pos = cursor - offset; if(c.getCharacters().length() >= pos) fixedCandidates.add(c.getCharacters().substring(pos)); else fixedCandidates.add(""); } else { fixedCandidates.add(c.getCharacters()); } } return fixedCandidates; }
/** * Return the biggest common startsWith string * * @param completionList list to compare * @return biggest common startsWith string */ public static String findStartsWithTerminalString(List<TerminalString> completionList) { StringBuilder builder = new StringBuilder(); for (TerminalString completion : completionList) while (builder.length() < completion.getCharacters().length() && startsWithTerminalString(completion.getCharacters().substring(0, builder.length() + 1), completionList)) builder.append(completion.getCharacters().charAt(builder.length())); return builder.toString(); }
public void write(PrintStream out) { if(ignoreRendering) { out.print(characters); } else { out.print(ANSI.START); out.print(style.toString()); out.print(';'); this.color.write(out); out.print('m'); out.print(getCharacters()); } }
@Override public List<TerminalString> getFormattedCompletionCandidatesTerminalString() { List<TerminalString> fixedCandidates = new ArrayList<>(completionCandidates.size()); for(TerminalString c : completionCandidates) { if(!ignoreOffset && offset < cursor) { int pos = cursor - offset; if(c.getCharacters().length() >= pos) { c.setCharacters(c.getCharacters().substring(pos)); fixedCandidates.add(c); } else fixedCandidates.add(new TerminalString("", true)); } else { fixedCandidates.add(c); } } return fixedCandidates; }
public static void switchEscapedSpacesToSpacesInTerminalStringList(List<TerminalString> list) { for (TerminalString ts : list) ts.setCharacters(switchEscapedSpacesToSpacesInWord(ts.getCharacters())); }
@Override public String toString() { if(ignoreRendering) return characters; return ANSI.START + style.toString() + ';' + this.color.toString() + 'm' + getCharacters() + ANSI.RESET; }
@Override public int complete(CommandContext ctx, String buffer, int cursor, List<String> candidates) { AeshCompleteOperation op = new AeshCompleteOperation(buffer, cursor); cliCompleter.complete(ctx, op, this); if (!op.getCompletionCandidates().isEmpty()) { for (TerminalString ts : op.getCompletionCandidates()) { candidates.add(ts.getCharacters()); } return op.getOffset(); } return -1; }
public Prompt(TerminalString terminalString) { if(terminalString != null) { ansiString = Parser.toCodePoints(terminalString.toString()); this.prompt = Parser.toCodePoints(terminalString.getCharacters()); } else this.prompt = new int[]{}; }
/** * style, text color, background color */ public String toString(TerminalString prev) { if(ignoreRendering) return characters; if(equalsIgnoreCharacter(prev)) return characters; else { StringBuilder builder = new StringBuilder(); builder.append(ANSI.START) .append(style.getValueComparedToPrev(prev.getStyle())); if(!this.color.equals(prev.color)) { if(prev.getStyle().isInvert()) builder.append(';').append(this.color.toString()); else builder.append(';').append(this.color.toString(prev.color)); } builder.append('m').append(getCharacters()); return builder.toString(); } }
@Override public int complete(CommandContext ctx, String buffer, int cursor, List<String> candidates) { AeshCompleteOperation op = new AeshCompleteOperation(buffer, cursor); cliCompleter.complete(ctx, op, this); if (!op.getCompletionCandidates().isEmpty()) { for (TerminalString ts : op.getCompletionCandidates()) { candidates.add(ts.getCharacters()); } return op.getOffset(); } return -1; }
@Override public int complete(CommandContext ctx, String buffer, int cursor, List<String> candidates) { AeshCompleteOperation co = new AeshCompleteOperation(aeshCommands.getAeshContext(), buffer, cursor); complete(co); for (TerminalString ts : co.getCompletionCandidates()) { candidates.add(ts.getCharacters()); } if (co.getCompletionCandidates().isEmpty()) { return -1; } Collections.sort(candidates); return co.getOffset(); }
@Override public int complete(CommandContext ctx, String buffer, int cursor, List<String> candidates) { AeshCompleteOperation co = new AeshCompleteOperation(aeshCommands.getAeshContext(), buffer, cursor); complete(co); for (TerminalString ts : co.getCompletionCandidates()) { candidates.add(ts.getCharacters()); } if (co.getCompletionCandidates().isEmpty()) { return -1; } Collections.sort(candidates); return co.getOffset(); }
private void completeDataWithValues(CompleterInvocation completerData) { if(completerData.getCompleterValues().size() == 1 && completerData.getCompleterValues().get(0).containSpaces()) { String tmpData = Parser.switchSpacesToEscapedSpacesInWord( completerData.getCompleterValues().get(0).getCharacters()); completerData.clearCompleterValues(); completerData.addCompleterValue(tmpData); completerData.setAppendSpace(true); } } }
private void completeLegacyCommands(CommandContext ctx, DefaultCallbackHandler parsedCmd, AeshCompleteOperation co) { legacyCommandCompleter.complete(ctx, parsedCmd, co); // DMR Operations and command handler completion doesn't work well with whitespace separator, so must not be appended. // whitespace only appended at the end of a command name. String buffer = ctx.getArgumentsString() == null ? co.getBuffer() : ctx.getArgumentsString() + co.getBuffer(); if (co.getCompletionCandidates().size() == 1 && co.getCompletionCandidates().get(0).getCharacters().startsWith(buffer)) { co.doAppendSeparator(true); } else if (!co.getCompletionCandidates().isEmpty()) { co.doAppendSeparator(false); } } }
private void completeLegacyCommands(CommandContext ctx, DefaultCallbackHandler parsedCmd, AeshCompleteOperation co) { legacyCommandCompleter.complete(ctx, parsedCmd, co); // DMR Operations and command handler completion doesn't work well with whitespace separator, so must not be appended. // whitespace only appended at the end of a command name. String buffer = ctx.getArgumentsString() == null ? co.getBuffer() : ctx.getArgumentsString() + co.getBuffer(); if (co.getCompletionCandidates().size() == 1 && co.getCompletionCandidates().get(0).getCharacters().startsWith(buffer)) { co.doAppendSeparator(true); } else if (!co.getCompletionCandidates().isEmpty()) { co.doAppendSeparator(false); } } }
@Test public void testRemoveEscapedSpacesFromCompletionCandidates() { AeshCompleteOperation co = new AeshCompleteOperation(aeshContext, "ls foob", 6); co.addCompletionCandidate("foo\\ bar"); co.addCompletionCandidate("foo\\ bars"); co.setOffset(3); co.removeEscapedSpacesFromCompletionCandidates(); assertEquals("foo bar", co.getCompletionCandidates().get(0).getCharacters()); assertEquals("foo bars", co.getCompletionCandidates().get(1).getCharacters()); } }
/** * Display the completion string in the terminal. * If !completion.startsWith(buffer.getLine()) the completion will be added to the line, * else it will replace whats at the buffer line. * * @param completion partial completion * @param appendSpace if its an actual complete */ private void displayCompletion(TerminalString completion, Buffer buffer, InputProcessor inputProcessor, boolean appendSpace, char separator) { if(completion.getCharacters().startsWith(buffer.asString())) { ActionMapper.mapToAction("backward-kill-word").accept(inputProcessor); inputProcessor.buffer().writeString(completion.toString()); } else { inputProcessor.buffer().writeString(completion.toString()); } if(appendSpace) { inputProcessor.buffer().writeChar(separator); } }