Shorten text and change Sepia::flow() to shorten ",help" output.
[sepia.git] / lib / Sepia / ReadLine.pm
blob5fc86398b7f9490a65b3fc428c172d6684d062fd
1 package Sepia::ReadLine;
2 use Term::ReadLine;
3 use Sepia;
4 require Exporter;
5 @ISA='Exporter';
6 @EXPORT='repl';
8 sub rl_complete
10 my ($text, $line, $start) = @_;
11 my @xs;
12 if (substr($line, 0, $start) =~ /^\s*$/ && $text =~ /^,(\S*)$/) {
13 my $x = qr/^\Q$1\E/;
14 @xs = map ",$_", grep /$x/, keys %Sepia::REPL;
15 } else {
16 my ($type, $str) = (substr $line, $start ?(($start-1), length($text)+1)
17 : ($start, length($text)))
18 =~ /^([\$\@\%\&]?)(.*)/;
19 my %h = qw(@ ARRAY % HASH & CODE * IO $ VARIABLE);
20 @xs = Sepia::completions $h{$type||'&'}, $str;
22 @xs;
25 sub repl
27 { package main; do $_ for @ARGV }
28 $TERM = new Term::ReadLine $0;
29 my $rl = Term::ReadLine->ReadLine;
30 if ($rl =~ /Gnu/) {
31 my $attr = $TERM->Attribs;
32 $attr->{completion_function} = \&rl_complete;
33 } elsif ($rl =~ /Perl/) {
34 $readline::rl_completion_function = \&rl_complete;
35 $readline::var_TcshCompleteMode = 1;
36 # XXX: probably helpful...
37 # } elsif (grep -x "$_/rlwrap", split ':', $ENV{PATH}) {
38 # warn "Sepia::ReadLine: Falling back to rlwrap.\n";
39 } else {
40 warn "Sepia::ReadLine: No completion with $rl.\n";
42 $Sepia::READLINE = sub { $TERM->readline(Sepia::prompt()) };
43 goto &Sepia::repl;