Prefilling a string with a string

Using JLine (or JLine2), is it possible to call readline on the ConsoleReader and have, in addition to the standard request, a buffer pre-filled with a line of my choice?

I tried to do, for example:

 reader.getCursorBuffer().write("Default"); reader.readLine("Prompt> "); 

This seems to be actually written to the buffer, but the prompt only displays in the line. If I press enter, readline returns "Default" , as expected. If I clear the screen, the buffer is redrawn and displayed correctly.

I understand that I need to somehow call reader.redrawLine() right after calling readline . The latter, however, blocks, which makes it difficult (not impossible, but, of course, it is wrong to use the second thread for this).

+6
source share
3 answers

Today I came across this precedent.

This hacked a bit, but I was able to preload the text into the JLine buffer and then let the user edit it by doing the following:

 String preloadReadLine(ConsoleReader reader, String prompt, String preload) throws IOException { reader.resetPromptLine(prompt, preload, 0); reader.print("\r"); return reader.readLine(prompt); } 

Yes, printing \r is a hack, but it seems to work.

I am using JLine-2.13.

+1
source

I managed to do this with the help of a thread (yes, it doesn’t feel right, but I didn’t find another one).

I found inspiration from code found in JLine itself , which also uses thread for similar purposes.

In Scala:

  val thr = new Thread() { override def run() = { reader.putString("Default") reader.flush() // Another way is: // reader.getCursorBuffer.write("Default") // writes into the buffer without displaying // out.print("D*f*ult") // here you can choose to display something different // reader.flush() } } thr.setPriority(Thread.MAX_PRIORITY) thr.setDaemon(true) thr.start() 
+1
source

I think you want either resetPromptLine or putStream if you already have the prompt set.

Don’t get carried away with your question, but I can’t understand how easy it is to print a line replacing the invitation (supposedly or visually by clicking on the hint down above the message above it).

0
source

Source: https://habr.com/ru/post/924572/


All Articles