Skip to content

Commit

Permalink
Command colors: add option --ansistyles
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 23, 2021
1 parent cc874f7 commit 6c3cfb7
Showing 1 changed file with 56 additions and 30 deletions.
86 changes: 56 additions & 30 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,10 @@ public static void setvar(LineReader lineReader, PrintStream out, PrintStream er

public static void colors(Terminal terminal, PrintStream out, String[] argv) throws HelpException, IOException {
String[] usage = {
"colors - view 256-color table",
"colors - view 256-color table and ANSI-styles",
"Usage: colors [OPTIONS]",
" -? --help Displays command help",
" -a --ansistyles List ANSI-styles",
" -c --columns=COLUMNS Number of columns in name/rgb table",
" COLUMNS = 1, display columns: color, style, ansi and HSL",
" -f --find=NAME Find color names which contains NAME ",
Expand All @@ -1053,42 +1054,46 @@ public static void colors(Terminal terminal, PrintStream out, String[] argv) thr
if (opt.isSet("help")) {
throw new Options.HelpException(opt.usage());
}
String style = null;
if (opt.isSet("lock")) {
style = opt.get("lock");
if (style.length() - style.replace(":", "").length() > 1) {
style = null;
}
}
Colors colors = new Colors(terminal, out);
if (!opt.isSet("view")) {
boolean rgb = opt.isSet("rgb");
int columns = terminal.getWidth() > (rgb ? 71 : 122) ? 6 : 5;
String findName = null;
boolean nameTable = opt.isSet("name");
boolean table16 = opt.isSet("small");
if (opt.isSet("find")) {
findName = opt.get("find").toLowerCase();
nameTable = true;
table16 = false;
columns = 4;
}
if (table16) {
columns = columns + 2;
}
if (opt.isSet("columns")) {
columns = opt.getNumber("columns");
}
colors.printColors(nameTable, rgb, table16, columns, findName, style);
if (opt.isSet("ansistyles")) {
colors.printStyles();
} else {
colors.printColor(opt.get("view").toLowerCase(), style);
String style = null;
if (opt.isSet("lock")) {
style = opt.get("lock");
if (style.length() - style.replace(":", "").length() > 1) {
style = null;
}
}
if (!opt.isSet("view")) {
boolean rgb = opt.isSet("rgb");
int columns = terminal.getWidth() > (rgb ? 71 : 122) ? 6 : 5;
String findName = null;
boolean nameTable = opt.isSet("name");
boolean table16 = opt.isSet("small");
if (opt.isSet("find")) {
findName = opt.get("find").toLowerCase();
nameTable = true;
table16 = false;
columns = 4;
}
if (table16) {
columns = columns + 2;
}
if (opt.isSet("columns")) {
columns = opt.getNumber("columns");
}
colors.printColors(nameTable, rgb, table16, columns, findName, style);
} else {
colors.printColor(opt.get("view").toLowerCase(), style);
}
}
}

private static class Colors {
private static final String COLORS_24BIT = "[0-9a-fA-F]{6}";
private static final List<String> COLORS_16 = Arrays.asList("black","red","green","yellow","blue","magenta","cyan","white"
, "!black","!red","!green","!yellow","!blue","!magenta","!cyan","!white");
private static final List<String> COLORS_16 = Arrays.asList("black","red","green","yellow","blue","magenta"
,"cyan","white", "!black","!red","!green","!yellow","!blue","!magenta","!cyan","!white");
boolean name;
boolean rgb;
private final Terminal terminal;
Expand All @@ -1102,6 +1107,27 @@ public Colors(Terminal terminal, PrintStream out) {
this.out = out;
}

private String getAnsiStyle(String style) {
return style;
}

public void printStyles() {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.tabs(13);
for (String s : Styles.ANSI_STYLES) {
AttributedStyle as = new StyleResolver(this::getAnsiStyle).resolve("." + s);
asb.style(as);
asb.append(s);
asb.style(AttributedStyle.DEFAULT);
asb.append("\t");
asb.append(getAnsiStyle(s));
asb.append("\t");
asb.append(as.toAnsi());
asb.append("\n");
}
asb.toAttributedString().println(terminal);
}

private String getStyle(String color) {
String out;
char fg = ' ';
Expand Down

0 comments on commit 6c3cfb7

Please sign in to comment.