| Visual(3pm) | User Contributed Perl Documentation | Visual(3pm) |
Term::Visual - split-terminal user interface
#!/usr/bin/perl -w
use strict;
use Term::Visual;
my $vt = Term::Visual->new( Alias => "interface",
Errlevel => 0 );
$vt->set_palette( mycolor => "magenta on black",
thiscolor => "green on black" );
my $window_id = $vt->create_window(
Window_Name => "foo",
Status => { 0 =>
{ format => "template for status line 1",
fields => [qw( foo bar )] },
1 =>
{ format => "template for status line 2",
fields => [ qw( biz baz ) ] },
},
Buffer_Size => 1000,
History_Size => 50,
Input_Prompt => "[foo] ", # Set the input prompt for the input line.
Use_Title => 0, # Don't use a titlebar
Use_Status => 0, # Don't use a statusbar
Title => "Title of foo" );
POE::Session->create
(inline_states => {
_start => \&start_handler,
got_term_input => \&term_input_handler,
}
);
sub start_handler {
my $kernel = $_[KERNEL];
# Tell the terminal to send me input as "got_term_input".
$kernel->post( interface => send_me_input => "got_term_input" );
$vt->set_status_field( $window_id, bar => $value );
$vt->set_input_prompt($window_id, "\$");
$vt->print( $window_id, "my Window ID is $window_id" );
}
sub term_input_handler {
my ($kernel, $heap, $input, $exception) = @_[KERNEL, HEAP, ARG0, ARG1];
# Got an exception. These are interrupt (^C) or quit (^\).
if (defined $exception) {
warn "got exception: $exception";
exit;
}
$vt->print($window_id, $input);
}
# Only use delete_window if using multiple windows.
$vt->delete_window( $window_id );
$vt->shutdown;
Term::Visual is a "visual" terminal interface for curses applications. It provides the split-screen interface you may have seen in console based IRC and MUD clients.
Term::Visual uses the POE networking and multitasking framework to support concurrent input from network sockets and the console, multiple timers, and more.
Term::Visual->method();
my $vt = Term::Visual->new(
Alias => "interface",
Common_Input => 1,
Tab_Complete => sub { ... },
Errlevel => 0 );
Alias is a session alias for POE.
Common_Input is an optional flag used
to globalize History_Position,
History_Size,
Command_History,
Data,
Data_Save,
Cursor,
Cursor_Save,
Tab_Complete,
Insert,
Edit_Position
in create_window(); Thus all windows created will have common
input.
Tab_Complete is a handler for tab completion.
Tab_Complete => sub {
my $left = shift;
my @return;
my %complete = (
foo => "foobar ",
biz => "bizbaz ",
);
return $complete{$left};
}
Tab_Complete is covered more indepth in the examples directory.
Errlevel not implemented yet.
Errlevel sets Term::Visual's error level.
my $window_id = $vt->create_window( ... );
Set the window's name
Window_Name => "foo"
Set the Statusbar's format
Status => { 0 => # first statusline
{ format => "\0(st_frames)" .
" [" .
"\0(st_values)" .
"%8.8s" .
"\0(st_frames)" .
"] " .
"\0(st_values)" .
"%s",
fields => [qw( time name )] },
1 => # second statusline
{ format => "foo %s bar %s",
fields => [qw( foo bar )] },
}
Set the size of the scrollback buffer
Buffer_Size => 1000
Set the command history size
History_Size => 50
Set the input prompt of the window
Input_Prompt => "foo"
Set the title of the window
Title => "This is the Titlebar"
Don't use Term::Visual's Titlebar.
Use_Title => 0
Don't use Term::Visual's StatusBar.
Use_Status => 0
No need to declare Use_Status or Use_Title if you want to use the Statusbar or Titlebar.
create a handler for parsing the input in your POE Session.
POE::Session->create
(inline_states => {
_start => \&start_handler,
got_term_input => \&term_input_handler,
}
);
POE's _start handler is a good place to tell Term::Visual how to send you input.
sub start_handler {
my $kernel = $_[KERNEL];
# Tell the terminal to send me input as "got_term_input".
$kernel->post( interface => send_me_input => "got_term_input" );
...
}
Now create your "term_input_handler" to parse input. In this case we simply check for exceptions and print the input to the screen.
sub term_input_handler {
my ($kernel, $heap, $input, $exception) = @_[KERNEL, HEAP, ARG0, ARG1];
# Got an exception. These are interrupt (^C) or quit (^\).
if (defined $exception) {
warn "got exception: $exception";
exit;
}
$vt->print($window_id, $input);
}
$vt->print( $window_id, "this is a string" );
my @array = qw(foo bar biz baz);
$vt->print( $window_id, @array );
my $current_window = $vt->current_window;
$vt->print( $current_window, "current window is $current_window" );
my $window_name = $vt->get_window_name( $window_id );
my $window_id = $vt->get_window_id( $window_name );
$vt->delete_window($window_id);
or
$vt->delete_window(@window_ids);
my $validity = $vt->validate_window( $window_id );
or
my $validity = $vt->validate_window( $window_name );
if ($validity) { do stuff };
my %palette = $vt->get_palette();
my $color_desc = $vt->get_palette($colorname);
my ($foo, $bar) = $vt->get_palette($biz, $baz);
$vt->set_palette( color_name => "color on color" );
$vt->set_palette( color_name => "color on color",
another => "color on color" );
NOTE: (ncolor, st_values, st_frames, stderr_text, stderr_bullet, statcolor)
are set and used by Term::Visual internally.
It is safe to redifine there values.
So if you wanted to print something with a color you could simply use:
$vt->print( $window_id, "\0(color_name)My this is a wonderful color." );
$vt->set_title( $window_id, "This is the new Title" );
my $title = $vt->get_title( $window_id );
$vt->change_window( $window_id );
$vt->change_window( 0 );
...
$vt->change_window( 1 );
$vt->set_status_format( $window_id,
0 => { format => "template for status line 1",
fields => [ qw( foo bar ) ] },
1 => { format => "template for status line 2",
fields => [ qw( biz baz ) ] }, );
$vt->set_status_field( $window_id, field => "value" );
$vt->set_status_field( $window_id, foo => "bar", biz => "baz" );
$vt->set_input_prompt($window_id, "\$");
$vt->set_input_prompt($window_id, "[foo]");
columnize is used internally, but might be of use
externally as well.
Arguments given to columnize must be a hash.
key 'Items' must be an array reference.
The default value for Maxwidth may change to $COLS.
my $table = $vt->columnize(
Items => \@list,
Padding => 2, # default value and optional
MaxColumns => 10, # default value and optional
MaxWidth => 80 # default value and optional
);
bind is used for key bindings.
our %Bindings = (
Up => 'history',
Down => 'history',
...
);
$vt->bind(%Bindings);
sub handler_history {
my ($kernel, $heap, $key, $win) = @_[KERNEL, HEAP, ARG0, ARG2];
if ($key eq 'KEY_UP') {
$vt->command_history($win, 1);
}
else {
$vt->command_history($win, 2);
}
}
POE::Session->create(
inline_states => {
_start => \&handler_start,
_stop => \&handler_stop,
history => \&handler_history,
...
}
);
$vt->unbind('Up', 'Down');
$vt->unbind(keys %Bindings);
$vt->debug("message");
Debugging must be turned on before using this.
change sub DEBUG () { 0 } to 1 or
add this to your program:
sub Term::Visual::DEBUG () { 1 }
use Term::Visual;
$vt->shutdown();
Kill a Window.
Questions and Comments can be sent to lunartear@cpan.org
Please send bug reports and wishlist items to:
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Term-Visual
Rocco has contributed to the development of Term::Visual In many ways.
Rocco Caputo <troc+visterm@pobox.com>
Thank you!
| 2021-01-08 | perl v5.32.0 |