| 1 | #!/usr/bin/env pugs |
|---|
| 2 | use v6; |
|---|
| 3 | |
|---|
| 4 | ## Author: Austin Seipp, 2007 |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | #Commands our shell can execute |
|---|
| 8 | my %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 |
|---|
| 17 | my @history = (); |
|---|
| 18 | my %stats = ( |
|---|
| 19 | "count" => 1, |
|---|
| 20 | "lastexec" => "nil", |
|---|
| 21 | "history" => @history, |
|---|
| 22 | ); |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | #main loop |
|---|
| 26 | loop { |
|---|
| 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 |
|---|
| 48 | sub cmd_about { |
|---|
| 49 | "promptr: perl 6 prompt".say |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | sub cmd_history { |
|---|
| 54 | for (1..%stats<count>-1) -> $i { |
|---|
| 55 | say " " ~ $i ~ ") " ~ %stats<history>[$i-1]; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | sub cmd_last { |
|---|
| 60 | say %stats<lastexec>; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | sub cmd_help { |
|---|
| 64 | for %cmds.kv -> ($cmd,@values) { |
|---|
| 65 | say " " ~ $cmd ~ "\t" ~ (((split "",$cmd).elems < 8) ?? "\t" !! "") ~ @values[0]; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | sub cmd_quit { |
|---|
| 70 | "Leaving promptr...".say; |
|---|
| 71 | exit; |
|---|
| 72 | } |
|---|