File: test.pl

package info (click to toggle)
libterm-shell-perl 0.02-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 124 kB
  • ctags: 106
  • sloc: perl: 1,006; makefile: 3
file content (122 lines) | stat: -rw-r--r-- 3,229 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!perl -w

package app;
use strict;
use base qw(Term::Shell);
use Data::Dumper;

sub init {
    my $o = shift;
    $o->remove_handlers("run_squiggle");
    $o->{API}{match_uniq} = 0;	# allow only exact matches
    $o->{API}{check_idle} = 1;	# run on_idle() every 1 second
}

sub idle {
    my $o = shift;
    $o->{SHELL}{num}++;
}

# The default method (when you enter a blank line). This command is not shown
# in the help or in completion lists.
sub run_ {
    print "Default command...\n";
}

# A standard command. It has a summary (smry_), help topic (help_), and an
# action. But it doesn't provide custom command completion (comp_).
sub smry_fuzz { "A test for bears at play" }
sub help_fuzz {
    <<'END';
Fuzzy bears are harsh quacked, man.
END
}
sub run_fuzz {
    my $o = shift;
    print "Please enter the name of your mother.\n";
    my $l = $o->prompt('Name: ', undef, [qw(Jill Mary Blanche)]);
    print "Say hi to $l for me!\n";
}

# This command ('proxy') runs 'foo' and prints its return value (42).
sub run_proxy {
    my $o = shift;
    my $c = shift;
    my $r = $o->run($c || "foo", @_);
    print "Foo returned: ", $r, "\n";
    print Dumper $o->{API}{command};
}

sub catch_run {
    my $o = shift;
    my $cmd = shift;
    print "NOTE: catch_run() called. Emulating $cmd()\n";
    print Dumper \@_;
}

# This command ('squiggle') has two aliases ('foo', 'bar'). It doesn't have a
# summary or a help topic. It does provide custom command completion, though.
# If you try to complete the line after typing 'squiggle' (or 'foo' or 'bar'),
# you will be able to complete to any of the words qw(all work and no play is
# no fun at). Just for fun.
sub run_squiggle {
    print "Squiggle!\n";
    return 42;
}
sub comp_squiggle {
    my $o = shift;
    my $word = shift;
    $o->{SHELL}{num}++;
    $o->completions($word, [qw(all work and no play is no fun at)]);
}
sub alias_squiggle { qw(foo bar) }

# You can override the prompt
sub prompt_str {
    my $o = shift;
    $o->{SHELL}{num}++;
    "test:$o->{SHELL}{num}> ";
}

sub run_attribs {
    my $o = shift;
    my $term = $o->term;
    print Dumper $term->Features;

    my @keys = qw(
	readline_name
	basic_word_break_characters
    );
    print Dumper $term->Attribs->{$_} for @keys;
}

package main;

if ($ENV{TEST_INTERACTIVE} or not (exists $ENV{MAKELEVEL} or exists $ENV{__MKLVL__}))
{
    print <<END;
==============================================================================
Type 'help' to see a list of commands or help topics. If your terminal
supports tab-completion (and Term::ReadLine supports it too), then you should
be able to hit tab to complete the command-line.

Have fun!
==============================================================================
END
    my $app = app->new('default');
    my $term = $app->term;
    warn "Using term $term\n";
    $app->cmdloop;
}
else {
    print <<END;
==============================================================================
To test the actual command-line client, please set TEST_INTERACTIVE in your
environment and rerun 'make test'.

Alternatively, you can run 'perl -Mblib test.pl'.

Have fun!
==============================================================================
END
}