-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2002-2016, the original author or authors. | ||
* | ||
* This software is distributable under the BSD license. See the terms of the | ||
* BSD license in the documentation provided with this software. | ||
* | ||
* http://www.opensource.org/licenses/bsd-license.php | ||
*/ | ||
package org.jline.terminal; | ||
|
||
/** | ||
* Class holding the cursor position. | ||
* | ||
* @see Terminal#getCursorPosition(java.util.function.IntConsumer) | ||
*/ | ||
public class Cursor { | ||
|
||
private final int x; | ||
private final int y; | ||
|
||
public Cursor(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o instanceof Cursor) { | ||
Cursor c = (Cursor) o; | ||
return x == c.x && y == c.y; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return x * 31 + y; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Cursor[" + "x=" + x + ", y=" + y + ']'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/org/jline/terminal/impl/CursorSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (c) 2002-2016, the original author or authors. | ||
* | ||
* This software is distributable under the BSD license. See the terms of the | ||
* BSD license in the documentation provided with this software. | ||
* | ||
* http://www.opensource.org/licenses/bsd-license.php | ||
*/ | ||
package org.jline.terminal.impl; | ||
|
||
import org.jline.terminal.Cursor; | ||
import org.jline.terminal.Terminal; | ||
import org.jline.utils.Curses; | ||
import org.jline.utils.InfoCmp; | ||
|
||
import java.io.IOError; | ||
import java.io.IOException; | ||
import java.util.function.IntConsumer; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class CursorSupport { | ||
|
||
public static Cursor getCursorPosition(Terminal terminal, IntConsumer discarded) { | ||
try { | ||
String u6 = terminal.getStringCapability(InfoCmp.Capability.user6); | ||
String u7 = terminal.getStringCapability(InfoCmp.Capability.user7); | ||
if (u6 == null || u7 == null) { | ||
return null; | ||
} | ||
// Prepare parser | ||
boolean inc1 = false; | ||
StringBuilder patb = new StringBuilder(); | ||
int index = 0; | ||
while (index < u6.length()) { | ||
char ch; | ||
switch (ch = u6.charAt(index++)) { | ||
case '\\': | ||
switch (u6.charAt(index++)) { | ||
case 'e': | ||
case 'E': | ||
patb.append("\\x1b"); | ||
break; | ||
default: | ||
throw new IllegalArgumentException(); | ||
} | ||
break; | ||
case '%': | ||
ch = u6.charAt(index++); | ||
switch (ch) { | ||
case '%': | ||
patb.append('%'); | ||
break; | ||
case 'i': | ||
inc1 = true; | ||
break; | ||
case 'd': | ||
patb.append("([0-9]+)"); | ||
break; | ||
default: | ||
throw new IllegalArgumentException(); | ||
} | ||
break; | ||
default: | ||
switch (ch) { | ||
case '[': | ||
patb.append('\\'); | ||
break; | ||
} | ||
patb.append(ch); | ||
break; | ||
} | ||
} | ||
Pattern pattern = Pattern.compile(patb.toString()); | ||
// Output cursor position request | ||
Curses.tputs(terminal.writer(), u7); | ||
terminal.flush(); | ||
StringBuilder sb = new StringBuilder(); | ||
int start = 0; | ||
while (true) { | ||
int c = terminal.reader().read(); | ||
if (c < 0) { | ||
return null; | ||
} | ||
sb.append((char) c); | ||
Matcher matcher = pattern.matcher(sb.substring(start)); | ||
if (matcher.matches()) { | ||
int y = Integer.parseInt(matcher.group(1)); | ||
int x = Integer.parseInt(matcher.group(2)); | ||
if (inc1) { | ||
x--; | ||
y--; | ||
} | ||
if (discarded != null) { | ||
for (int i = 0; i < start; i++) { | ||
discarded.accept(sb.charAt(i)); | ||
} | ||
} | ||
return new Cursor(x, y); | ||
} else if (!matcher.hitEnd()) { | ||
start++; | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new IOError(e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters