Skip to content

Commit

Permalink
nano command: navigation with arrow keys on unwrapped line
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jul 30, 2019
1 parent d24e4e6 commit 2a8493d
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected class Buffer {
List<String> lines;

int firstLineToDisplay;
int firstColumnToDisplay;
int firstColumnToDisplay = 0;
int offsetInLineToDisplay;

int line;
Expand Down Expand Up @@ -303,6 +303,13 @@ boolean isBreakable(char ch) {
}

void moveToChar(int pos) {
if (!wrapping) {
if (pos > column && pos - firstColumnToDisplay + 1 > width()) {
firstColumnToDisplay = offsetInLine + column - 6;
} else if (pos < column && firstColumnToDisplay + 5 > pos) {
firstColumnToDisplay = Math.max(0, firstColumnToDisplay - width() + 5);
}
}
offsetInLine = prevLineOffset(line, pos + 1).get();
column = pos - offsetInLine;
}
Expand Down Expand Up @@ -348,7 +355,7 @@ boolean moveLeft(int chars) {
moveToChar(offsetInLine + column - 1);
} else if (line > 0) {
line--;
moveToChar(wrapping ? length(getLine(line), tabs) : Math.min(length(getLine(line), tabs), width()));
moveToChar(length(getLine(line), tabs));
} else {
bof();
ret = false;
Expand All @@ -365,22 +372,24 @@ boolean moveRight(int chars) {
}

int width() {
return size.getColumns() - (printLineNumbers ? 8 : 0) - (wrapping ? 0 : 2);
return size.getColumns() - (printLineNumbers ? 8 : 0) - (wrapping ? 0 : 1) - (firstColumnToDisplay > 0 ? 1 : 0);
}

boolean moveRight(int chars, boolean fromBeginning) {
if (fromBeginning) {
firstColumnToDisplay = 0;
offsetInLine = 0;
column = 0;
chars = chars <= length(getLine(line), tabs) ? chars : length(getLine(line), tabs);
}
boolean ret = true;
while (--chars >= 0) {
int len = wrapping ? length(getLine(line), tabs) : Math.min(length(getLine(line), tabs), width());
int len = length(getLine(line), tabs);
if (offsetInLine + column + 1 <= len) {
moveToChar(offsetInLine + column + 1);
} else if (getLine(line + 1) != null) {
line++;
firstColumnToDisplay = 0;
offsetInLine = 0;
column = 0;
} else {
Expand Down Expand Up @@ -432,7 +441,7 @@ void moveDisplayDown(int lines) {
// Adjust cursor
while (--lines >= 0) {
int lastLineToDisplay = firstLineToDisplay;
if (firstColumnToDisplay > 0 || !wrapping) {
if (!wrapping) {
lastLineToDisplay += height - 1;
} else {
int off = offsetInLineToDisplay;
Expand Down Expand Up @@ -477,8 +486,9 @@ void moveDisplayUp(int lines) {

private void cursorDown(int lines) {
// Adjust cursor
firstColumnToDisplay = 0;
while (--lines >= 0) {
if (firstColumnToDisplay > 0 || !wrapping) {
if (!wrapping) {
if (getLine(line + 1) != null) {
line++;
offsetInLine = 0;
Expand Down Expand Up @@ -508,8 +518,9 @@ private void cursorDown(int lines) {
}

private void cursorUp(int lines) {
firstColumnToDisplay = 0;
while (--lines >= 0) {
if (firstColumnToDisplay > 0 || !wrapping) {
if (!wrapping) {
if (line > 0) {
line--;
column = Math.min(length(getLine(line), tabs) - offsetInLine, wantedColumn);
Expand Down Expand Up @@ -550,7 +561,7 @@ void ensureCursorVisible() {
int off = offsetInLineToDisplay;
while (true) {
if (cur < line || off < offsetInLine) {
if (firstColumnToDisplay > 0 || !wrapping) {
if (!wrapping) {
cursor += rwidth;
cur++;
} else {
Expand All @@ -564,7 +575,12 @@ void ensureCursorVisible() {
}
}
} else if (cur == line) {
cursor += column;
if (!wrapping && column > firstColumnToDisplay + width()) {
while (column > firstColumnToDisplay + width()) {
firstColumnToDisplay += width();
}
}
cursor += column - firstColumnToDisplay + (firstColumnToDisplay > 0 ? 1 : 0);
break;
} else {
throw new IllegalStateException();
Expand Down Expand Up @@ -756,14 +772,28 @@ List<AttributedString> getDisplayedLines(int nbLines) {
}
if (curLine >= lines.size()) {
// Nothing to do
} else if (firstColumnToDisplay > 0 || !wrapping) {
} else if (!wrapping) {
AttributedString disp = new AttributedString(getLine(curLine));
if (disp.columnLength() - firstColumnToDisplay >= width) {
highlightDisplayedLine(curLine, firstColumnToDisplay
, firstColumnToDisplay + width - cut.columnLength(), line);
line.append(cut);
if (this.line == curLine) {
int cutCount = 1;
if (firstColumnToDisplay > 0) {
line.append(cut);
cutCount = 2;
}
if (disp.columnLength() - firstColumnToDisplay >= width - (cutCount - 1)*cut.columnLength()) {
highlightDisplayedLine(curLine, firstColumnToDisplay
, firstColumnToDisplay + width - cutCount*cut.columnLength(), line);
line.append(cut);
} else {
highlightDisplayedLine(curLine, firstColumnToDisplay, disp.columnLength(), line);
}
} else {
highlightDisplayedLine(curLine, firstColumnToDisplay, Integer.MAX_VALUE, line);
if (disp.columnLength() >= width) {
highlightDisplayedLine(curLine, 0, width - cut.columnLength(), line);
line.append(cut);
} else {
highlightDisplayedLine(curLine, 0, disp.columnLength(), line);
}
}
curLine++;
} else {
Expand Down Expand Up @@ -811,7 +841,7 @@ public int getDisplayedCursor() {
int off = offsetInLineToDisplay;
while (true) {
if (cur < line || off < offsetInLine) {
if (firstColumnToDisplay > 0 || !wrapping) {
if (!wrapping) {
cursor += rwidth;
cur++;
} else {
Expand All @@ -825,7 +855,12 @@ public int getDisplayedCursor() {
}
}
} else if (cur == line) {
cursor += column;
if (!wrapping && column > firstColumnToDisplay + width()) {
while (column > firstColumnToDisplay + width()) {
firstColumnToDisplay += width();
}
}
cursor += column - firstColumnToDisplay + (firstColumnToDisplay > 0 ? 1 : 0);
break;
} else {
throw new IllegalStateException();
Expand Down Expand Up @@ -967,7 +1002,6 @@ void nextSearch() {
}
line = newLine;
moveRight(newPos, true);
ensureCursorVisible();
} else {
setMessage("\"" + searchTerm + "\" not found");
}
Expand Down

0 comments on commit 2a8493d

Please sign in to comment.