beryx / text-io

A library for creating interactive console applications in Java
http://text-io.beryx.org/
Apache License 2.0
342 stars 45 forks source link

Any way to get TextTerminal.print method to handle \r ? #8

Closed tonypiazza closed 6 years ago

tonypiazza commented 6 years ago

I would like to use the print method of TextTerminal to output a line that is continually updated. In past, I was able to accomplish this using \r at the end of printf format string. So far, I have been unable to get this working with TextIO. Is there any way to accomplish this?

Thanks again,

-Tony

tonypiazza commented 6 years ago

It seems I am able to do this when running outside my IDE (Eclipse) and using the rawPrint method. In this case, the type of the actual TextTerminal is JLineTextTerminal. But running inside my IDE, the same code does not work because we get a SwingTextTerminal. Is there any way to support this behavior when using the SwingTextTerminal?

Thanks,

-Tony

siordache commented 6 years ago

Currently this is not supported. The javadoc of rawPrint states that the behavior is undefined if the string contains line separators. I will add support for resetting a line in the next release.

siordache commented 6 years ago

I released text-io-3.0.0-alpha-1, which adds support for resetting the current line. Example usage:

TextTerminal<?> textTerm = TextIoFactory.getTextTerminal();
while(true) {
    textTerm.print("" + Math.random());
    try {
    Thread.sleep(1000L);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    textTerm.resetLine();
}

Please give it a try.

tonypiazza commented 6 years ago

I tried using the resetLine feature in 3.0.0-alpha1 but found it caused a lot of flicker. I recorded 2 videos comparing the difference between using rawPrintf with \r and resetLine:

Example using rawPrintf with \r https://www.dropbox.com/s/9gse785aclk5v72/textio-rawPrintf.mp4?dl=0

Example using resetLine https://www.dropbox.com/s/35enofpujad1fjv/textio-resetLine.mp4?dl=0

Please let me know if you have any trouble accessing the videos.

Thanks again,

-Tony

On Thu, Nov 23, 2017 at 4:26 PM, Serban Iordache notifications@github.com wrote:

I released text-io-3.0.0-alpha-1, which adds support for resetting the current line. Example usage:

TextTerminal<?> textTerm = TextIoFactory.getTextTerminal(); while(true) { textTerm.print("" + Math.random()); try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } textTerm.resetLine(); }

Please give it a try.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/beryx/text-io/issues/8#issuecomment-346704527, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCy18MPYXe1tgOF6XdK7Pr1BFovk_E4ks5s5fENgaJpZM4QoOJJ .

siordache commented 6 years ago

I can reproduce the flickering if there is a delay between the call to resetLine and the call to print. This gist illustrates the problem.

Make sure that in your code there are no operations that may introduce some delay between the call to resetLine and the call to print. Instead of:

textTerm.resetLine();
textTerm.print(getData());

try to use:

String infoText = getData();
textTerm.resetLine();
textTerm.print(infoText);

Does the flickering disappear?

tonypiazza commented 6 years ago

It does not make any difference. My code looks like this:

metrics.addFlight(flight);

terminal.resetLine();

terminal.printf("%,10d\t%,10d\t%,10d\t%,10d",

            metrics.getTotalFlights(),

            metrics.getTotalCancelled(),

            metrics.getTotalDiverted(),

            metrics.getAirports().size()

);

For now I will continue using \r with rawPrintf.

Thanks again for all your help.

-Tony

On Fri, Nov 24, 2017 at 4:37 AM, Serban Iordache notifications@github.com wrote:

I can reproduce the flickering if there is a delay between the call to resetLine and the call to print. This gist https://gist.github.com/siordache/e0832eb09d7d09cde631f3840575761d illustrates the problem.

Make sure that in your code there are no operations that may introduce some delay between the call to resetLine and the call to print. Instead of:

textTerm.resetLine(); textTerm.print(getData());

try to use:

String infoText = getData(); textTerm.resetLine(); textTerm.print(infoText);

Does the flickering disappear?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/beryx/text-io/issues/8#issuecomment-346797604, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCy1wqFDAwYcZFGH5pfjqaiRe1KrJyZks5s5pxngaJpZM4QoOJJ .

siordache commented 6 years ago

I suspect that the metrics.getXXX() methods introduce a small delay. Try this please:

String infoText = String.format("%,10d\t%,10d\t%,10d\t%,10d",
                metrics.getTotalFlights(),
                metrics.getTotalCancelled(),
                metrics.getTotalDiverted(),
                metrics.getAirports().size()
);
textTerm.resetLine();
textTerm.print(infoText);

If this still doesn't solve the flickering problem, I will try to come up with another solution in the next release.

tonypiazza commented 6 years ago

That didn't make any difference.

Thanks,

-Tony

On Sat, Nov 25, 2017 at 2:34 AM, Serban Iordache notifications@github.com wrote:

I suspect that the metrics.getXXX() methods introduce a small delay. Try this please:

String infoText = String.format("%,10d\t%,10d\t%,10d\t%,10d", metrics.getTotalFlights(), metrics.getTotalCancelled(), metrics.getTotalDiverted(), metrics.getAirports().size() ); textTerm.resetLine(); textTerm.print(infoText);

If this still doesn't solve the flickering problem, I will try to come up with another solution in the next release.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/beryx/text-io/issues/8#issuecomment-346926907, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCy1_5jkMSvW-reIcdYJA3k2MzOuKwDks5s59EdgaJpZM4QoOJJ .

siordache commented 6 years ago

Do you also have flicker with the SwingTextTerminal?

tonypiazza commented 6 years ago

It doesn't work at all when using SwingTextTerminal. Only with JLineTextTerminal.

On Sat, Nov 25, 2017 at 5:21 PM, Serban Iordache notifications@github.com wrote:

Do you also have flicker with the SwingTextTerminal?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/beryx/text-io/issues/8#issuecomment-346972763, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCy1yWGColUw8N_vDE8VLwZPUwtgDhLks5s6KEXgaJpZM4QoOJJ .

siordache commented 6 years ago

What means it doesn't work at all? Could you please make a video with an example using resetLine with SwingTextTerminal? What operation system do you have?

tonypiazza commented 6 years ago

I have attached a video of using resetLine with SwingTextTerminal. As you can see, the flicker is worse.

Thanks,

-Tony

On Sat, Nov 25, 2017 at 6:18 PM, Serban Iordache notifications@github.com wrote:

What means it doesn't work at all? Could you please make a video with an example using resetLine with SwingTextTerminal? What operation system do you have?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/beryx/text-io/issues/8#issuecomment-346975020, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCy1_7tDqKeHZlzvkCe7vfKGilPpsctks5s6K5mgaJpZM4QoOJJ .

siordache commented 6 years ago

In version 3.0.0-alpha-2 I added the method moveToLineStart to TextTerminal. Replace resetLine with moveToLineStart in your program and see how it works. For JLineTextTerminal, the internal implementation just calls rawPrint("\r"), so I expect it to work properly in your application. However, I am curious how your program behaves with a SwingTextTerminal.

tonypiazza commented 6 years ago

Using moveLineToStart gives the same result, which is lots of flicker.

Hope you have a good week ahead.

Thanks,

-Tony

On Sun, Nov 26, 2017 at 4:18 PM, Serban Iordache notifications@github.com wrote:

In version 3.0.0-alpha-2 I added the method moveToLineStart to TextTerminal. Replace resetLine with moveToLineStart in your program and see how it works. For JLineTextTerminal, the internal implementation just calls rawPrint("\r"), so I expect it to work properly in your application. However, I am curious how your program behaves with a SwingTextTerminal.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/beryx/text-io/issues/8#issuecomment-347043152, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCy1_WuzWuTWMB-N0puJjPCEaVyzYobks5s6eOngaJpZM4QoOJJ .

siordache commented 6 years ago

Do you have flicker with moveToLineStart even when using the JLineTextTerminal? That's really strange, because, as I said, for JLineTextTerminal I just call rawPrint("\r").

tonypiazza commented 6 years ago

There is less flicker when using moveLineToStart with JLineTextTerminal.

On Mon, Nov 27, 2017 at 12:37 AM, Serban Iordache notifications@github.com wrote:

Do you have flicker with moveLineToStart even when using the JLineTextTerminal? That's really strange, because, as I said, for JLineTextTerminal I just call rawPrint("\r").

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/beryx/text-io/issues/8#issuecomment-347091552, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCy132Mwm3Ad3lT8qKTDCkjpaL6DZe5ks5s6li7gaJpZM4QoOJJ .

siordache commented 6 years ago

Ok, thanks. I think I have a solution to remove the flicker completely for JLineTextTerminal. I will implement it in the next release.

Have a nice week! Serban

siordache commented 6 years ago

@tonypiazza Can you please grab the release 3.0.0-alpha-3 and test again with moveLineToStart? This is the last time I bother you, promise!

I'm interested in the behavior of both JLineTextTerminal and SwingTextTerminal.

Thanks, Serban

tonypiazza commented 6 years ago

Serban,

You are definitely not bothering me! I really appreciate your extra effort on this feature. I tested it in both JLineTextTerminal and SwingTextTerminal and it works much better. There is almost no flicker. You have solved the problem! :-)

Thanks again for all your work on this project.

-Tony

On Thu, Nov 30, 2017 at 3:05 PM, Serban Iordache notifications@github.com wrote:

Can you please grab the release 3.0.0-alpha-3 and test again with moveLineToStart? This is the last time I bother you, promise!

I'm interested in the behavior of both JLineTextTerminal and SwingTextTerminal.

Thanks, Serban

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/beryx/text-io/issues/8#issuecomment-348320415, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCy1yplmr0fxLdvE4a33HyR456mmUmvks5s7xh_gaJpZM4QoOJJ .

siordache commented 6 years ago

Thanks Tony, I'm glad I could help.