Skip to content

Commit

Permalink
Better of implementation for when DELAY_LINE_WRAP is unset.
Browse files Browse the repository at this point in the history
  • Loading branch information
PerBothner committed Nov 20, 2016
1 parent 8bbf2c7 commit f5ad557
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,16 @@ enum Option {

/** After writing into the rightmost column, do we immediately
* move to the next line (the default)? Or do we wait until
* the next character. */
* the next character.
* If set, an input line that is exactly {@code N*columns} wide will
* use {@code N} screen lines; otherwise it will use {@code N+1} lines.
* When the cursor position is the right margin of the last line
* (i.e. after {@code N*columns} normal characters), if this option
* it set, the cursor will be remain on the last line (line {@code N-1},
* zero-origin); if unset the cursor will be on the empty next line.
* Regardless, for all except the last screen line if the cursor is at
* the right margin, it will be shown at the start of the next line.
*/
DELAY_LINE_WRAP,
AUTO_PARAM_SLASH(true),
AUTO_REMOVE_SLASH(true),
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3200,7 +3200,7 @@ protected void redisplay(boolean flush) {
newLines = new ArrayList<>();
newLines.add(full);
} else {
newLines = full.columnSplitLength(size.getColumns(), true);
newLines = full.columnSplitLength(size.getColumns(), true, display.delayLineWrap());
}

List<AttributedString> rightPromptLines;
Expand All @@ -3224,7 +3224,7 @@ protected void redisplay(boolean flush) {
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);
sb.append(prompt);
sb.append(insertSecondaryPrompts(new AttributedString(buf.upToCursor()), secondaryPrompts, false));
List<AttributedString> promptLines = sb.columnSplitLength(size.getColumns());
List<AttributedString> promptLines = sb.columnSplitLength(size.getColumns(), false, display.delayLineWrap());
if (!promptLines.isEmpty()) {
cursorPos = size.cursorPos(promptLines.size() - 1,
promptLines.get(promptLines.size() - 1).columnLength());
Expand Down Expand Up @@ -3955,7 +3955,7 @@ private void update() {
// Compute displayed prompt
PostResult pr = computePost(possible, completion(), null, completed);
AttributedString text = insertSecondaryPrompts(AttributedStringBuilder.append(prompt, buf.toString()), new ArrayList<>());
int promptLines = text.columnSplitLength(size.getColumns()).size();
int promptLines = text.columnSplitLength(size.getColumns(), false, display.delayLineWrap()).size();
if (pr.lines >= size.getRows() - promptLines) {
int displayed = size.getRows() - promptLines - 1;
if (pr.selectedLine >= 0) {
Expand All @@ -3965,7 +3965,7 @@ private void update() {
topLine = pr.selectedLine - displayed + 1;
}
}
List<AttributedString> lines = pr.post.columnSplitLength(size.getColumns(), true);
List<AttributedString> lines = pr.post.columnSplitLength(size.getColumns(), true, display.delayLineWrap());
List<AttributedString> sub = new ArrayList<>(lines.subList(topLine, topLine + displayed));
sub.add(new AttributedStringBuilder()
.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.CYAN))
Expand Down Expand Up @@ -4068,7 +4068,7 @@ protected boolean doList(List<Candidate> possible, String completed, boolean run
// and redraw the line at the bottom
mergeCandidates(possible);
AttributedString text = insertSecondaryPrompts(AttributedStringBuilder.append(prompt, buf.toString()), new ArrayList<>());
int promptLines = text.columnSplitLength(size.getColumns()).size();
int promptLines = text.columnSplitLength(size.getColumns(), false, display.delayLineWrap()).size();
PostResult postResult = computePost(possible, null, null, completed);
int lines = postResult.lines;
int listMax = getInt(LIST_MAX, DEFAULT_LIST_MAX);
Expand Down Expand Up @@ -4103,7 +4103,7 @@ protected boolean doList(List<Candidate> possible, String completed, boolean run
}
post = () -> {
AttributedString t = insertSecondaryPrompts(AttributedStringBuilder.append(prompt, buf.toString()), new ArrayList<>());
int pl = t.columnSplitLength(size.getColumns()).size();
int pl = t.columnSplitLength(size.getColumns(), false, display.delayLineWrap()).size();
PostResult pr = computePost(cands, null, null, current);
if (pr.lines >= size.getRows() - pl) {
post = null;
Expand All @@ -4112,7 +4112,7 @@ protected boolean doList(List<Candidate> possible, String completed, boolean run
redisplay(false);
buf.cursor(oldCursor);
println();
List<AttributedString> ls = postResult.post.columnSplitLength(size.getColumns(), false);
List<AttributedString> ls = postResult.post.columnSplitLength(size.getColumns(), false, display.delayLineWrap());
Display d = new Display(terminal, false);
d.resize(size.getRows(), size.getColumns());
d.update(ls, -1);
Expand Down Expand Up @@ -4669,7 +4669,7 @@ public boolean mouse() {
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);
sb.append(prompt);
sb.append(insertSecondaryPrompts(new AttributedString(buf.upToCursor()), secondaryPrompts, false));
List<AttributedString> promptLines = sb.columnSplitLength(size.getColumns());
List<AttributedString> promptLines = sb.columnSplitLength(size.getColumns(), false, display.delayLineWrap());

int currentLine = promptLines.size() - 1;
int wantedLine = Math.max(0, Math.min(currentLine + event.getY() - cursor.getY(), secondaryPrompts.size()));
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/jline/utils/AttributedCharSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ public AttributedString columnSubSequence(int start, int stop) {
}

public List<AttributedString> columnSplitLength(int columns) {
return columnSplitLength(columns, false);
return columnSplitLength(columns, false, true);
}
public List<AttributedString> columnSplitLength(int columns, boolean includeNewlines) {
public List<AttributedString> columnSplitLength(int columns, boolean includeNewlines, boolean delayLineWrap) {
List<AttributedString> strings = new ArrayList<>();
int cur = 0;
int beg = cur;
Expand All @@ -236,7 +236,13 @@ public List<AttributedString> columnSplitLength(int columns, boolean includeNewl
int cp = codePointAt(cur);
int w = isHidden(cur) ? 0 : WCWidth.wcwidth(cp);
if (cp == '\n') {
strings.add(subSequence(beg, includeNewlines ? cur+1 : cur));
if (! delayLineWrap && col == columns) {
strings.add(subSequence(beg, cur));
strings.add(includeNewlines ? AttributedString.NEWLINE
: AttributedString.EMPTY);
}
else
strings.add(subSequence(beg, includeNewlines ? cur+1 : cur));
beg = cur + 1;
col = 0;
} else if ((col += w) > columns) {
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/org/jline/utils/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public Display(Terminal terminal, boolean fullscreen) {
this.cursorDownIsNewLine = "\n".equals(tput(Capability.cursor_down));
}

/** If cursor is at right margin, don't wrap immediately. */
/** If cursor is at right margin, don't wrap immediately.
* See {@link LineReader.Option#DELAY_LINE_WRAP}.
*/
public boolean delayLineWrap() {
return delayLineWrap;
}
Expand All @@ -68,7 +70,7 @@ public void resize(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.columns1 = columns + 1;
oldLines = AttributedString.join(AttributedString.EMPTY, oldLines).columnSplitLength(columns, true);
oldLines = AttributedString.join(AttributedString.EMPTY, oldLines).columnSplitLength(columns, true, delayLineWrap());
}
}

Expand Down Expand Up @@ -101,13 +103,6 @@ public void update(List<AttributedString> newLines, int targetCursorPos) {
cursorPos = 0;
reset = false;
}
if (! delayLineWrap()
&& newLines.get(newLines.size()-1).columnLength() == columns) {
List<AttributedString> tmpLines = new ArrayList<AttributedString>();
tmpLines.addAll(newLines);
tmpLines.add(AttributedString.EMPTY);
newLines = tmpLines;
}

// Detect scrolling
if (fullScreen && newLines.size() == oldLines.size() && canScroll) {
Expand Down Expand Up @@ -285,7 +280,8 @@ public void update(List<AttributedString> newLines, int targetCursorPos) {
}
lineIndex++;
boolean newWrap = ! newNL && lineIndex < newLines.size();
if (targetCursorPos + 1 == lineIndex * columns1 && newWrap)
if (targetCursorPos + 1 == lineIndex * columns1
&& (newWrap || ! delayLineWrap))
targetCursorPos++;
boolean atRight = (cursorPos - curCol) % columns1 == columns;
wrapNeeded = false;
Expand Down

0 comments on commit f5ad557

Please sign in to comment.