Groovy: How to write on one line in the same position on the screen? (Windows)

I want to show the% advance in groovy, so I want to write in the same position, which means that instead of seeing:

1% 2% 3% ... 

The user will see that the numbers change in one place. How can I do it? (I work on windows)

+6
source share
4 answers

I often do this using carriage return without a string:

 printf "%5d\r", loopval 

Each time through your loop, printing starts again at the beginning of the line.

It can get a little dirty if any other messages should be printed during this event, especially if the other message contains newlines. But this is a cheap and dirty solution.

+4
source

You can use the jline that comes with Groovy:

 (1..5).each { print "Done $it of 5" Thread.sleep( 1000 ) print jline.ANSIBuffer.ANSICodes.left( 9999 ) } 

As long as your console supports ANSI escape sequences, this should work ...

PS: I used 9999 because (as the documentation says on the left )

If n is greater than or equal to the current cursor column, the cursor moves to the first column

+2
source

For everyone who comes across this, I found the following optimal solution:

 (0..100).each { print "\r$it%" } println() 
+2
source

as Danny Y. already said, the line will work, the inverse areas will also do the trick:

 (0..100).each { print "\b"*20+it+"%" sleep 100 } 

I had to rewrite multi-line output - the solution I found was

 print "\r\n"*80 

to scroll through all the old content from the screen is not a good solution, but it works on a Windows shell :-)

+1
source

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


All Articles