Java Code Examples for org.aesh.readline.terminal.formatting.TerminalString

The following examples show how to use org.aesh.readline.terminal.formatting.TerminalString. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: aesh-readline   Source File: CompletionHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Display all possible completions
 *
 * @param completions all completion items
 */
private void displayCompletions(List<TerminalString> completions, Buffer buffer,
                                InputProcessor inputProcessor) {
    completions.sort(new CaseInsensitiveComparator());

    //if the buffer is longer than one line, we need to move the cursor down the number of lines
    //before we continue
    if(inputProcessor.buffer().buffer().length() > inputProcessor.buffer().size().getWidth()) {
        int numRows = inputProcessor.buffer().buffer().length() / inputProcessor.buffer().size().getWidth();
        int cursorRow = inputProcessor.buffer().buffer().cursor() / inputProcessor.buffer().size().getWidth();
        for(; cursorRow < numRows; cursorRow++)
            inputProcessor.buffer().writeOut(new int[] {27, '[', 'B' });
    }
    //finally move to a new line
    inputProcessor.buffer().writeOut(Config.CR);
    //then we print out the completions
    inputProcessor.buffer().writeOut(Parser.formatDisplayListTerminalString(completions,
            inputProcessor.buffer().size().getHeight(), inputProcessor.buffer().size().getWidth()));
    //then on the next line we write the line again
    inputProcessor.buffer().drawLineForceDisplay();
}
 
Example 2
Source Project: aesh-readline   Source File: CompletionHandler.java    License: Apache License 2.0 6 votes vote down vote up
public int compare(TerminalString s1, TerminalString s2) {
    int n1 = s1.getCharacters().length();
    int n2 = s2.getCharacters().length();
    int min = Math.min(n1, n2);
    for (int i = 0; i < min; i++) {
        char c1 = s1.getCharacters().charAt(i);
        char c2 = s2.getCharacters().charAt(i);
        if (c1 != c2) {
            c1 = Character.toUpperCase(c1);
            c2 = Character.toUpperCase(c2);
            if (c1 != c2) {
                c1 = Character.toLowerCase(c1);
                c2 = Character.toLowerCase(c2);
                if (c1 != c2) {
                    // No overflow because of numeric promotion
                    return c1 - c2;
                }
            }
        }
    }
    return n1 - n2;
}
 
Example 3
Source Project: aesh-readline   Source File: CompleteOperationImpl.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source Project: aesh-readline   Source File: CompleteOperationImpl.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 5
Source Project: aesh-readline   Source File: Parser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Decides if it's possible to format provided Strings into calculated number of columns while the output will not exceed
 * terminal width
 *
 * @param displayList
 * @param numRows
 * @param terminalWidth
 * @return true if it's possible to format strings to columns and false otherwise.
 */
private static boolean canDisplayColumns(List<TerminalString> displayList, int numRows, int terminalWidth) {

    int numColumns = displayList.size() / numRows;
    if (displayList.size() % numRows > 0) {
        numColumns++;
    }

    int[] columnSizes = calculateColumnSizes(displayList, numColumns, numRows, terminalWidth);

    int totalSize = 0;
    for (int columnSize : columnSizes) {
        totalSize += columnSize;
    }
    return totalSize <= terminalWidth;
}
 
Example 6
private List<String> complete(CommandContext ctx, String cmd, Boolean separator) {
    Completion<AeshCompleteOperation> completer
            = (Completion<AeshCompleteOperation>) ctx.getDefaultCommandCompleter();
    AeshCompleteOperation op = new AeshCompleteOperation(cmd, cmd.length());
    completer.complete(op);
    if (separator != null) {
        assertEquals(op.hasAppendSeparator(), separator);
    }
    List<String> candidates = new ArrayList<>();
    for (TerminalString ts : op.getCompletionCandidates()) {
        candidates.add(ts.getCharacters());
    }
    // aesh-readline does sort the candidates prior to display.
    Collections.sort(candidates);
    return candidates;
}
 
Example 7
private List<String> complete(CommandContext ctx, String cmd, Boolean separator, int offset) {
    Completion<AeshCompleteOperation> completer
            = (Completion<AeshCompleteOperation>) ctx.getDefaultCommandCompleter();
    AeshCompleteOperation op = new AeshCompleteOperation(cmd, cmd.length());
    completer.complete(op);
    if (separator != null) {
        assertEquals(op.hasAppendSeparator(), separator);
    }
    if (offset > 0) {
        assertEquals(op.getOffset(), offset);
    }
    List<String> candidates = new ArrayList<>();
    for (TerminalString ts : op.getCompletionCandidates()) {
        candidates.add(ts.getCharacters());
    }
    return candidates;
}
 
Example 8
Source Project: aesh-readline   Source File: CompletionHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void processMultipleCompletions(List<C> possibleCompletions, Buffer buffer, InputProcessor inputProcessor) {
    String startsWith = "";

    if(!possibleCompletions.get(0).isIgnoreStartsWith())
        startsWith = Parser.findStartsWithOperation(possibleCompletions);

    if(startsWith.length() > 0 ) {
        if(startsWith.contains(" ") && !possibleCompletions.get(0).doIgnoreNonEscapedSpace())
            displayCompletion(new TerminalString(Parser.switchSpacesToEscapedSpacesInWord(startsWith), true),
                    buffer, inputProcessor,
                    false, possibleCompletions.get(0).getSeparator());
        else
            displayCompletion(new TerminalString(startsWith, true), buffer, inputProcessor,
                    false, possibleCompletions.get(0).getSeparator());
    }
    // display all
    // check size
    else {
        List<TerminalString> completions = new ArrayList<>();
        for(int i=0; i < possibleCompletions.size(); i++)
            completions.addAll(possibleCompletions.get(i).getCompletionCandidates());

        if(completions.size() > 100) {
            if(status == CompletionStatus.ASKING_FOR_COMPLETIONS) {
                displayCompletions(completions, buffer, inputProcessor);
                status = CompletionStatus.COMPLETE;
            }
            else {
                status = CompletionStatus.ASKING_FOR_COMPLETIONS;
                inputProcessor.buffer().writeOut(Config.CR);
                inputProcessor.buffer().writeOut("Display all " + completions.size() + " possibilities? (y or n)");
            }
        }
        // display all
        else {
            displayCompletions(completions, buffer, inputProcessor);
        }
    }
}
 
Example 9
Source Project: aesh-readline   Source File: CompletionHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 10
Source Project: aesh-readline   Source File: Prompt.java    License: Apache License 2.0 5 votes vote down vote up
public Prompt(TerminalString terminalString) {
    if(terminalString != null) {
        ansiString = Parser.toCodePoints(terminalString.toString());
        this.prompt = Parser.toCodePoints(terminalString.getCharacters());
    }
    else
        this.prompt = new int[]{};
}
 
Example 11
Source Project: aesh-readline   Source File: Parser.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 12
Source Project: aesh-readline   Source File: Parser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 13
Source Project: aesh-readline   Source File: Parser.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean startsWithTerminalString(String criteria, List<TerminalString> completionList) {
    for (TerminalString completion : completionList)
        if (!completion.getCharacters().startsWith(criteria))
            return false;

    return true;
}
 
Example 14
Source Project: aesh-readline   Source File: ParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFormatDisplayCompactListTerminalString() {
    TerminalString terminalLong = new TerminalString("This string is too long to terminal width");

    TerminalString terminalShort1 = new TerminalString("str1");
    TerminalString terminalShort2 = new TerminalString("str2");
    TerminalString terminalShort3 = new TerminalString("str3");
    TerminalString terminalShort4 = new TerminalString("str4");

    TerminalString terminalLonger1 = new TerminalString("longer1");
    TerminalString terminalLonger2 = new TerminalString("longer2");
    TerminalString terminalLonger3 = new TerminalString("longer3");

    assertEquals(
        terminalLong.toString() + Config.getLineSeparator(),
        Parser.formatDisplayCompactListTerminalString(Collections.singletonList(terminalLong), 10));

    assertEquals(
        terminalShort1.toString() + "  " + terminalShort2.toString() + Config.getLineSeparator(),
        Parser.formatDisplayCompactListTerminalString(Arrays.asList(terminalShort1, terminalShort2), 20));

    assertEquals(
        terminalShort1.toString() + Config.getLineSeparator() + terminalShort2.toString() + Config.getLineSeparator(),
        Parser.formatDisplayCompactListTerminalString(Arrays.asList(terminalShort1, terminalShort2), 10));

    assertEquals(
        terminalShort1.toString() + "  " + terminalShort3.toString() + Config.getLineSeparator() + terminalShort2.toString() + Config.getLineSeparator(),
        Parser.formatDisplayCompactListTerminalString(Arrays.asList(terminalShort1, terminalShort2, terminalShort3), 15));

    assertEquals(
        terminalShort1.toString() + "  " + terminalShort3.toString() + Config.getLineSeparator() +
            terminalShort2.toString() + "  " + terminalShort4.toString() + Config.getLineSeparator(),
        Parser.formatDisplayCompactListTerminalString(Arrays.asList(terminalShort1, terminalShort2, terminalShort3, terminalShort4), 15));

    assertEquals(
        terminalLonger1.toString() + "  " + terminalShort1.toString() + Config.getLineSeparator() +
            terminalLonger2.toString() + Config.getLineSeparator() + terminalLonger3.toString() + Config.getLineSeparator(),
        Parser.formatDisplayCompactListTerminalString(
            Arrays.asList(terminalLonger1, terminalLonger2, terminalLonger3, terminalShort1), 15));
}
 
Example 15
@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;
}
 
Example 16
@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();
}
 
Example 17
Source Project: galleon   Source File: PmCompleterInvocation.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<TerminalString> getCompleterValues() {
    return delegate.getCompleterValues();
}
 
Example 18
Source Project: galleon   Source File: PmCompleterInvocation.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void setCompleterValuesTerminalString(List<TerminalString> terminalStrings) {
    delegate.setCompleterValuesTerminalString(terminalStrings);
}
 
Example 19
Source Project: galleon   Source File: PmCompleterInvocation.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void addCompleterValueTerminalString(TerminalString terminalString) {
    delegate.addCompleterValueTerminalString(terminalString);
}
 
Example 20
Source Project: aesh-readline   Source File: CompleteOperationImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<TerminalString> getCompletionCandidates() {
    return completionCandidates;
}
 
Example 21
Source Project: aesh-readline   Source File: CompleteOperationImpl.java    License: Apache License 2.0 4 votes vote down vote up
public void setCompletionCandidatesTerminalString(List<TerminalString> completionCandidates) {
    this.completionCandidates = completionCandidates;
}
 
Example 22
Source Project: aesh-readline   Source File: CompleteOperationImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void addCompletionCandidate(TerminalString completionCandidate) {
    this.completionCandidates.add(completionCandidate);
}
 
Example 23
Source Project: aesh-readline   Source File: CompleteOperationImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void addCompletionCandidatesTerminalString(List<TerminalString> completionCandidates) {
    this.completionCandidates.addAll(completionCandidates);
}
 
Example 24
Source Project: aesh-readline   Source File: CompleteOperationImpl.java    License: Apache License 2.0 4 votes vote down vote up
private void addStringCandidate(String completionCandidate) {
    this.completionCandidates.add(new TerminalString(completionCandidate, true));
}
 
Example 25
Source Project: aesh-readline   Source File: Parser.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Format completions so that they look similar to GNU Readline
 *
 * @param displayList to format
 * @param termHeight max height
 * @param termWidth max width
 * @return formatted string to be outputted
 */
public static String formatDisplayListTerminalString(List<TerminalString> displayList, int termHeight, int termWidth) {
    if (displayList == null || displayList.size() < 1)
        return "";
    // make sure that termWidth is > 0
    if (termWidth < 1)
        termWidth = 80; // setting it to default

    int maxLength = 0;
    for (TerminalString completion : displayList)
        if (completion.getCharacters().length() > maxLength)
            maxLength = completion.getCharacters().length();

    maxLength = maxLength + 2; // adding two spaces for better readability
    int numColumns = termWidth / maxLength;
    if (numColumns > displayList.size()) // we dont need more columns than items
        numColumns = displayList.size();
    if (numColumns < 1)
        numColumns = 1;
    int numRows = displayList.size() / numColumns;

    // add a row if we cant display all the items
    if (numRows * numColumns < displayList.size())
        numRows++;

    StringBuilder completionOutput = new StringBuilder();
    if (numRows > 1) {
        // create the completion listing
        for (int i = 0; i < numRows; i++) {
            for (int c = 0; c < numColumns; c++) {
                int fetch = i + (c * numRows);
                if (fetch < displayList.size()) {
                    if (c == numColumns - 1) {  // No need to pad the right most column
                        completionOutput.append(displayList.get(i + (c * numRows)).toString());
                    } else {
                        completionOutput.append(padRight(maxLength
                                + displayList.get(i + (c * numRows)).getANSILength(),
                                displayList.get(i + (c * numRows)).toString()));
                    }
                } else {
                    break;
                }
            }
            completionOutput.append(Config.getLineSeparator());
        }
    }
    else {
        for (TerminalString ts : displayList) {
            completionOutput.append(ts.toString()).append("  ");
        }
        completionOutput.append(Config.getLineSeparator());
    }

    return completionOutput.toString();
}
 
Example 26
Source Project: aesh-readline   Source File: Parser.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Format output to columns with flexible sizes and no redundant space between them
 *
 * @param displayList to format
 * @param termWidth max width
 * @return formatted string to be outputted
 */
public static String formatDisplayCompactListTerminalString(List<TerminalString> displayList, int termWidth) {
    if (displayList == null || displayList.size() < 1)
        return "";
    // make sure that termWidth is > 0
    if (termWidth < 1)
        termWidth = 80; // setting it to default

    int numRows = 1;

    // increase numRows and check in loop if it's possible to format strings to columns
    while (!canDisplayColumns(displayList, numRows, termWidth) && numRows < displayList.size()) {
        numRows++;
    }

    int numColumns = displayList.size() / numRows;
    if (displayList.size() % numRows > 0) {
        numColumns++;
    }

    int[] columnsSizes = calculateColumnSizes(displayList, numColumns, numRows, termWidth);

    StringBuilder stringOutput = new StringBuilder();

    for (int i = 0; i < numRows; i++) {
        for (int c = 0; c < numColumns; c++) {
            int fetch = i + (c * numRows);
            int nextFetch = i + ((c + 1) * numRows);

            if (fetch < displayList.size()) {
                // don't need to format last column of row = nextFetch doesn't exit
                if (nextFetch < displayList.size()) {
                    stringOutput.append(padRight(columnsSizes[c] + displayList.get(i + (c * numRows)).getANSILength(),
                            displayList.get(i + (c * numRows)).toString()));
                }
                else {
                    stringOutput.append(displayList.get(i + (c * numRows)).toString());
                }
            }
            else {
                break;
            }
        }
        stringOutput.append(Config.getLineSeparator());
    }

    return stringOutput.toString();
}
 
Example 27
Source Project: aesh-readline   Source File: Parser.java    License: Apache License 2.0 4 votes vote down vote up
public static void switchEscapedSpacesToSpacesInTerminalStringList(List<TerminalString> list) {
    for (TerminalString ts : list)
        ts.setCharacters(switchEscapedSpacesToSpacesInWord(ts.getCharacters()));
}
 
Example 28
@Override
public List<TerminalString> getCompleterValues() {
    return delegate.getCompleterValues();
}
 
Example 29
@Override
public void setCompleterValuesTerminalString(List<TerminalString> terminalStrings) {
    delegate.setCompleterValuesTerminalString(terminalStrings);
}
 
Example 30
@Override
public void addCompleterValueTerminalString(TerminalString terminalString) {
    delegate.addCompleterValueTerminalString(terminalString);
}