(quickie) print in place?

Discussion in 'Perl Misc' started by Damian, Feb 10, 2004.

  1. Damian

    Damian Guest

    was wondering how exactly do you print(...) in place? Like the percent
    counter for installers like linux's rpm. In other words so the output
    keeps going to the same spot in stead of advancing the cursor.

    If this is something only on Linux/Unix thats fine, as thats the only
    place I really need to do this. Though portability never hurts.

    Many thanks.
    Damian, Feb 10, 2004
    #1
    1. Advertising

  2. Damian

    gnari Guest

    "Damian" <> wrote in message
    news:c09aeu$14hlvn$-berlin.de...
    > was wondering how exactly do you print(...) in place? Like the percent
    > counter for installers like linux's rpm. In other words so the output
    > keeps going to the same spot in stead of advancing the cursor.
    >
    > If this is something only on Linux/Unix thats fine, as thats the only
    > place I really need to do this. Though portability never hurts.


    "\r"

    gnari
    gnari, Feb 10, 2004
    #2
    1. Advertising

  3. Damian

    Ben Morrow Guest

    "Damian" <> wrote:
    > was wondering how exactly do you print(...) in place? Like the percent
    > counter for installers like linux's rpm. In other words so the output
    > keeps going to the same spot in stead of advancing the cursor.
    >
    > If this is something only on Linux/Unix thats fine, as thats the only
    > place I really need to do this. Though portability never hurts.


    Term::ANSIScreen.

    Ben

    --
    Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
    From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
    (Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
    /Alcestis/) [ flame, and falls out of sight. ]
    Ben Morrow, Feb 10, 2004
    #3
  4. Damian

    Damian Guest

    gnari wrote:
    > "Damian" <> wrote in
    > message news:c09aeu$14hlvn$-berlin.de...
    >> was wondering how exactly do you print(...) in place? Like the
    >> percent counter for installers like linux's rpm. In other words so
    >> the output keeps going to the same spot in stead of advancing the
    >> cursor.
    >>
    >> If this is something only on Linux/Unix thats fine, as thats the only
    >> place I really need to do this. Though portability never hurts.

    >
    > "\r"
    >
    > gnari


    This this a good way of doing it? This is what I just tried:

    #!/usr/local/bin/perl -w

    use strict;

    $| = 1; # Needs to be non zero so you cna see print's in a loop.

    for my $i (0..10) {
    my $c = $i * 10;
    print "Percent complete: $c %\r";

    sleep(1); # Sleep for 1 second.
    }
    print "Percent complete: Done!\n"


    I notice though that that if you hit a key while it's looping (logged
    into ssh), it shifts the whole thing over, then the next itereation
    comes along and rewites that portion, ending up with the right hand bit
    of text from the shifted part rtemaining there. Is the only way to make
    sure that everything up to the end of thel ine is cleared, short of
    manually padding it with spaces, which would require I find out the
    column width and then find out how much space is left. Theres gotta be a
    beter way to do this.
    Damian, Feb 10, 2004
    #4
  5. Damian

    Damian Guest

    Damian wrote:
    > gnari wrote:
    > I notice though that that if you hit a key while it's looping (logged
    > into ssh), it shifts the whole thing over, then the next itereation
    > comes along and rewites that portion, ending up with the right hand
    > bit of text from the shifted part rtemaining there. Is the only way
    > to make sure that everything up to the end of thel ine is cleared,
    > short of manually padding it with spaces, which would require I find
    > out the column width and then find out how much space is left. Theres
    > gotta be a beter way to do this.


    Or would it be bettter to supress input / local echo? Is this even
    possible and a good way to go about this?
    Damian, Feb 10, 2004
    #5
  6. Damian

    Ben Morrow Guest

    "Damian" <> wrote:
    > Damian wrote:
    > > I notice though that that if you hit a key while it's looping (logged
    > > into ssh), it shifts the whole thing over, then the next itereation
    > > comes along and rewites that portion, ending up with the right hand
    > > bit of text from the shifted part rtemaining there. Is the only way
    > > to make sure that everything up to the end of thel ine is cleared,
    > > short of manually padding it with spaces, which would require I find
    > > out the column width and then find out how much space is left. Theres
    > > gotta be a beter way to do this.

    >
    > Or would it be bettter to supress input / local echo? Is this even
    > possible and a good way to go about this?


    Depending on what you're doing, yes, it may well be.

    See Term::ReadKey and (as I said before) Term::ANSIScreen.

    Ben

    --
    Although few may originate a policy, we are all able to judge it.
    - Pericles of Athens, c.430 B.C.
    Ben Morrow, Feb 10, 2004
    #6
  7. On Mon, 09 Feb 2004 18:30:05 -0800, Damian wrote:

    <snip>

    >
    > This this a good way of doing it? This is what I just tried:
    >
    > #!/usr/local/bin/perl -w
    >
    > use strict;
    >
    > $| = 1; # Needs to be non zero so you cna see print's in a loop.
    >
    > for my $i (0..10) {
    > my $c = $i * 10;
    > print "Percent complete: $c %\r";
    >
    > sleep(1); # Sleep for 1 second.
    > }
    > print "Percent complete: Done!\n"
    >
    >
    > I notice though that that if you hit a key while it's looping (logged
    > into ssh), it shifts the whole thing over, then the next itereation
    > comes along and rewites that portion, ending up with the right hand bit
    > of text from the shifted part rtemaining there. Is the only way to make
    > sure that everything up to the end of thel ine is cleared, short of
    > manually padding it with spaces, which would require I find out the
    > column width and then find out how much space is left. Theres gotta be a
    > beter way to do this.


    If you want to have a progress bar or something similar, try
    Term::progressBar.

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. http://www.gnu.org/licenses/gpl.txt
    for more information.

    a fortune quote ...
    This fortune is inoperative. Please try another.
    James Willmore, Feb 10, 2004
    #7
  8. Damian

    Brad Baxter Guest

    On Mon, 9 Feb 2004, Damian wrote:

    > was wondering how exactly do you print(...) in place? Like the percent
    > counter for installers like linux's rpm. In other words so the output
    > keeps going to the same spot in stead of advancing the cursor.
    >
    > If this is something only on Linux/Unix thats fine, as thats the only
    > place I really need to do this. Though portability never hurts.
    >


    You've gotten answers that are surely more correct and more portable, but
    I typically use this technique for "quickie" progress reporting:

    perl -e '$|++;for(1..10){printf "%6d%s", $_, "\b"x6; sleep 1}'

    Yes, it suffers from the same problems you mentioned re \r.

    Regards,

    Brad
    Brad Baxter, Feb 10, 2004
    #8
    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. Craig G

    Javascript Quickie

    Craig G, Jan 25, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    400
    Craig G
    Jan 25, 2005
  2. Roy

    Quickie: Printing out code?

    Roy, Oct 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    409
  3. Roy
    Replies:
    1
    Views:
    586
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 26, 2006
  4. Gene Ellis
    Replies:
    0
    Views:
    505
    Gene Ellis
    Aug 25, 2004
  5. bartek
    Replies:
    3
    Views:
    656
    Victor Bazarov
    Mar 7, 2004
Loading...

Share This Page