Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

full screen UI in perl

by Anonymous Monk
on Jun 12, 2001 at 03:36 UTC ( #87680=perlquestion: print w/replies, xml ) Need Help??

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

is there any way to implement in perl a method of displaying lines so that a line can be replaced on the screen (lets say in the middle of a bunch of other lines) without reprinting all the lines. like updating lines on the screen without clearing the terminal and printing the new data again? i have heard 'curses' does this but i am not sure. can you enlighten me?

Replies are listed 'Best First'.
Re: full screen UI in perl
by stephen (Priest) on Jun 12, 2001 at 04:19 UTC
    If you're displaying on an ANSI terminal, you can also use Term::ANSIScreen to move the cursor around. Here's a sample that clears the screen, prints "Hello" at column 10, row 10, pauses, then prints "Bye" on the same line.
    use strict; use Term::ANSIScreen qw(:cursor :screen); $| = 1; cls; locate(10, 10); print "Hello"; sleep(1); locate(10,10); clline(); print "Bye"; sleep(1);

    Update: Turned off buffering (which was causing some problems) and added example for clline.


      Wouldn't you be better off setting $| = 1 then printing without the newlines?

          -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: full screen UI in perl
by ZZamboni (Curate) on Jun 12, 2001 at 04:05 UTC
    Curses is the right word. It is a library (in Unix at least, you didn't say your platform) that allows you to do full-screen manipulation. See the Curses module for more details. I have never programmed with curses, so I don't really know how well it works from Perl.

    Update: See also these nodes: Curses Help and How can I use curses with Perl?


Re: full screen UI in perl
by toma (Vicar) on Jun 12, 2001 at 12:49 UTC
    Curses has quite a few functions, some of which you may find useful. The addstr();refresh() functions do what you want.

    There are also more full-featured curses functions in Curses::Forms and Curses::Widgets.

    Here is a snippet that uses Curses to do something like what you want. The hard part is figuring out how to keep the screen refreshed while your code is off doing something more interesting.

    use strict; use Curses; my $mwh = new Curses; $mwh->addstr(8, $LINES-2, 'Initialized...'); $mwh->refresh; sleep 2; for (qw(Show_me_first I'm_second Third_then_quit)) { $mwh->addstr(8, $LINES-2, sprintf("%15s",$_)); $mwh->refresh(); sleep 2; } END { endwin(); # Make Curses clean up after itself }

    The Perl Curses module documentation assumes that you already understand the curses library. For simple tasks, it's not too bad to figure out. As your requirements become more complex, it is easy to get bogged down in the details of curses. Another approach would be to use a GUI status window such as one provided by Tk (warning: Tk link returns lots of stuff).

    It should work perfectly the first time! - toma

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://87680]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-05-19 05:32 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (71 votes). Check out past polls.