Term::Clui(3pm)
π Term::Clui β the perl module that gives your command-line app a whole personality π
(this is the Term::Clui(3pm) page, from the User Contributed Perl Documentation corner of the world, bestie)
β¨ NAME β¨
Term::Clui.pm β a Perl module offering a Command-Line User Interface π
πΈ SYNOPSIS πΈ
okay so here's the whole vibe in code form, no cap:
use Term::Clui;
$chosen = choose("A Title", @a_list); # single choice
@chosen = choose("A Title", @a_list); # multiple choice
# multi-line question-texts are possible...
$x = choose("Which ?\n(Mouse, or Arrow-keys and Return)", @w);
$x = choose("Which ?\n".help_text(), @w);
if (confirm($text)) { do_something(); };
$answer = ask($question);
$answer = ask($question,$suggestion);
$password = ask_password("Enter password:");
$filename = ask_filename("Which file ?"); # with Tab-completion
$newtext = edit($title, $oldtext);
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 hands you a high-level user interface so your command-line apps all get that consistent "look and feel", like they all went to the same aesthetic boarding school. π
Its metaphor for the computer? A human-like conversation-partner. Every question/response, once it's done, gets summarised onto one single line and just stays on screen, so the history of the whole session slowly accumulates in front of you, ready for review, or for cut/paste. Which means β and this is the slay part β this interface can be intermixed with standard applications that write to STDOUT or STDERR, like make, pgp, rcs etc. π«Ά
For the user, choose() uses either (since 1.50) the mouse; or arrow keys (or hjkl) and Return; also q to quit, and SpaceBar or Button3 to highlight multiple choices. 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 does not 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, and also (since 1.50) the SET_ANY_EVENT_MOUSE and kmous (terminfo) sequences, which are supported by all xterm, rxvt, konsole, screen, linux, gnome and putty terminals. π§
There is an associated file selector, Term::Clui::FileSelect
Since version 1.60, a speaking interface is provided for the visually-impaired user; it employs eflite or espeak. Speech is turned on if the CLUI_SPEAK environment variable is set to any non-empty string. Since version 1.62, if speakup is running, it is silenced while Term::Clui runs, and then restored. Because Term::Clui's metaphor for the computer is a human-like conversation-partner, this works very naturally. The application needs no modification. ππ
There is an equivalent Python3 module, with (as far as possible) the same calling interface, at http://cpansearch.perl.org/src/PJB/Term-Clui-1.71/py/TermClui.py
This is Term::Clui.pm version 1.71 β
π 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, the user is asked to enter "clue" letters, and as soon as the items matching them will fit onto the screen they are displayed as a choice. π΅οΈβοΈ
π SUBROUTINES π
ask( $question ); OR ask( $question, $default );
Asks the user the question and returns a string answer, with no newline character at the end. If the optional second argument is present, it is offered to the user as a default. If the $question is multi-line, the entry-field is at the top to the right of the first line, and the subsequent lines are formatted within the screen width and displayed beneath, as with choose. π
For the user, left and right arrow keys move backward and forward through the string, delete and backspace erase the previous character, ctrl-A moves to the beginning, ctrl-E to the end, and ctrl-D or ctrl-X clear the current string. π«Ά
ask_password( $question );
Does the same with no echo, as used for password entry. π€« (very sneaky, very demure)
ask_filename( $question );
Uses Term::ReadLine::Gnu to provide filename-completion with the Tab key, but also displays multi-line questions in the same way as ask and choose do. This function was introduced in version 1.65. β¨οΈ
choose( $question, @list );
Displays the question, and formats the list items onto the lines beneath it. β¨
If choose is called in a scalar context, 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 undefined if the choice was cancelled. π
If choose is called in an array context, the user can also mark an item with the SpaceBar. choose then returns the list of marked items, (including the item highlit when Return was pressed), or an empty array 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 therefore exchange defaults simply by using the same question words, such as "Which printer ?". Multiple choices are not remembered, as the danger exists that the user might fail to notice some of the highlit items (for example, all the items might not fit onto one screen). π
The database ~/.clui_dir/choices or $ENV{CLUI_DIR}/choices is available to be read or written if lower-level manipulation is needed, and the EXPORT_OK routines get_default($question) and set_default($question, $choice) should be used for this purpose, as they handle DBM's problem with concurrent accesses. The whole default database mechanism can be disabled by CLUI_DIR=OFF if you really want to :-( π
If the items won't fit on the screen, the user is asked to enter a substring as a clue. As soon as the matching items will fit, they are displayed to be chosen as normal. If the user pressed "q" at this choice, they are asked if they wish to change their substring clue; if they reply "n" to this, choose quits and returns undefined. π§
If the $question is multi-line, The first line is put at the top as usual with the choices arranged beneath it; the subsequent lines are formatted within the screen width and displayed at the bottom. After the choice is made all but the first line is erased, and the first line remains on-screen with the choice 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. π
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"; π’
inform( $message );
Similar to warn "$message\n"; except that it doesn't add the newline at the end if there already is one, and it uses /dev/tty rather than STDERR if it can. π£
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 one or two lines it just omits the title and displays it. Otherwise it 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. π
If there is only one argument and it's a filename, then the user's PAGER displays it, except (since 1.65) if it's a .doc file, when either wvText, antiword or catdoc is used to extract its contents first. π
help_text( $mode );
This returns a short help message for the user. If mode is "ask" then the text describes the keys the user has available when responding to an &ask question; If mode is "multi" then the text describes the keys and mouse actions the user has available when responding to a multiple-choice &choose question; otherwise, the text describes the keys and mouse actions the user has available when responding to a single-choice &choose. π
π EXPORT_OK SUBROUTINES π
The following routines are not exported by default, but are exported under the ALL tag, so if you need them you should:
import Term::Clui qw(:ALL);
beep()
Beeps. π’ (yes that's it, that's the whole thing)
timestamp()
Returns a sortable timestamp string in "YYYYMMDD hhmmss" form. β°
get_default( $question )
Consults the database ~/.clui_dir/choices or $ENV{CLUI_DIR}/choices and returns the choice that the user made the last time this question was asked. This is better than opening the database directly as it handles DBM's problem with concurrent accesses. ποΈ
set_default( $question, $new_default )
Opens the database ~/.clui_dir/choices or $ENV{CLUI_DIR}/choices and sets the default response which will be offered to the user made the next time this question is asked. This is better than opening the database directly as it handles DBM's problem with concurrent accesses. πΎ
π DEPENDENCIES π
It requires Exporter, which is core Perl. It uses Term::ReadKey if it's available; and uses Term::Size if it's available; if not, it tries tput before guessing 80x24. π₯οΈ
β¨ ENVIRONMENT β¨
okay bestie, so Term::Clui is lowkey nosy about your environment variables, and honestly? same. here's the tea β
CLUI_DIRβ this one lets a programmer or a user override~/.clui_diras the directory wherechoose()keeps its database of previous choices. it's giving "i want my choices remembered somewhere else" energy.CLUI_DIR = OFFβ if you set it toOFF, the whole default database mechanism gets disabled. only do this if you really want to, okay? the original says "if you really want to :-(" and i felt that π.LANGorLC_TYPEβ if either of these contains the stringutf8orutf-8(case insensitive, no cap), thenchoose()andinform()open/dev/ttywith a utf8 encoding. slay for unicode π .CLUI_SPEAKβ if this is set, or ifEDITORis set toemacspeak, and iffliteis installed, then Term::Clui will usefliteto speak its questions and choices out loud. yes, it literally talks to you π£οΈβ¨.CLUI_MOUSEβ if you set this toOFF, thenchoose()will not interpret mouse-clicks as making a choice. the advantage? the mouse can then be used to highlight and paste text from this window as usual. so it's a trade-off, bestie π±οΈ.HOME,LOGDIR,EDITOR,PAGERβ Term::Clui also consults these if they are set. it's just checking in, being supportive π«Ά.
π EXAMPLES π
these scripts use Term::Clui and Term::Clui::FileSelect, and they live in the examples subdirectory of the build directory. go peek, they're iconic β¨
linux_adminβ the author uses this a lot at work for routine system administration of linux boxes, especially Fedora and Debian. it includescrontab,chkconfig,update-rc.d,visudo,vipw, starting and stopping daemons, reconfiguringsquidsambaorapache, editingsysconfigor running any of thesystem-config-*utilities, and much else. main character sysadmin energy πaudio_stuffβ this one offers an arrow-key-and-return interface integratingaplaymidi,cdrecord,cdda2wav,icedax,lame,mkisofs,muscript,normalize,normalize-audio,mpg123,sndfile-play,timidity,wodimand so on. it lets you rip, burn, play, or convert audio files between Muscript, MIDI, WAV and MP3 formats. DJ bestie activated π§π₯login_shellβ gives the naive user arrow-key-and-return access to a text-based browser, a mail client, a news client,sshandftpand various other stuff. so cute, so accessible π¦test_scriptβ this is the test script, as used during development. the behind-the-scenes queen π§chooseβ this wrapsTerm::Clui::choosefor use at the shell-script level. it can either choose between command-line arguments, or, with the-f(filter) option, between lines of STDIN, likegrep. a-m(multiple) option allows multiple-choice. this can be a very useful script, and you may want to copy it into/usr/local/bin/or elsewhere in yourPATH. periodt. copy her π
π AUTHOR π
- Original author:
Peter J Billam www.pjb.com.au/comp/contact.html
- Current maintainer:
Graham Ollis
- Contributors:
Peter Scott
π CREDITS π
this is based on some old perl 4 libraries: ask.pl, choose.pl, confirm.pl, edit.pl, sorry.pl, inform.pl and view.pl, which were themselves based on some even older curses-based programs in C. the ancestry is real π
π SEE ALSO π
Term::Clui::FileSelectTerm::ReadKeyTerm::Sizehttp://www.pjb.com.au/http://invisible-island.net/xterm/ctlseqs/ctlseqs.htmlhttp://search.cpan.org/~pjbfestival(1)eflite(1)espeak(1)espeakup(1)edbrowse(1)emacspeak(1)perl(1)
there's an equivalent Python3 module with (as far as possible) the same calling interface, at https://fastapi.metacpan.org/source/PJB/Term-Clui-1.71/py/TermClui.py.
okay that's the whole vibe. this page was from perl v5.30.0, dated 2019-11-10, and it's Term::Clui(3pm). go forth and choose things beautifully, bestie π β¨π¦
π¦ where this page comes from
- debian bookworm: libterm-clui-perl 1.76-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libterm-clui-perl 1.76-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libterm-clui-perl 1.76-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libterm-clui-perl 1.76-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms