| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use Shelly; | ||
| 4 | |||
| 5 | my @COMMANDS = qw(help server login connect message disconnect hop); | ||
| 6 | my @WORDS = qw(hello how are you today fantasy rumble forward down fast); | ||
| 7 | |||
| 8 | my $sh = Term::Shelly->new(); | ||
| 9 | |||
| 10 | $sh->out( | ||
| 11 | "This is a demo of Term::Shelly. It is designed to show you some of the | ||
| 12 | features of it. For this demo there is only a short number of features | ||
| 13 | available, more will appear later. You can do basic line editing (arrows move | ||
| 14 | around, backspace, ^W, tab completion, ^U, etc)","","The following commands can | ||
| 15 | be tab completed: | ||
| 16 | " . join(" ", @COMMANDS) . " | ||
| 17 | However, no commands actually do anything, this is jus tto demo the tab | ||
| 18 | completion system. The following words can be tab completed: | ||
| 19 | " . join(" ", @WORDS)." | ||
| 20 | Commands must start with a / (forward slash) and be at the beginning of | ||
| 21 | the line."); | ||
| 22 | |||
| 23 | $sh->{"completion_function"} = \&completer; | ||
| 24 | |||
| 25 | |||
| 26 | while (1) { | ||
| 27 | $sh->do_one_loop(); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub completer { | ||
| 31 | my ($line, $bword, $pos, $curword) = @_; | ||
| 32 | |||
| 33 | my @matches; | ||
| 34 | |||
| 35 | # Context-sensitive completion. | ||
| 36 | # | ||
| 37 | # Only complete commands if our current word begins on the 0th column | ||
| 38 | # and starts with a / (slash) | ||
| 39 | if (($bword == 0) && (substr($line,$bword,1) eq '/')) { | ||
| 40 | # We want to complete a command... | ||
| 41 | $word = substr($curword,1); | ||
| 42 | @matches = map { "/$_" } grep(m/^\Q$word\E/i, @COMMANDS); | ||
| 43 | } else { | ||
| 44 | @matches = grep(m/\Q$curword\E/i, @WORDS); | ||
| 45 | } | ||
| 46 | |||
| 47 | return @matches; | ||
| 48 | } |