I want to output all of my STDOUT to a single line....dont want to scroll

S

seancsnyder

I want my program to simply output to a single line in STDOUT. In
other words i want to print a line, then 'backspace' that line and
print somethign new on the same line. I do not want the terminal
window to scroll.

Any ideas? CAn this be done?
 
D

David Squire

I want my program to simply output to a single line in STDOUT. In
other words i want to print a line, then 'backspace' that line and
print somethign new on the same line. I do not want the terminal
window to scroll.

Any ideas? CAn this be done?

You could do something like this:

----

#!/usr/bin/perl

use strict;
use warnings;

$| = 1; # output autoflush mode
my $last_message_length = 0;
while (my $message = <DATA>) {
chomp $message;
print "\r";
print ' ' x $last_message_length;
print "\r";
print $message; # must not contain \n or \r
sleep(1); # for example
$last_message_length = length $message;
}

__DATA__
First message
Second message
The third message is really very much longer. In fact, it is more than
80 characters
The fourth message is shorter

----

But as you will most likely see if you run this, there are problems if
the message is longer than your terminal width. You could also look into
more robust solutions using, for example, the Curses module from cpan.


DS
 
D

David Squire

(e-mail address removed) wrote:

[top-posting corrected. Please don't do that.]
David said:
You could do something like this:

----

#!/usr/bin/perl

use strict;
use warnings;

$| = 1; # output autoflush mode
[snip]

what does this do?

$| = 1; # output autoflush mode

What the comment says! See:

perldoc perlvar


DS
 
T

Ted Zlatanov

I want my program to simply output to a single line in STDOUT. In
other words i want to print a line, then 'backspace' that line and
print somethign new on the same line. I do not want the terminal
window to scroll.

Besides the \r technique shown in this thread, you could also clear
the screen.

On Unix machines, you can often do it with the `/usr/bin/clear'
program. IIRC on Windows it's `cls' or something similar.

This is definitely slower than \r, so if your data updates quickly it
won't look nice. It does look better in some circumstances, though
(e.g. your data is longer than the screen width).

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top