Java Code Examples for org.jline.utils.AttributedStringBuilder#append()

The following examples show how to use org.jline.utils.AttributedStringBuilder#append() . 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: Bats   File: BatsSqlLineApplication.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public PromptHandler getPromptHandler(SqlLine sqlLine) {
    // if (config.hasPath(PROMPT_WITH_SCHEMA) && config.getBoolean(PROMPT_WITH_SCHEMA)) {
    return new PromptHandler(sqlLine) {
        @Override
        protected AttributedString getDefaultPrompt(int connectionIndex, String url, String defaultPrompt) {
            AttributedStringBuilder builder = new AttributedStringBuilder();
            builder.style(resolveStyle("f:y"));
            builder.append("bats");

            ConnectionMetadata meta = sqlLine.getConnectionMetadata();

            String currentSchema = meta.getCurrentSchema();
            if (currentSchema != null) {
                builder.append(" (").append(currentSchema).append(")");
            }
            return builder.style(resolveStyle("default")).append("> ").toAttributedString();
        }
    };
    // }
    // return super.getPromptHandler(sqlLine);
}
 
Example 2
Source Project: Bats   File: DrillSqlLineApplication.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public PromptHandler getPromptHandler(SqlLine sqlLine) {
  if (config.hasPath(PROMPT_WITH_SCHEMA) && config.getBoolean(PROMPT_WITH_SCHEMA)) {
    return new PromptHandler(sqlLine) {
      @Override
      protected AttributedString getDefaultPrompt(int connectionIndex, String url, String defaultPrompt) {
        AttributedStringBuilder builder = new AttributedStringBuilder();
        builder.style(resolveStyle("f:y"));
        builder.append("apache drill");

        ConnectionMetadata meta = sqlLine.getConnectionMetadata();

        String currentSchema = meta.getCurrentSchema();
        if (currentSchema != null) {
          builder.append(" (").append(currentSchema).append(")");
        }
        return builder.style(resolveStyle("default")).append("> ").toAttributedString();
      }
    };
  }
  return super.getPromptHandler(sqlLine);
}
 
Example 3
@Override
public PromptHandler getPromptHandler(SqlLine sqlLine) {
    if (config.hasPath(PROMPT_WITH_SCHEMA) && config.getBoolean(PROMPT_WITH_SCHEMA)) {
        return new PromptHandler(sqlLine) {
            @Override
            protected AttributedString getDefaultPrompt(int connectionIndex, String url, String defaultPrompt) {
                AttributedStringBuilder builder = new AttributedStringBuilder();
                builder.style(resolveStyle("f:y"));
                builder.append("lealone");

                ConnectionMetadata meta = sqlLine.getConnectionMetadata();

                String currentSchema = meta.getCurrentSchema();
                if (currentSchema != null) {
                    builder.append(" (").append(currentSchema).append(")");
                }
                return builder.style(resolveStyle("default")).append("> ").toAttributedString();
            }
        };
    }
    return super.getPromptHandler(sqlLine);
}
 
Example 4
Source Project: flink   File: CliChangelogResultView.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected List<AttributedString> computeMainHeaderLines() {
	final AttributedStringBuilder schemaHeader = new AttributedStringBuilder();

	// add change column
	schemaHeader.append(' ');
	schemaHeader.style(AttributedStyle.DEFAULT.underline());
	schemaHeader.append("+/-");
	schemaHeader.style(AttributedStyle.DEFAULT);

	Arrays.stream(resultDescriptor.getResultSchema().getFieldNames()).forEach(s -> {
		schemaHeader.append(' ');
		schemaHeader.style(AttributedStyle.DEFAULT.underline());
		normalizeColumn(schemaHeader, s, MAX_COLUMN_WIDTH);
		schemaHeader.style(AttributedStyle.DEFAULT);
	});

	return Collections.singletonList(schemaHeader.toAttributedString());
}
 
Example 5
Source Project: flink   File: CliChangelogResultView.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected List<AttributedString> computeMainHeaderLines() {
	final AttributedStringBuilder schemaHeader = new AttributedStringBuilder();

	// add change column
	schemaHeader.append(' ');
	schemaHeader.style(AttributedStyle.DEFAULT.underline());
	schemaHeader.append("+/-");
	schemaHeader.style(AttributedStyle.DEFAULT);

	Arrays.stream(resultDescriptor.getResultSchema().getFieldNames()).forEach(s -> {
		schemaHeader.append(' ');
		schemaHeader.style(AttributedStyle.DEFAULT.underline());
		normalizeColumn(schemaHeader, s, MAX_COLUMN_WIDTH);
		schemaHeader.style(AttributedStyle.DEFAULT);
	});

	return Collections.singletonList(schemaHeader.toAttributedString());
}
 
Example 6
Source Project: flink   File: CliInputView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<AttributedString> computeMainLines() {
	final List<AttributedString> lines = new ArrayList<>();

	// space
	IntStream.range(0, getVisibleMainHeight() / 2 - 2).forEach((i) -> lines.add(AttributedString.EMPTY));

	// title
	lines.add(new AttributedString(CliStrings.DEFAULT_MARGIN + inputTitle));

	// input line
	final AttributedStringBuilder inputLine = new AttributedStringBuilder();
	inputLine.append(CliStrings.DEFAULT_MARGIN + "> ");
	final String input = currentInput.toString();
	// add string left of cursor
	inputLine.append(currentInput.substring(0, cursorPos));
	inputLine.style(AttributedStyle.DEFAULT.inverse().blink());
	if (cursorPos < input.length()) {
		inputLine.append(input.charAt(cursorPos));
		inputLine.style(AttributedStyle.DEFAULT);
		inputLine.append(input.substring(cursorPos + 1, input.length()));
	} else {
		inputLine.append(' '); // show the cursor at the end
	}

	lines.add(inputLine.toAttributedString());

	// isError
	if (isError) {
		final AttributedStringBuilder errorLine = new AttributedStringBuilder();
		errorLine.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
		errorLine.append(CliStrings.DEFAULT_MARGIN + CliStrings.INPUT_ERROR);
		lines.add(AttributedString.EMPTY);
		lines.add(errorLine.toAttributedString());
	}

	return lines;
}
 
Example 7
private AttributedString getOutputString(
    Rows.Row row, String delim, AttributedStyle style) {
  AttributedStringBuilder builder = new AttributedStringBuilder();

  for (int i = 0; i < row.values.length; i++) {
    if (builder.length() > 0) {
      builder.append(delim);
    }
    builder.append(SqlLine.rpad(row.values[i], row.sizes[i]), style);
  }

  return builder.toAttributedString();
}
 
Example 8
Source Project: Flink-CEPplus   File: CliResultView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<AttributedString> computeMainLines() {
	final List<AttributedString> lines = new ArrayList<>();

	int lineIdx = 0;
	for (String[] line : results) {
		final AttributedStringBuilder row = new AttributedStringBuilder();

		// highlight selected row
		if (lineIdx == selectedRow) {
			row.style(AttributedStyle.DEFAULT.inverse());
		}

		for (int colIdx = 0; colIdx < line.length; colIdx++) {
			final String col = line[colIdx];
			final int columnWidth = computeColumnWidth(colIdx);

			row.append(' ');
			// check if value was present before last update, if not, highlight it
			// we don't highlight if the retrieval stopped
			// both inverse and bold together do not work correctly
			if (previousResults != null && lineIdx != selectedRow && refreshThread.isRunning &&
					(lineIdx >= previousResults.size() || !col.equals(previousResults.get(lineIdx)[colIdx]))) {
				row.style(AttributedStyle.BOLD);
				normalizeColumn(row, col, columnWidth);
				row.style(AttributedStyle.DEFAULT);
			} else {
				normalizeColumn(row, col, columnWidth);
			}
		}
		lines.add(row.toAttributedString());

		lineIdx++;
	}

	return lines;
}
 
Example 9
Source Project: Flink-CEPplus   File: CliView.java    License: Apache License 2.0 5 votes vote down vote up
private AttributedString computeTitleLine() {
	final String title = getTitle();
	final AttributedStringBuilder titleLine = new AttributedStringBuilder();
	titleLine.style(AttributedStyle.INVERSE);
	final int totalMargin = width - title.length();
	final int margin = totalMargin / 2;
	repeatChar(titleLine, ' ', margin);
	titleLine.append(title);
	repeatChar(titleLine, ' ', margin + (totalMargin % 2));
	return titleLine.toAttributedString();
}
 
Example 10
Source Project: Flink-CEPplus   File: CliUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void normalizeColumn(AttributedStringBuilder sb, String col, int maxWidth) {
	// limit column content
	if (col.length() > maxWidth) {
		sb.append(col, 0, maxWidth - 1);
		sb.append('~');
	} else {
		repeatChar(sb, ' ', maxWidth - col.length());
		sb.append(col);
	}
}
 
Example 11
@ShellMethod(value = "Display the available post processors")
public CharSequence postprocessors() {
    AttributedStringBuilder result = new AttributedStringBuilder();
    result.append("Available Post-Processors\n\n", AttributedStyle.BOLD);
    for (PostProcessor postProcessor : postProcessors) {
        result.append("\t" + postProcessor.getName() + ": ", AttributedStyle.BOLD);
        Class<?> cls =
                ((Class) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
        result.append(cls.getName() + "\n", AttributedStyle.DEFAULT);
    }

    return result;
}
 
Example 12
@Override
protected List<AttributedString> computeHeaderLines() {

	final AttributedStringBuilder statusLine = new AttributedStringBuilder();
	statusLine.style(AttributedStyle.INVERSE);
	// left
	final String left;
	if (isRetrieving()) {
		left = CliStrings.DEFAULT_MARGIN + CliStrings.RESULT_REFRESH_INTERVAL + ' ' + REFRESH_INTERVALS.get(refreshInterval).f0;
	} else {
		left = CliStrings.DEFAULT_MARGIN + CliStrings.RESULT_STOPPED;
	}

	// right
	final String right;
	if (lastRetrieval == null) {
		right = CliStrings.RESULT_LAST_REFRESH + ' ' + CliStrings.RESULT_REFRESH_UNKNOWN + CliStrings.DEFAULT_MARGIN;
	} else {
		right = CliStrings.RESULT_LAST_REFRESH + ' ' + lastRetrieval.format(TIME_FORMATTER) + CliStrings.DEFAULT_MARGIN;
	}
	// all together
	final int middleSpace = getWidth() - left.length() - right.length();
	statusLine.append(left);
	repeatChar(statusLine, ' ', middleSpace);
	statusLine.append(right);

	return Arrays.asList(statusLine.toAttributedString(), AttributedString.EMPTY);
}
 
Example 13
Source Project: Flink-CEPplus   File: CliInputView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<AttributedString> computeMainLines() {
	final List<AttributedString> lines = new ArrayList<>();

	// space
	IntStream.range(0, getVisibleMainHeight() / 2 - 2).forEach((i) -> lines.add(AttributedString.EMPTY));

	// title
	lines.add(new AttributedString(CliStrings.DEFAULT_MARGIN + inputTitle));

	// input line
	final AttributedStringBuilder inputLine = new AttributedStringBuilder();
	inputLine.append(CliStrings.DEFAULT_MARGIN + "> ");
	final String input = currentInput.toString();
	// add string left of cursor
	inputLine.append(currentInput.substring(0, cursorPos));
	inputLine.style(AttributedStyle.DEFAULT.inverse().blink());
	if (cursorPos < input.length()) {
		inputLine.append(input.charAt(cursorPos));
		inputLine.style(AttributedStyle.DEFAULT);
		inputLine.append(input.substring(cursorPos + 1, input.length()));
	} else {
		inputLine.append(' '); // show the cursor at the end
	}

	lines.add(inputLine.toAttributedString());

	// isError
	if (isError) {
		final AttributedStringBuilder errorLine = new AttributedStringBuilder();
		errorLine.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
		errorLine.append(CliStrings.DEFAULT_MARGIN + CliStrings.INPUT_ERROR);
		lines.add(AttributedString.EMPTY);
		lines.add(errorLine.toAttributedString());
	}

	return lines;
}
 
Example 14
Source Project: flink   File: CliResultView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<AttributedString> computeMainLines() {
	final List<AttributedString> lines = new ArrayList<>();

	int lineIdx = 0;
	for (String[] line : results) {
		final AttributedStringBuilder row = new AttributedStringBuilder();

		// highlight selected row
		if (lineIdx == selectedRow) {
			row.style(AttributedStyle.DEFAULT.inverse());
		}

		for (int colIdx = 0; colIdx < line.length; colIdx++) {
			final String col = line[colIdx];
			final int columnWidth = computeColumnWidth(colIdx);

			row.append(' ');
			// check if value was present before last update, if not, highlight it
			// we don't highlight if the retrieval stopped
			// both inverse and bold together do not work correctly
			if (previousResults != null && lineIdx != selectedRow && refreshThread.isRunning &&
					(lineIdx >= previousResults.size() || !col.equals(previousResults.get(lineIdx)[colIdx]))) {
				row.style(AttributedStyle.BOLD);
				normalizeColumn(row, col, columnWidth);
				row.style(AttributedStyle.DEFAULT);
			} else {
				normalizeColumn(row, col, columnWidth);
			}
		}
		lines.add(row.toAttributedString());

		lineIdx++;
	}

	return lines;
}
 
Example 15
Source Project: flink   File: CliUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void normalizeColumn(AttributedStringBuilder sb, String col, int maxWidth) {
	// limit column content
	if (col.length() > maxWidth) {
		sb.append(col, 0, maxWidth - 1);
		sb.append('~');
	} else {
		repeatChar(sb, ' ', maxWidth - col.length());
		sb.append(col);
	}
}
 
Example 16
@Override protected AttributedString getDefaultPrompt(int connectionIndex,
    String url, String defaultPrompt) {
  AttributedStringBuilder builder = new AttributedStringBuilder();
  builder.append("my_app");

  final ConnectionMetadata meta = sqlLine.getConnectionMetadata();

  final String currentSchema = meta.getCurrentSchema();
  if (currentSchema != null) {
    builder.append(" (").append(currentSchema).append(")");
  }
  return builder.append(">").toAttributedString();
}
 
Example 17
Source Project: flink   File: CliChangelogResultView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<AttributedString> computeHeaderLines() {

	final AttributedStringBuilder statusLine = new AttributedStringBuilder();
	statusLine.style(AttributedStyle.INVERSE);
	// left
	final String left;
	if (isRetrieving()) {
		left = CliStrings.DEFAULT_MARGIN + CliStrings.RESULT_REFRESH_INTERVAL + ' ' + REFRESH_INTERVALS.get(refreshInterval).f0;
	} else {
		left = CliStrings.DEFAULT_MARGIN + CliStrings.RESULT_STOPPED;
	}

	// right
	final String right;
	if (lastRetrieval == null) {
		right = CliStrings.RESULT_LAST_REFRESH + ' ' + CliStrings.RESULT_REFRESH_UNKNOWN + CliStrings.DEFAULT_MARGIN;
	} else {
		right = CliStrings.RESULT_LAST_REFRESH + ' ' + lastRetrieval.format(TIME_FORMATTER) + CliStrings.DEFAULT_MARGIN;
	}
	// all together
	final int middleSpace = getWidth() - left.length() - right.length();
	statusLine.append(left);
	repeatChar(statusLine, ' ', middleSpace);
	statusLine.append(right);

	return Arrays.asList(statusLine.toAttributedString(), AttributedString.EMPTY);
}
 
Example 18
Source Project: flink   File: CliInputView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<AttributedString> computeMainLines() {
	final List<AttributedString> lines = new ArrayList<>();

	// space
	IntStream.range(0, getVisibleMainHeight() / 2 - 2).forEach((i) -> lines.add(AttributedString.EMPTY));

	// title
	lines.add(new AttributedString(CliStrings.DEFAULT_MARGIN + inputTitle));

	// input line
	final AttributedStringBuilder inputLine = new AttributedStringBuilder();
	inputLine.append(CliStrings.DEFAULT_MARGIN + "> ");
	final String input = currentInput.toString();
	// add string left of cursor
	inputLine.append(currentInput.substring(0, cursorPos));
	inputLine.style(AttributedStyle.DEFAULT.inverse().blink());
	if (cursorPos < input.length()) {
		inputLine.append(input.charAt(cursorPos));
		inputLine.style(AttributedStyle.DEFAULT);
		inputLine.append(input.substring(cursorPos + 1, input.length()));
	} else {
		inputLine.append(' '); // show the cursor at the end
	}

	lines.add(inputLine.toAttributedString());

	// isError
	if (isError) {
		final AttributedStringBuilder errorLine = new AttributedStringBuilder();
		errorLine.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
		errorLine.append(CliStrings.DEFAULT_MARGIN + CliStrings.INPUT_ERROR);
		lines.add(AttributedString.EMPTY);
		lines.add(errorLine.toAttributedString());
	}

	return lines;
}
 
Example 19
private AttributedString getOutputString(
    Rows rows, Rows.Row row, String delim) {
  AttributedStringBuilder builder = new AttributedStringBuilder();

  boolean isStyled = sqlLine.getOpts().getColor();
  for (int i = 0; i < row.values.length; i++) {
    if (builder.length() > 0) {
      builder.append(delim,
          isStyled ? AttributedStyles.GREEN : AttributedStyle.DEFAULT);
    }

    String v;

    final int[] sizes = row.sizes;
    if (row.isMeta) {
      v = SqlLine.center(row.values[i], row.sizes[i]);
      if (rows.isPrimaryKey(i)) {
        builder.append(v, AttributedStyles.CYAN);
      } else {
        builder.append(v, AttributedStyle.BOLD);
      }
    } else {
      v = rpad(row.values[i], row.sizes[i]);
      if (rows.isPrimaryKey(i)) {
        builder.append(v, AttributedStyles.CYAN);
      } else {
        builder.append(v);
      }
    }
  }

  if (row.deleted) { // make deleted rows red
    return new AttributedStringBuilder()
        .append(builder.toString(), AttributedStyles.RED)
        .toAttributedString();
  } else if (row.updated) { // make updated rows blue
    return new AttributedStringBuilder()
        .append(builder.toString(), AttributedStyles.BLUE)
        .toAttributedString();
  } else if (row.inserted) { // make new rows green
    return new AttributedStringBuilder()
        .append(builder.toString(), AttributedStyles.GREEN)
        .toAttributedString();
  }

  return builder.toAttributedString();
}
 
Example 20
@Override
protected List<AttributedString> computeHeaderLines() {
	final AttributedStringBuilder statusLine = new AttributedStringBuilder();
	statusLine.style(AttributedStyle.INVERSE);
	// left
	final String left;
	if (isRetrieving()) {
		left = CliStrings.DEFAULT_MARGIN + CliStrings.RESULT_REFRESH_INTERVAL + ' ' + REFRESH_INTERVALS.get(refreshInterval).f0;
	} else {
		left = CliStrings.DEFAULT_MARGIN + CliStrings.RESULT_STOPPED;
	}
	// middle
	final StringBuilder middleBuilder = new StringBuilder();
	middleBuilder.append(CliStrings.RESULT_PAGE);
	middleBuilder.append(' ');
	if (page == LAST_PAGE) {
		middleBuilder.append(CliStrings.RESULT_LAST_PAGE);
	} else {
		middleBuilder.append(page);
	}
	middleBuilder.append(CliStrings.RESULT_PAGE_OF);
	middleBuilder.append(pageCount);
	final String middle = middleBuilder.toString();
	// right
	final String right;
	if (lastRetrieval == null) {
		right = CliStrings.RESULT_LAST_REFRESH + ' ' + CliStrings.RESULT_REFRESH_UNKNOWN + CliStrings.DEFAULT_MARGIN;
	} else {
		right = CliStrings.RESULT_LAST_REFRESH + ' ' + lastRetrieval.format(TIME_FORMATTER) + CliStrings.DEFAULT_MARGIN;
	}
	// all together
	final int totalLeftSpace = getWidth() - middle.length();
	final int leftSpace = totalLeftSpace / 2 - left.length();
	statusLine.append(left);
	repeatChar(statusLine, ' ', leftSpace);
	statusLine.append(middle);
	final int rightSpacing = getWidth() - statusLine.length() - right.length();
	repeatChar(statusLine, ' ', rightSpacing);
	statusLine.append(right);

	return Arrays.asList(statusLine.toAttributedString(), AttributedString.EMPTY);
}