Skip to content

Commit

Permalink
Command colors: reformatting name table
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 19, 2020
1 parent f163d40 commit 8422cfe
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Expand Up @@ -1039,15 +1039,15 @@ public static void colors(Terminal terminal, PrintStream out, String[] argv) thr
"colors - view 256-color table",
"Usage: colors [OPTIONS]",
" -? --help Displays command help",
" -c --columns=COLUMNS Number of columns in name table",
" -c --columns=COLUMNS Number of columns in name table (default 6)",
" -n --name Color name table (default number table)",
" -s --small View 16-color table (default 256-color)"
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
throw new Options.HelpException(opt.usage());
}
int columns = 256;
int columns = terminal.getWidth() > 131 ? 6 : 5;
if (opt.isSet("columns")) {
columns = opt.getNumber("columns");
}
Expand Down Expand Up @@ -1185,23 +1185,39 @@ public void printColors(boolean name, boolean small, int columns) throws IOExcep
int col = 0;
Integer idx = 0;
int colWidth = 22;
int lb = 1;
while (line != null) {
line = line.trim();
if (!line.isEmpty() && !line.startsWith("#")) {
String fg = foreground(idx);
AttributedStyle ss = new StyleResolver(this::getStyle).resolve("." + fg + line, null);
asb.style(ss);
if (columns > 3) {
colWidth = idx > 15 && idx < 232 ? 22 : (idx % 2 == 0 || columns != 6 ? 16 : 17);
lb = idx > 15 && idx < 232 ? 1 : -1;
}
asb.append(String.valueOf(idx)).append(addPadding(colWidth - idx.toString().length(), line));
col++;
idx++;
if ((col + 1)*colWidth > width || col + 1 > columns) {
if ((col + 1)*colWidth > width || col + lb > columns) {
col = 0;
asb.style(AttributedStyle.DEFAULT);
asb.append('\n');
}
}
if (small && idx == 16) {
break;
if (idx == 16) {
if (small) {
break;
} else if (col != 0) {
col = 0;
asb.style(AttributedStyle.DEFAULT);
asb.append('\n');
}
}
if (idx == 232 && col != 0) {
col = 0;
asb.style(AttributedStyle.DEFAULT);
asb.append('\n');
}
line = reader.readLine();
}
Expand Down

0 comments on commit 8422cfe

Please sign in to comment.