NAME

Term::Clui.pm - Perl module offering a Command-Line User Interface

SYNOPSIS

use Term::Clui;
$chosen = &choose('A Title', @a_list);
if (&confirm ($text)) { &do_something; };
$newtext = &edit($title, $text);
&edit($filename);
&view ($title, $text)  # if $title is not a filename
&view ($textfile)  # if $textfile _is_ a filename

&edit (&choose ('Edit which file ?', grep (-T, readdir D)));

DESCRIPTION

Term::Clui offers a high-level user interface to give the user of command-line applications a consistent "look and feel". Its metaphor for the computer is as a human-like conversation-partner, and as each question/response is completed it is summarised onto one line, and remains on screen, so that the history of the session gradually accumulates on the screen and is available for review, or for cut/paste. This user interface can therefore be intermixed with standard applications which write to STDOUT or STDERR, such as make, pgp, rcs etc.

For the user, &choose uses arrow keys (or hjkl) and return or q, and &confirm expects y, Y, n or N. In general, ctrl-L redraws the (currently active bit of the) screen. &edit and &view use the default EDITOR and PAGER if possible.

It's fast, simple, and has few external dependencies. It doesn't use curses (which is a whole-of-screen interface); it uses a small subset of vt100 sequences (up down left right normal and reverse) which are very portable. There is an HTML equivalent planned, offering similarly named routines for CGI scripts.

WINDOW-SIZE

Term::Clui attempts to handle the WINCH signal. If the window size is changed, then as soon as the user enters the next keystroke (such as ctrl-L) the current question/response will be redisplayed to fit the new size.

The first line of the question, the one which will remain on-screen, is not re-formatted, but is left to be dealt with by the width of the window. Subsequent lines are split into blank-separated words which are filled into the available width; lines beginning with white-space are treated as the beginning of a new indented paragraph, individual words which will not fit onto one line are truncated, and successive blank lines are collapsed into one. If the question will not fit within the available rows, it is truncated.

If the available choice items in a &choose overflow the screen, then currently complete.pl is invoked. This is fine if the user knows the item they want by its first letters, but it's not good otherwise. It will be replaced by asking the user to enter 'clue' letters, and as soon as the items matching them will fit onto the screen they will be displayed as a choice.

SUBROUTINES

ask( $question );

Asks the user the question and returns a string answer, with no newline character at the end.

ask_password( $question );

Does the same with no echo, as used for password entry.

choose( $question, @list );

Displays the question, and formats the list items onto the lines beneath it. The user can choose an item using arrow keys (or hjkl) and return, or cancel the choice with a 'q'. choose then returns the chosen item, or the empty string if the choice was cancelled.

A DBM database is maintained of the question and its chosen response. The next time the user is offered a choice with the same question, if that response is still in the list it is highlighted as the default; otherwise the first item is highlighted. Different parts of the code, or different applications using Term::Clui.pm can exchange defaults simply by using the same question words, such as "Which printer ?". The database ~/db/choices is available to be read or written if lower-level manipulation is needed.

confirm( $question );

Asks the question, takes 'y', 'n', 'Y' or 'N' as a response. If the $question is multi-line, after the response, all but the first line is erased, and the first line remains on-screen with Yes or No appended after it; you should therefore try to arrange multi-line questions so that the first line is the question in short form, and subsequent lines are explanation and elaboration. Returns true or false.

edit( $title, $text ); OR edit( $filename );

Uses the environment variable EDITOR ( or vi :-) Uses RCS if directory RCS/ exists

sorry( $message );

Similar to warn "Sorry, $message\n";

view( $title, $text ); OR view( $filename );

If the $text is longer than a screenful, uses the environment variable PAGER ( or less ) to display it; if it is shorter, uses a simple built-in routine which expects either 'q' or Return from the user. If the user presses Return the displayed text remains on the screen and the dialogue continues after it; if the user presses 'q' the text is erased.

DEPENDENCIES

It requires Exporter, flush.pl, and (currently) complete.pl, which are all core Perl. It uses Term::Size.pm if it's available; if not, it tries `tput` before guessing 80x24.

ENVIRONMENT

Uses the environment variables HOME, LOGDIR, EDITOR and PAGER, if they are set.

AUTHOR

Peter J Billam <computing@pjb.com.au>

CREDITS

Based on some old perl 4 libraries, ask.pl, choose.pl, confirm.pl, edit.pl, sorry.pl and view.pl, which were in turn based on some even older curses-based programs in C.

SEE ALSO

http://www.pjb.com.au/ , perl(1), http://www.cpan.org/SITES.html