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

Discussion in 'Perl Misc' started by seancsnyder@gmail.com, Sep 11, 2006.

  1. Guest

    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?
    , Sep 11, 2006
    #1
    1. Advertising

  2. David Squire Guest

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

    wrote:
    > 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
    David Squire, Sep 11, 2006
    #2
    1. Advertising

  3. Guest

    what does this do?

    $| = 1; # output autoflush mode

    thanks
    -sean


    David Squire wrote:
    > wrote:
    > > 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
    , Sep 11, 2006
    #3
  4. David Squire Guest

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

    wrote:

    [top-posting corrected. Please don't do that.]

    > David Squire wrote:
    >> wrote:
    >>> 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


    [snip]

    > what does this do?
    >
    > $| = 1; # output autoflush mode
    >


    What the comment says! See:

    perldoc perlvar


    DS
    David Squire, Sep 11, 2006
    #4
  5. Ted Zlatanov Guest

    On 11 Sep 2006, wrote:

    > 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
    Ted Zlatanov, Sep 13, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    1
    Views:
    13,602
    Oisin
    Mar 24, 2006
  2. chaser7016
    Replies:
    1
    Views:
    567
    Neredbojias
    Sep 18, 2005
  3. Replies:
    37
    Views:
    2,033
    Flash Gordon
    Jan 9, 2006
  4. Eric Layman
    Replies:
    3
    Views:
    615
    Rad [Visual C# MVP]
    Apr 14, 2007
  5. ela
    Replies:
    12
    Views:
    331
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page