4

I've looked at JLine, Lanterna, and others, but I'm not seeing a simple way to find the current caret position in the terminal with these tools. I've looked at a number of escape codes, tput, etc. But, I'm looking for the easiest way to get the current column and row where the caret is located with Java. Maybe I haven't found the right call in these libraries...

What's the easiest way to get the row and column of the caret in the terminal?

I'm looking for a pure textual library so that I can re-write the buffer. I'm aware of ansi escape codes and how to manipulate them to produce the effects I'm after. What I'm trying to do is make a Java prompt library in the vain of Inquirer.js for Node. It has a number of simple ways to get info from the user (lists, questions, split lists, etc). All of it text -- so all of it without a UI, and so non-swing. I don't want swing, I just want a decent terminal UI experience.

lucidquiet
  • 6,124
  • 7
  • 51
  • 88
  • Do you need this feature to work for muliple operating systems, or just one? I have a feeling it may take some native code – Sam Apr 10 '14 at 13:57
  • Using [ANSI control characters](http://www.termsys.demon.co.uk/vtansi.htm) looks promising if you're not worried about supporting Windows – Sam Apr 10 '14 at 14:34
  • @Sam I'm looking for *nixy version. I work on a Mac, so if it was only that platform then fair enough. I realize getting this to work on would likely be a huge pain, so it's not a criteria. – lucidquiet Apr 10 '14 at 14:38
  • @Sam ANSI control characters get me close, but I don't see how they would get me the caret position (row, col)... so maybe I missed something...? – lucidquiet Apr 10 '14 at 14:39
  • Outputting `[6n` should return back for input something like `[{Row};{Column}R` where `` depends on what kind of terminal you are using. A bash-style solution can be found [here](http://stackoverflow.com/questions/2575037/how-to-get-the-cursor-position-in-bash). – Sam Apr 10 '14 at 15:00
  • @Sam So the only remaining part is to figure out how to get Java to run the control character and subsequently read it. I suppose writing that control sequence to Standard Out and then parsing from Standard In the [{Row};{Column}R might work -- have to try it...(I'm doubtful, but worth a shot). – lucidquiet Apr 10 '14 at 16:10
  • If you google around, you may notice that people actually have a lot of trouble doing this in shell scripts and in C. The chances of being able to do it in Java are quite low. – krispy Apr 15 '14 at 15:23
  • @Krispy Yup. I did google around for it. And I did realize the chances were low. Didn't mean it was impossible or that I shouldn't ask. I also felt it was worth the bounty... But no luck. – lucidquiet Apr 16 '14 at 07:03
  • You didn't mention what you didn't like about things like Charva and JCurses. Are you entirely opposed to using curses? – krispy Apr 16 '14 at 12:47
  • @Krispy no I'm not apposed to using any pure java library, I just didn't find where it provided the Current Position (Row,Column). – lucidquiet Apr 16 '14 at 17:27
  • Well, it's not exactly what you're looking for, but Charva is actually a full terminal-based implementation of the Swing api. So it kind of has to have a way to get the cursor position. It could be way overkill, though. – krispy Apr 17 '14 at 19:23

2 Answers2

0

Edit2

With the http://docs.oracle.com/javase/7/docs/api/javax/swing/text/Caret.html Caret Interface, you can create a CaretListener Object in order to find a caret position.

So you would have to create a new CaretListener that responds to the GUI, with the getDot() method.

This might help... with code anyway. http://bestjavapractices.blogspot.com.au/2011/11/get-current-caret-position-in.html

Now at the moment that would only work on a GUI/SwingComponents and I'm not sure if that would work for a terminal application ,which is what you want, where I assume you would be using command line arguments and such to get things to work.

I don't think that you could do this as I think the terminal is really just printing out the output, but I will keep checking for a little while anyway.

If you could tell me what you are trying to use the caret for that would help in my efforts.

Hope this helps.

If it doesn't you may need to look through some more Text Toolkits, such as

BoydyBoydy
  • 159
  • 1
  • 9
  • Sorry, by cursor I didn't mean the mouse cursor... I meant the caret. My mistake. The row & column in the textual sense. – lucidquiet Apr 07 '14 at 00:28
  • http://docs.oracle.com/javase/7/docs/api/javax/swing/text/Caret.html this might help. I'll check properly soon and reply! – BoydyBoydy Apr 07 '14 at 01:31
  • I'm looking for a pure textual library so that I can re-write the buffer. I'm aware of ansi escape codes and how to manipulate them to produce the effects I'm after. What I'm trying to do is make a Java prompt library in the vain of Inquirer.js for Node. It has a number of simple ways to get info from the user (lists, questions, split lists, etc). All of it text -- so all of it without a UI, and so non-swing. I don't want swing, I just want a decent terminal UI experience. – lucidquiet Apr 07 '14 at 04:16
  • ok sorry, i hope someone else with more knowledge is able to help you. Good Luck! – BoydyBoydy Apr 07 '14 at 04:31
0

Current version of Lanterna can read the cursor position using ANSI CSI: https://github.com/mabe02/lanterna/blob/master/src/main/java/com/googlecode/lanterna/terminal/ansi/ANSITerminal.java#L266

And here you can find simple Java solution how to send the ANSI command and read from System.in reported cursor position. How to read the cursor position given by the terminal (ANSI Device Status Report) in JAVA

Sergiy Kozachenko
  • 1,399
  • 11
  • 31