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

The following examples show how to use org.jline.utils.AttributedStringBuilder#toAttributedString() . 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: samza   File: CliHighlighter.java    License: Apache License 2.0 6 votes vote down vote up
public AttributedString highlight(LineReader reader, String buffer) {
  AttributedStringBuilder builder = new AttributedStringBuilder();
  List<String> tokens = splitWithSpace(buffer);

  for (String token : tokens) {
    if (isKeyword(token)) {
      builder.style(AttributedStyle.BOLD.foreground(AttributedStyle.YELLOW))
              .append(token);
    } else {
      builder.style(AttributedStyle.DEFAULT)
              .append(token);
    }
  }

  return builder.toAttributedString();
}
 
Example 2
Source Project: presto   File: InputHighlighter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public AttributedString highlight(LineReader reader, String buffer)
{
    TokenSource tokens = StatementSplitter.getLexer(buffer, STATEMENT_DELIMITERS);
    AttributedStringBuilder builder = new AttributedStringBuilder();

    boolean error = false;
    while (true) {
        Token token = tokens.nextToken();
        int type = token.getType();
        if (type == Token.EOF) {
            break;
        }
        String text = token.getText();

        if (error || (type == SqlBaseLexer.UNRECOGNIZED)) {
            error = true;
            builder.styled(ERROR_STYLE, text);
        }
        else if (isKeyword(text)) {
            builder.styled(KEYWORD_STYLE, text);
        }
        else if (isString(type)) {
            builder.styled(STRING_STYLE, text);
        }
        else if (isNumber(type)) {
            builder.styled(NUMBER_STYLE, text);
        }
        else if (isComment(type)) {
            builder.styled(COMMENT_STYLE, text);
        }
        else {
            builder.append(text);
        }
    }

    return builder.toAttributedString();
}
 
Example 3
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 4
Source Project: Flink-CEPplus   File: CliStrings.java    License: Apache License 2.0 5 votes vote down vote up
public static AttributedString messageError(String message, String s) {
	final AttributedStringBuilder builder = new AttributedStringBuilder()
		.style(AttributedStyle.DEFAULT.bold().foreground(AttributedStyle.RED))
		.append("[ERROR] ")
		.append(message);

	if (s != null) {
		builder
			.append(" Reason:\n")
			.append(s);
	}

	return builder.toAttributedString();
}
 
Example 5
Source Project: flink   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 6
Source Project: flink   File: CliStrings.java    License: Apache License 2.0 5 votes vote down vote up
public static AttributedString messageError(String message, String s) {
	final AttributedStringBuilder builder = new AttributedStringBuilder()
		.style(AttributedStyle.DEFAULT.bold().foreground(AttributedStyle.RED))
		.append("[ERROR] ")
		.append(message);

	if (s != null) {
		builder
			.append(" Reason:\n")
			.append(s);
	}

	return builder.toAttributedString();
}
 
Example 7
Source Project: flink   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 8
Source Project: flink   File: CliStrings.java    License: Apache License 2.0 5 votes vote down vote up
public static AttributedString messageError(String message, String s) {
	final AttributedStringBuilder builder = new AttributedStringBuilder()
		.style(AttributedStyle.DEFAULT.bold().foreground(AttributedStyle.RED))
		.append("[ERROR] ")
		.append(message);

	if (s != null) {
		builder
			.append(" Reason:\n")
			.append(s);
	}

	return builder.toAttributedString();
}
 
Example 9
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 10
protected AttributedString getPrompt(
    SqlLine sqlLine, int connectionIndex, String prompt) {
  AttributedStringBuilder promptStringBuilder = new AttributedStringBuilder();
  final SqlLineOpts opts = sqlLine.getOpts();
  final ConnectionMetadata meta = sqlLine.getConnectionMetadata();
  for (int i = 0; i < prompt.length(); i++) {
    switch (prompt.charAt(i)) {
    case '%':
      if (i < prompt.length() - 1) {
        String dateTime = formatDateTime(prompt.charAt(i + 1));
        if (dateTime != null) {
          promptStringBuilder.append(dateTime);
        } else {
          switch (prompt.charAt(i + 1)) {
          case 'c':
            if (connectionIndex >= 0) {
              promptStringBuilder.append(String.valueOf(connectionIndex));
            }
            break;
          case 'C':
            if (connectionIndex >= 0) {
              promptStringBuilder
                  .append(String.valueOf(connectionIndex)).append(": ");
            }
            break;
          case 'd':
            String databaseProductName = meta.getDatabaseProductName();
            if (databaseProductName != null) {
              promptStringBuilder.append(databaseProductName);
            }
            break;
          case 'n':
            String userName = meta.getUserName();
            if (userName != null) {
              promptStringBuilder.append(userName);
            }
            break;
          case 'u':
            String url = meta.getUrl();
            if (url != null) {
              promptStringBuilder.append(url);
            }
            break;
          case 'S':
            String currentSchema = meta.getCurrentSchema();
            if (currentSchema != null) {
              promptStringBuilder.append(currentSchema);
            }
            break;
          case '[':
            int closeBracketIndex = prompt.indexOf("%]", i + 2);
            if (closeBracketIndex > 0) {
              String color = prompt.substring(i + 2, closeBracketIndex);
              AttributedStyle style = resolveStyle(color);
              promptStringBuilder.style(style);
              i = closeBracketIndex;
              break;
            }
          // fall through if not a color
          case ':':
            int nextColonIndex = prompt.indexOf(":", i + 2);
            SqlLineProperty property;
            if (nextColonIndex > 0
                && ((property = BuiltInProperty.valueOf(
                prompt.substring(i + 2, nextColonIndex),
                true)) != null)) {
              promptStringBuilder.append(opts.get(property));
              i = nextColonIndex - 1;
              break;
            }
          // fall through if not a variable name
          default:
            promptStringBuilder
                .append(prompt.charAt(i)).append(prompt.charAt(i + 1));
          }
        }
        i = i + 1;
      }
      break;
    default:
      promptStringBuilder.append(prompt.charAt(i));
    }
  }
  return promptStringBuilder.toAttributedString();
}
 
Example 11
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();
}