cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Monks. I am trying to make a little text mode spinner that will indicate that a script is running. Somehow I can't get the ANSIScreen mod to work properly. The code follows. When I run it, it waits till the script is done then prints out the intended chars with the escape codes embedded, rather than taking them as escape codes. I'm running it on Win32. What am I doing wrong?

TIA....

Steve

use strict; use Win32::Console::ANSI; # thanks ikegami $|=1; # turn off output buffering. thanks Tanktalus use Term::ANSIScreen qw/:screen :cursor/; our @spinchars = qw( / - \ | ); our $spinvalue = 0; for my $i (0..10) { spinner(); sleep 1; } $|=0; # restore output buffering sub spinner { print left(1)."$spinchars[$spinvalue]"; $spinvalue++; if ($spinvalue > 3) { $spinvalue = 0; } return; }

UPDATE:

With suggested chantes it works now except that the cursor flashes next to the spinner in an undesirable way. I tried to turn it off with

my $console = Term::ANSIScreen->new; $console->Cursor(-1,-1,-1,-1,0);

As it seemed might work based on the Term::ANSIScreen docs. Alas something is not working right as the cursor doesn't shut off.

Formatting fixed by GrandFather

2006-06-28 Retitled by GrandFather, as per Monastery guidelines
Original title: 'ANSIScreen progress spinner'

Replies are listed 'Best First'.
Re: ANSIScreen won't work
by Tanktalus (Canon) on Jun 27, 2006 at 17:14 UTC

    First, you're buffering your output. This is on by default, thus you need to take a step to turn it off. Try $|++ somewhere before you start printing anything out. That will turn off the buffering.

    Second, I seem to recall there being something you need to do to get a windows console to interpret the ANSI codes, but don't remember what that is. Hopefully a Windows monk will know.

      I seem to recall there being something you need to do to get a windows console to interpret the ANSI codes

      The console doesn't naturally understand ANSI escape sequences. Adding use Win32::Console::ANSI; to your script adds support for ANSI escape sequences to STDOUT(your console).

Re: Term::ANSIScreen Won't Work
by johngg (Canon) on Jun 27, 2006 at 18:08 UTC
    You may need to escape your backslash to get a literal '\' like this

    our @spinchars = qw( / - \\ | );

    I certainly did when I wrote a spinner, although I didn't use qw( ... ).

    Cheers,

    JohnGG

    Update: No, I was wrong, it seems you don't have to when using qw( ... ) which is useful to know. You do have to use double backslash if quoting each spinner character separately

    our @spinchars = ('/', '-', '\\', '|');