root/examples/promptr.p6

Revision 16452, 1.4 kB (checked in by thoughtpolice, 18 months ago)

added examples/promptr.p6

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env pugs
2use v6;
3
4## Author: Austin Seipp, 2007
5
6
7#Commands our shell can execute
8my %cmds  = (
9             ":about"   => ("about this program",&cmd_about),
10             ":history" => ("gives command history",&cmd_history),
11             ":last"    => ("last command given",&cmd_last),
12             ":help"    => ("this command",&cmd_help),
13             ":quit"    => ("exits prompt",&cmd_quit),
14);
15
16#info used by the commands, used for various purposes
17my @history = ();
18my %stats   = (
19             "count"    => 1,
20             "lastexec" => "nil",
21             "history"  => @history,
22);
23
24
25#main loop
26loop {
27  print %stats<count>++ ~ ") ";
28  my $in = =$*IN;
29  %stats<history>.push($in);
30  my ($cmd,@ops) = split " ",$in;
31  %stats<lastexec> = ($cmd eq ":last") ?? %stats<lastexec> !! $in;
32  if ($cmd eq any(%cmds.keys)) == True {
33    for %cmds.keys -> $command {
34      if $cmd eq $command {
35        %cmds{$command}[1](@ops);
36        last;
37      }
38    }
39  } else {
40    say "err: invalid command, try :help for help.";
41  }
42}
43
44
45
46
47#these are our command functions
48sub cmd_about {
49  "promptr: perl 6 prompt".say
50}
51
52
53sub cmd_history {
54  for (1..%stats<count>-1) -> $i {
55    say "  " ~ $i ~ ") " ~ %stats<history>[$i-1];
56  }
57}
58
59sub cmd_last {
60  say %stats<lastexec>;
61}
62
63sub cmd_help {
64  for %cmds.kv -> ($cmd,@values) {
65    say " " ~ $cmd ~ "\t" ~ (((split "",$cmd).elems < 8) ?? "\t" !! "") ~ @values[0];
66  }
67}
68
69sub cmd_quit {
70  "Leaving promptr...".say;
71  exit;
72}
Note: See TracBrowser for help on using the browser.