Skip to content

Commit

Permalink
Improve usability of attributed styles
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed May 16, 2017
1 parent 937e121 commit b50c103
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
4 changes: 1 addition & 3 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ public static void history(LineReader reader, PrintStream out, PrintStream err,
for (History.Entry entry : history) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.append(" ");
sb.style(AttributedStyle.BOLD);
sb.append(String.format("%3d", entry.index() + 1));
sb.style(AttributedStyle.DEFAULT);
sb.styled(AttributedStyle::bold, String.format("%3d", entry.index() + 1));
if (opt.isSet("d")) {
sb.append(" ");
LocalTime lt = LocalTime.from(entry.time().atZone(ZoneId.systemDefault()))
Expand Down
8 changes: 2 additions & 6 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,12 @@ protected String getDisplay(Terminal terminal, Path p) {
String name = p.getFileName().toString();
if (Files.isDirectory(p)) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.BOLD.foreground(AttributedStyle.RED));
sb.append(name);
sb.style(AttributedStyle.DEFAULT);
sb.styled(AttributedStyle.BOLD.foreground(AttributedStyle.RED), name);
sb.append("/");
name = sb.toAnsi(terminal);
} else if (Files.isSymbolicLink(p)) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.BOLD.foreground(AttributedStyle.RED));
sb.append(name);
sb.style(AttributedStyle.DEFAULT);
sb.styled(AttributedStyle.BOLD.foreground(AttributedStyle.RED), name);
sb.append("@");
name = sb.toAnsi(terminal);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jline.reader.Highlighter;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.WCWidth;

public class DefaultHighlighter implements Highlighter {
Expand Down Expand Up @@ -51,30 +52,30 @@ public AttributedString highlight(LineReader reader, String buffer) {
AttributedStringBuilder sb = new AttributedStringBuilder();
for (int i = 0; i < buffer.length(); i++) {
if (i == underlineStart) {
sb.style(sb.style().underline());
sb.style(AttributedStyle::underline);
}
if (i == negativeStart) {
sb.style(sb.style().inverse());
sb.style(AttributedStyle::inverse);
}
char c = buffer.charAt(i);
if (c == '\t' || c == '\n') {
sb.append(c);
} else if (c < 32) {
sb.style(sb.style().inverseNeg())
sb.style(AttributedStyle::inverseNeg)
.append('^')
.append((char) (c + '@'))
.style(sb.style().inverseNeg());
.style(AttributedStyle::inverseNeg);
} else {
int w = WCWidth.wcwidth(c);
if (w > 0) {
sb.append(c);
}
}
if (i == underlineEnd) {
sb.style(sb.style().underlineOff());
sb.style(AttributedStyle::underlineOff);
}
if (i == negativeEnd) {
sb.style(sb.style().inverseOff());
sb.style(AttributedStyle::inverseOff);
}
}
return sb.toAttributedString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,12 @@ protected String getDisplay(Terminal terminal, Path p) {
String name = p.getFileName().toString();
if (Files.isDirectory(p)) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.BOLD.foreground(AttributedStyle.RED));
sb.append(name);
sb.style(AttributedStyle.DEFAULT);
sb.styled(AttributedStyle.BOLD.foreground(AttributedStyle.RED), name);
sb.append("/");
name = sb.toAnsi(terminal);
} else if (Files.isSymbolicLink(p)) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.BOLD.foreground(AttributedStyle.RED));
sb.append(name);
sb.style(AttributedStyle.DEFAULT);
sb.styled(AttributedStyle.BOLD.foreground(AttributedStyle.RED), name);
sb.append("@");
name = sb.toAnsi(terminal);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
package org.jline.utils;

import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -106,6 +109,27 @@ public AttributedStringBuilder style(AttributedStyle style) {
return this;
}

public AttributedStringBuilder style(Function<AttributedStyle,AttributedStyle> style) {
current = style.apply(current);
return this;
}

public AttributedStringBuilder styled(Function<AttributedStyle,AttributedStyle> style, CharSequence cs) {
return styled(style, sb -> sb.append(cs));
}

public AttributedStringBuilder styled(AttributedStyle style, CharSequence cs) {
return styled(s -> style, sb -> sb.append(cs));
}

public AttributedStringBuilder styled(Function<AttributedStyle,AttributedStyle> style, Consumer<AttributedStringBuilder> consumer) {
AttributedStyle prev = current;
current = style.apply(prev);
consumer.accept(this);
current = prev;
return this;
}

public AttributedStyle style() {
return current;
}
Expand Down

0 comments on commit b50c103

Please sign in to comment.