Skip to content

Commit

Permalink
Mapping negative absolute positions to the first column/row
Browse files Browse the repository at this point in the history
  • Loading branch information
romge committed Dec 26, 2020
1 parent 8e1b67b commit f8fa335
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/main/java/org/fusesource/jansi/Ansi.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,26 +550,26 @@ public Ansi a(Attribute attribute) {
}

/**
* Moves the cursor to row n, column m.
* The values are 1-based, and default to 1 (top left corner) if omitted.
* A sequence such as CSI ;5H is a synonym for CSI 1;5H as well as CSI 17;H is the same as CSI 17H and CSI 17;1H
* Moves the cursor to row n, column m. The values are 1-based.
* Any values less than 1 are mapped to 1.
*
* @param row row (1-based) from top
* @param row row (1-based) from top
* @param column column (1 based) from left
* @return this Ansi instance
*/
public Ansi cursor(final int row, final int column) {
return appendEscapeSequence('H', row, column);
return appendEscapeSequence('H', Math.max(1, row), Math.max(1, column));
}

/**
* Moves the cursor to column n. If n is negative it is not moved.
* Moves the cursor to column n. The parameter n is 1-based.
* If n is less than 1 it is moved to the first column.
*
* @param x the index of the column to move to
* @param x the index (1-based) of the column to move to
* @return this Ansi instance
*/
public Ansi cursorToColumn(final int x) {
return x >= 0 ? appendEscapeSequence('G', x) : this;
return appendEscapeSequence('G', Math.max(1, x));
}

/**
Expand Down Expand Up @@ -641,7 +641,7 @@ public Ansi cursorDownLine() {
* @return this Ansi instance
*/
public Ansi cursorDownLine(final int n) {
return n > 0 ? appendEscapeSequence('E', n) : n < 0 ? cursorUpLine(-n) : this;
return n < 0 ? cursorUpLine(-n) : appendEscapeSequence('E', n);
}

/**
Expand All @@ -661,7 +661,7 @@ public Ansi cursorUpLine() {
* @return this Ansi instance
*/
public Ansi cursorUpLine(final int n) {
return n > 0 ? appendEscapeSequence('F', n) : n < 0 ? cursorDownLine(-n) : this;
return n < 0 ? cursorDownLine(-n) : appendEscapeSequence('F', n);
}

public Ansi eraseScreen() {
Expand All @@ -681,11 +681,11 @@ public Ansi eraseLine(final Erase kind) {
}

public Ansi scrollUp(final int rows) {
return rows > 0 ? appendEscapeSequence('S', rows) : this;
return rows > 0 ? appendEscapeSequence('S', rows) : rows < 0 ? scrollDown(-rows) : this;
}

public Ansi scrollDown(final int rows) {
return rows > 0 ? appendEscapeSequence('T', rows) : this;
return rows > 0 ? appendEscapeSequence('T', rows) : rows < 0 ? scrollUp(-rows) : this;
}

public Ansi saveCursorPosition() {
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/fusesource/jansi/AnsiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ public void testClone() throws CloneNotSupportedException {

@ParameterizedTest
@CsvSource({
"-1,-1,ESC[-1;-1H", "-1,0,ESC[-1;0H", "-1,1,ESC[-1;1H", "-1,2,ESC[-1;2H",
"0,-1,ESC[0;-1H", "0,0,ESC[0;0H", "0,1,ESC[0;1H", "0,2,ESC[0;2H",
"1,-1,ESC[1;-1H", "1,0,ESC[1;0H", "1,1,ESC[1;1H", "1,2,ESC[1;2H",
"2,-1,ESC[2;-1H", "2,0,ESC[2;0H", "2,1,ESC[2;1H", "2,2,ESC[2;2H"
"-1,-1,ESC[1;1H", "-1,0,ESC[1;1H", "-1,1,ESC[1;1H", "-1,2,ESC[1;2H",
"0,-1,ESC[1;1H", "0,0,ESC[1;1H", "0,1,ESC[1;1H", "0,2,ESC[1;2H",
"1,-1,ESC[1;1H", "1,0,ESC[1;1H", "1,1,ESC[1;1H", "1,2,ESC[1;2H",
"2,-1,ESC[2;1H", "2,0,ESC[2;1H", "2,1,ESC[2;1H", "2,2,ESC[2;2H"
})
public void testCursor(int x, int y, String expected) {
assertAnsi(expected, new Ansi().cursor(x, y));
}

@ParameterizedTest
@CsvSource({"-2,''", "-1,''", "0,ESC[0G", "1,ESC[1G", "2,ESC[2G"})
@CsvSource({"-1,ESC[1G", "0,ESC[1G", "1,ESC[1G", "2,ESC[2G"})
public void testCursorToColumn(int x, String expected) {
assertAnsi(expected, new Ansi().cursorToColumn(x));
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public void testCursorDownLine() {
}

@ParameterizedTest
@CsvSource({"-2,ESC[2F", "-1,ESC[1F", "0,''", "1,ESC[1E", "2,ESC[2E"})
@CsvSource({"-2,ESC[2F", "-1,ESC[1F", "0,ESC[0E", "1,ESC[1E", "2,ESC[2E"})
public void testCursorDownLine(int n, String expected) {
assertAnsi(expected, new Ansi().cursorDownLine(n));
}
Expand All @@ -125,7 +125,7 @@ public void testCursorUpLine() {
}

@ParameterizedTest
@CsvSource({"-2,ESC[2E", "-1,ESC[1E", "0,''", "1,ESC[1F", "2,ESC[2F"})
@CsvSource({"-2,ESC[2E", "-1,ESC[1E", "0,ESC[0F", "1,ESC[1F", "2,ESC[2F"})
public void testCursorUpLine(int n, String expected) {
assertAnsi(expected, new Ansi().cursorUpLine(n));
}
Expand Down

0 comments on commit f8fa335

Please sign in to comment.