Skip to content

Commit

Permalink
Use style instead of colours for the completion list
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 9, 2020
1 parent b77a0a8 commit 63b5c11
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 69 deletions.
4 changes: 1 addition & 3 deletions reader/src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,10 @@ public interface LineReader {
String COMPLETION_STYLE_SELECTION = "COMPLETION_STYLE_SELECTION";
/** Completion style for displaying the candidate description */
String COMPLETION_STYLE_DESCRIPTION = "COMPLETION_STYLE_DESCRIPTION";
String COMPLETION_COLOR_DESCRIPTION = "COMPLETION_COLOR_DESCRIPTION";
/** Completion style for displaying the matching part of candidates */
String COMPLETION_STYLE_STARTING = "COMPLETION_STYLE_STARTING";
String COMPLETION_COLOR_STARTING = "COMPLETION_COLOR_STARTING";
/** Completion list background color */
String COMPLETION_LIST_BACKGROUND_COLOR = "COMPLETION_LIST_BACKGROUND_COLOR";
String COMPLETION_STYLE_BACKGROUND = "COMPLETION_STYLE_BACKGROUND";
/**
* Set the template for prompts for secondary (continuation) lines.
* This is a prompt template as described in the class header.
Expand Down
107 changes: 41 additions & 66 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ public class LineReaderImpl implements LineReader, Flushable
public static final String DEFAULT_SECONDARY_PROMPT_PATTERN = "%M> ";
public static final String DEFAULT_OTHERS_GROUP_NAME = "others";
public static final String DEFAULT_ORIGINAL_GROUP_NAME = "original";
public static final String DEFAULT_COMPLETION_STYLE_STARTING = "";
public static final String DEFAULT_COMPLETION_STYLE_DESCRIPTION = "";
public static final String DEFAULT_COMPLETION_STYLE_STARTING = "36"; // cyan
public static final String DEFAULT_COMPLETION_STYLE_DESCRIPTION = "90"; // dark gray
public static final String DEFAULT_COMPLETION_STYLE_GROUP = "35;1"; // magenta
public static final String DEFAULT_COMPLETION_STYLE_SELECTION = "7"; // inverted
public static final int DEFAULT_COMPLETION_COLOR_STARTING = 6; // cyan
public static final int DEFAULT_COMPLETION_COLOR_DESCRIPTION = 8; // dark gray
public static final int DEFAULT_COMPLETION_LIST_BACKGROUND_COLOR = 13;
public static final String DEFAULT_COMPLETION_STYLE_BACKGROUND = "48;5;242";
public static final int DEFAULT_INDENTATION = 0;
public static final int DEFAULT_FEATURES_MAX_BUFFER_SIZE = 1000;
public static final int DEFAULT_SUGGESTIONS_MIN_BUFFER_SIZE = 1;
Expand Down Expand Up @@ -5318,7 +5316,7 @@ protected void toColumns(Object items, int width, int maxWidth, AttributedString
}
// This is a group
if (items instanceof String && !doMenuList) {
sb.style(getCompletionStyleGroup(false))
sb.style(getCompletionStyleGroup())
.append((String) items)
.style(AttributedStyle.DEFAULT)
.append("\n");
Expand Down Expand Up @@ -5347,6 +5345,7 @@ else if (items instanceof List) {
sb.style(AttributedStyle.DEFAULT);
sb.append('\t');
}
AttributedStringBuilder asb = new AttributedStringBuilder();
for (int j = 0; j < columns; j++) {
int idx = index.applyAsInt(i, j);
if (idx < candidates.size()) {
Expand All @@ -5371,118 +5370,94 @@ else if (items instanceof List) {
}
if (cand == selection) {
out[1] = i;
sb.style(getCompletionStyleSelection(doMenuList));
asb.style(getCompletionStyleSelection());
if (left.toString().regionMatches(
isSet(Option.CASE_INSENSITIVE), 0, completed, 0, completed.length())) {
sb.append(left.toString(), 0, completed.length());
sb.append(left.toString(), completed.length(), left.length());
asb.append(left.toString(), 0, completed.length());
asb.append(left.toString(), completed.length(), left.length());
} else {
sb.append(left.toString());
asb.append(left.toString());
}
for (int k = 0; k < maxWidth - lw - rw; k++) {
sb.append(' ');
asb.append(' ');
}
if (right != null) {
sb.append(right);
asb.append(right);
}
asb.style(AttributedStyle.DEFAULT);
} else {
if (left.toString().regionMatches(
isSet(Option.CASE_INSENSITIVE), 0, completed, 0, completed.length())) {
sb.style(getCompletionStyleStarting(doMenuList));
sb.append(left, 0, completed.length());
sb.style(getCompletionListBackgroundStyle(doMenuList));
sb.append(left, completed.length(), left.length());
asb.style(getCompletionStyleStarting());
asb.append(left, 0, completed.length());
asb.style(AttributedStyle.DEFAULT);
asb.append(left, completed.length(), left.length());
} else {
sb.style(getCompletionListBackgroundStyle(doMenuList));
sb.append(left);
asb.append(left);
}
if (right != null || hasRightItem) {
sb.style(getCompletionListBackgroundStyle(doMenuList));
for (int k = 0; k < maxWidth - lw - rw; k++) {
sb.append(' ');
asb.append(' ');
}
}
if (right != null) {
sb.style(getCompletionStyleDescription(doMenuList));
sb.append(right);
asb.style(getCompletionStyleDescription());
asb.append(right);
asb.style(AttributedStyle.DEFAULT);
} else if (doMenuList) {
sb.style(getCompletionListBackgroundStyle(true));
for (int k = lw; k < maxWidth; k++) {
sb.append(' ');
asb.append(' ');
}
}
}
sb.style(getCompletionListBackgroundStyle(doMenuList));
if (hasRightItem) {
for (int k = 0; k < MARGIN_BETWEEN_COLUMNS; k++) {
sb.append(' ');
asb.append(' ');
}
}
if (doMenuList) {
sb.append(' ');
asb.append(' ');
}
}
}
if (doMenuList) {
sb.style(getCompletionListBackgroundStyle());
}
sb.append(asb.toAttributedString());
sb.append('\n');
}
out[0] += lines;
}
}

private AttributedStyle getCompletionStyleStarting(boolean menuList) {
String str = getString(COMPLETION_STYLE_STARTING, DEFAULT_COMPLETION_STYLE_STARTING);
if (str.isEmpty()) {
return buildStyle(getInt(COMPLETION_COLOR_STARTING, DEFAULT_COMPLETION_COLOR_STARTING), menuList);
}
return buildStyle(str, menuList);
private AttributedStyle getCompletionListBackgroundStyle() {
return getCompletionStyle(COMPLETION_STYLE_BACKGROUND, DEFAULT_COMPLETION_STYLE_BACKGROUND);
}

protected AttributedStyle getCompletionStyleDescription(boolean menuList) {
String str = getString(COMPLETION_STYLE_DESCRIPTION, DEFAULT_COMPLETION_STYLE_DESCRIPTION);
if (str.isEmpty()) {
return buildStyle(getInt(COMPLETION_COLOR_DESCRIPTION, DEFAULT_COMPLETION_COLOR_DESCRIPTION), menuList);
}
return buildStyle(str, menuList);
private AttributedStyle getCompletionStyleStarting() {
return getCompletionStyle(COMPLETION_STYLE_STARTING, DEFAULT_COMPLETION_STYLE_STARTING);
}

protected AttributedStyle getCompletionStyleGroup(boolean menuList) {
return getCompletionStyle(COMPLETION_STYLE_GROUP, DEFAULT_COMPLETION_STYLE_GROUP, menuList);
protected AttributedStyle getCompletionStyleDescription() {
return getCompletionStyle(COMPLETION_STYLE_DESCRIPTION, DEFAULT_COMPLETION_STYLE_DESCRIPTION);
}

protected AttributedStyle getCompletionStyleSelection(boolean menuList) {
return getCompletionStyle(COMPLETION_STYLE_SELECTION, DEFAULT_COMPLETION_STYLE_SELECTION, menuList);
protected AttributedStyle getCompletionStyleGroup() {
return getCompletionStyle(COMPLETION_STYLE_GROUP, DEFAULT_COMPLETION_STYLE_GROUP);
}

protected AttributedStyle getCompletionStyle(String name, String value, boolean menuList) {
return buildStyle(getString(name, value), menuList);
protected AttributedStyle getCompletionStyleSelection() {
return getCompletionStyle(COMPLETION_STYLE_SELECTION, DEFAULT_COMPLETION_STYLE_SELECTION);
}

protected AttributedStyle getCompletionListBackgroundStyle(boolean menuList) {
if (menuList) {
return AttributedStyle.DEFAULT.background(getCompletionListBackgroundColor());
}
return AttributedStyle.DEFAULT.backgroundDefault();
protected AttributedStyle getCompletionStyle(String name, String value) {
return buildStyle(getString(name, value));
}

protected int getCompletionListBackgroundColor() {
return getInt(COMPLETION_LIST_BACKGROUND_COLOR, DEFAULT_COMPLETION_LIST_BACKGROUND_COLOR);
}

protected AttributedStyle buildStyle(String str, boolean menuList) {
if (menuList) {
return AttributedString.fromAnsi("\u001b[" + str + "m ").styleAt(0)
.background(getCompletionListBackgroundColor());
}
protected AttributedStyle buildStyle(String str) {
return AttributedString.fromAnsi("\u001b[" + str + "m ").styleAt(0);
}

protected AttributedStyle buildStyle(int fg, boolean menuList) {
if (menuList) {
return AttributedStyle.DEFAULT.foreground(fg).background(getCompletionListBackgroundColor());
}
return AttributedStyle.DEFAULT.foreground(fg);
}

private String getCommonStart(String str1, String str2, boolean caseInsensitive) {
int[] s1 = str1.codePoints().toArray();
int[] s2 = str2.codePoints().toArray();
Expand Down

0 comments on commit 63b5c11

Please sign in to comment.