Search in sources :

Example 1 with AttributedStyle

use of org.jline.utils.AttributedStyle in project samza by apache.

the class QueryResultLogView method drawStatusBar.

private void drawStatusBar(int rowsInBuffer) {
    terminal.puts(InfoCmp.Capability.save_cursor);
    terminal.puts(InfoCmp.Capability.cursor_address, height - 1, 0);
    AttributedStyle statusBarStyle = AttributedStyle.DEFAULT.background(AttributedStyle.WHITE).foreground(AttributedStyle.BLACK);
    AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(statusBarStyle.bold().italic()).append("Q").style(statusBarStyle).append(": Quit     ").style(statusBarStyle.bold().italic()).append("SPACE").style(statusBarStyle).append(": Pause/Resume     ").append(String.valueOf(rowsInBuffer) + " rows in buffer     ");
    if (paused) {
        attrBuilder.style(statusBarStyle.bold().foreground(AttributedStyle.RED).blink()).append("PAUSED");
    }
    String statusBarText = attrBuilder.toAnsi();
    terminal.writer().print(statusBarText);
    terminal.flush();
    terminal.puts(InfoCmp.Capability.restore_cursor);
}
Also used : AttributedStyle(org.jline.utils.AttributedStyle) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder)

Example 2 with AttributedStyle

use of org.jline.utils.AttributedStyle in project samza by apache.

the class CliCommandHandler method formatSchema4Display.

/*
      Field    | Type
      -------------------------
      Field1   | Type 1
      Field2   | VARCHAR(STRING)
      Field... | VARCHAR(STRING)
      -------------------------
  */
private List<String> formatSchema4Display(SqlSchema schema) {
    final String headerField = "Field";
    final String headerType = "Type";
    final char seperator = '|';
    final char lineSep = '-';
    int terminalWidth = terminal.getWidth();
    // Two spaces * 2 plus one SEPERATOR
    if (terminalWidth < 2 + 2 + 1 + headerField.length() + headerType.length()) {
        return Collections.singletonList("Not enough room.");
    }
    // Find the best seperator position for least rows
    int seperatorPos = headerField.length() + 2;
    int minRowNeeded = Integer.MAX_VALUE;
    int longestLineCharNum = 0;
    int rowCount = schema.getFields().size();
    for (int j = seperatorPos; j < terminalWidth - headerType.length() - 2; ++j) {
        boolean fieldWrapped = false;
        int rowNeeded = 0;
        for (int i = 0; i < rowCount; ++i) {
            SqlSchema.SqlField field = schema.getFields().get(i);
            int fieldLen = field.getFieldName().length();
            int typeLen = field.getFieldSchema().getFieldType().toString().length();
            int fieldRowNeeded = CliUtil.ceilingDiv(fieldLen, j - 2);
            int typeRowNeeded = CliUtil.ceilingDiv(typeLen, terminalWidth - 1 - j - 2);
            rowNeeded += Math.max(fieldRowNeeded, typeRowNeeded);
            fieldWrapped |= fieldRowNeeded > 1;
            if (typeRowNeeded > 1) {
                longestLineCharNum = terminalWidth;
            } else {
                longestLineCharNum = Math.max(longestLineCharNum, j + typeLen + 2 + 1);
            }
        }
        if (rowNeeded < minRowNeeded) {
            minRowNeeded = rowNeeded;
            seperatorPos = j;
        }
        if (!fieldWrapped)
            break;
    }
    List<String> lines = new ArrayList<>(minRowNeeded + 4);
    // Header
    StringBuilder line = new StringBuilder(terminalWidth);
    line.append(CliConstants.SPACE);
    line.append(headerField);
    CliUtil.appendTo(line, seperatorPos - 1, CliConstants.SPACE);
    line.append(seperator);
    line.append(CliConstants.SPACE);
    line.append(headerType);
    lines.add(line.toString());
    line = new StringBuilder(terminalWidth);
    CliUtil.appendTo(line, longestLineCharNum - 1, lineSep);
    lines.add(line.toString());
    // Body
    AttributedStyle oddLineStyle = AttributedStyle.BOLD.foreground(AttributedStyle.BLUE);
    AttributedStyle evenLineStyle = AttributedStyle.BOLD.foreground(AttributedStyle.CYAN);
    final int fieldColSize = seperatorPos - 2;
    final int typeColSize = terminalWidth - seperatorPos - 1 - 2;
    for (int i = 0; i < rowCount; ++i) {
        SqlSchema.SqlField sqlField = schema.getFields().get(i);
        String field = sqlField.getFieldName();
        String type = getFieldDisplayValue(sqlField.getFieldSchema());
        int fieldLen = field.length();
        int typeLen = type.length();
        int fieldStartIdx = 0, typeStartIdx = 0;
        while (fieldStartIdx < fieldLen || typeStartIdx < typeLen) {
            line = new StringBuilder(terminalWidth);
            line.append(CliConstants.SPACE);
            int numToWrite = Math.min(fieldColSize, fieldLen - fieldStartIdx);
            if (numToWrite > 0) {
                line.append(field, fieldStartIdx, fieldStartIdx + numToWrite);
                fieldStartIdx += numToWrite;
            }
            CliUtil.appendTo(line, seperatorPos - 1, CliConstants.SPACE);
            line.append(seperator);
            line.append(CliConstants.SPACE);
            numToWrite = Math.min(typeColSize, typeLen - typeStartIdx);
            if (numToWrite > 0) {
                line.append(type, typeStartIdx, typeStartIdx + numToWrite);
                typeStartIdx += numToWrite;
            }
            if (i % 2 == 0) {
                AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(evenLineStyle);
                attrBuilder.append(line.toString());
                lines.add(attrBuilder.toAnsi());
            } else {
                AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(oddLineStyle);
                attrBuilder.append(line.toString());
                lines.add(attrBuilder.toAnsi());
            }
        }
    }
    // Footer
    line = new StringBuilder(terminalWidth);
    CliUtil.appendTo(line, longestLineCharNum - 1, lineSep);
    lines.add(line.toString());
    return lines;
}
Also used : AttributedStyle(org.jline.utils.AttributedStyle) SqlSchema(org.apache.samza.sql.schema.SqlSchema) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) ArrayList(java.util.ArrayList) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder)

Example 3 with AttributedStyle

use of org.jline.utils.AttributedStyle in project samza by apache.

the class CliCommandHandler method commandLs.

private void commandLs(CliCommand command) {
    List<Integer> execIds = new ArrayList<>();
    String parameters = command.getParameters();
    if (CliUtil.isNullOrEmpty(parameters)) {
        execIds.addAll(executions.keySet());
    } else {
        execIds.addAll(splitExecutionIds(parameters));
    }
    if (execIds.size() == 0) {
        return;
    }
    execIds.sort(Integer::compareTo);
    final int terminalWidth = terminal.getWidth();
    final int idWidth = 3;
    final int statusWidth = 20;
    final int cmdWidth = terminalWidth - idWidth - statusWidth - 4;
    AttributedStyle oddLineStyle = AttributedStyle.DEFAULT.BOLD.foreground(AttributedStyle.BLUE);
    AttributedStyle evenLineStyle = AttributedStyle.DEFAULT.BOLD.foreground(AttributedStyle.CYAN);
    for (int i = 0; i < execIds.size(); ++i) {
        Integer id = execIds.get(i);
        String cmd = executions.get(id);
        if (cmd == null)
            continue;
        String status = "UNKNOWN";
        try {
            ExecutionStatus execStatus = executor.queryExecutionStatus(id);
            status = execStatus.name();
        } catch (ExecutorException e) {
            LOG.error("Error in commandLs: ", e);
        }
        int cmdStartIdx = 0;
        int cmdLength = cmd.length();
        StringBuilder line;
        while (cmdStartIdx < cmdLength) {
            line = new StringBuilder(terminalWidth);
            if (cmdStartIdx == 0) {
                line.append(CliConstants.SPACE);
                line.append(id);
                CliUtil.appendTo(line, 1 + idWidth + 1, CliConstants.SPACE);
                line.append(status);
            }
            CliUtil.appendTo(line, 1 + idWidth + 1 + statusWidth + 1, CliConstants.SPACE);
            int numToWrite = Math.min(cmdWidth, cmdLength - cmdStartIdx);
            if (numToWrite > 0) {
                line.append(cmd, cmdStartIdx, cmdStartIdx + numToWrite);
                cmdStartIdx += numToWrite;
            }
            if (i % 2 == 0) {
                AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(evenLineStyle);
                attrBuilder.append(line.toString());
                writer.println(attrBuilder.toAnsi());
            } else {
                AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(oddLineStyle);
                attrBuilder.append(line.toString());
                writer.println(attrBuilder.toAnsi());
            }
        }
    }
    writer.flush();
}
Also used : AttributedStyle(org.jline.utils.AttributedStyle) ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) ExecutionStatus(org.apache.samza.sql.client.interfaces.ExecutionStatus) ArrayList(java.util.ArrayList) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder)

Aggregations

AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)3 AttributedStyle (org.jline.utils.AttributedStyle)3 ArrayList (java.util.ArrayList)2 ExecutorException (org.apache.samza.sql.client.exceptions.ExecutorException)1 ExecutionStatus (org.apache.samza.sql.client.interfaces.ExecutionStatus)1 SqlSchema (org.apache.samza.sql.schema.SqlSchema)1