NAME

Term::Completion - read one line of user input, with convenience functions

USAGE

use Term::Completion;
my $tc = Term::Completion->new(
  prompt  => "Enter your first name: ",
  choices => [ qw(Alice Bob Chris Dave Ellen) ]
);
my $name = $tc->complete();
print "You entered: $name\n";

DESCRIPTION

Term::Completion is an extensible, highly configurable replacement for the venerable Term::Complete package. It is object-oriented and thus allows subclassing. Two derived classes are Term::Completion::Multi and Term::Completion::Path.

A prompt is printed and the user may enter one line of input, submitting the answer by pressing the ENTER key. This basic scenario can be implemented like this:

my $answer = <STDIN>;
chomp $answer;

But often you don't want the user to type in the full word (from a list of choices), but allow completion, i.e. expansion of the word as far as possible by pressing as few keys as necessary.

Some users like to cycle through the choices, preferably with the up/down arrow keys.

And finally, you may not want the user to enter any random characters, but validate what was enter and come back if the entry did not pass the validation.

If you are missing full line editing (left/right, delete to the left and right, jump to the beginning and the end etc.), you are probably wrong here, and want to consider Term::ReadLine and friends.

Global Setup

The technical challenge for this package is to read single keystrokes from the input handle - usually STDIN, the user's terminal. There are various ways how to accomplish that, and Term::Completion supports them all:

Exports

Term::Completion does not export anything by default, in order not to pollute your namespace. Here are the exportable methods:

Methods

Term::Completion objects are simple hashes. All fields are fully accessible and can be tweaked directly, without accessor methods.

Term::Completion offers the following methods:

Configuration

There is a global hash %Term::Completion::DEFAULTS that contains the default values for all configurable options. Upon object construction (see "new(...)" any of these defaults can be overridden by placing the corresponding key/value pair in the arguments. Find below the list of configurable options, their default value and their purpose.

The key definitions are regular expressions (qr/.../) - this allows to match multiple keys for the same action, as well as disable the action completely by specifying an expression that will never match a single character, e.g. qr/-disable-/.

Predefined Validations

Whenever you need validation of the user's input, you can always specify your own code, see "validate($answer)" above. To support everybody's laziness, there are a couple of predefined validation methods available. You can specify them as a blank or comma separated string in the new(...) constructor:

my $tc = Term::Completion->new(
  prompt => 'Fruit: ',
  choices => [ qw(apple banana cherry) ],
  validate => 'nonblank fromchoices'
);

In the example above, you are guaranteed the user will choose one of the given choices. Here's a list of all pre-implemented validations:

This list obviously can be arbitrarily extended. Suggestions (submitted as patches) are welcome.

CAVEATS

Terminal handling

This package temporarily has to set the terminal into 'raw' mode, which means that all keys lose their special meaning (like CTRL-C, which normally interrupts the script). This is a highly platform-specific operation, and therefore this package depends on the portability of Term::ReadKey and POSIX. Reports about failing platforms are welcome, but there is probably little that can be fixed here.

Terminal size changes

This package does the best it can to handle changes of the terminal size during the completion process. It displays the prompt again and the current entry during completion, and restarts paging when showing the list of choices. The latter however only after you press a key - the bell sounds to indicate that something happened. This is because it does not seem possible to jump out of a getc().

Arrow key handling

On UNIX variants, the arrow keys generate a sequence of bytes, starting with the escape character, followed by a square brackets and others. Term::Completion accumulates these characters until they either match this sequence, or not. In the latter case, it will drop the previous characters and proceed with the last one typed. That however means that you won't be able to assign the bare escape key to an action. I found this to be the lesser of the evils. Suggestions on how to solve this in a clean way are welcome. Yes, I read "How can I tell whether there's a character waiting on a filehandle?" in perlfaq5 but that's probably little portable.

SEE ALSO

Term::Complete, Term::ReadKey, Term::Size, POSIX, Term::ReadLine

AUTHOR

Marek Rouchal, <marekr@cpan.org<gt>

BUGS

Please submit patches, bug reports and suggestions via the CPAN tracker http://rt.cpan.org.

COPYRIGHT AND LICENSE

Copyright (C) 2009-2013 by Marek Rouchal

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.