| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# fileman |
| 4 |
# Scott Bronson |
| 5 |
# 8 Dec 2003 |
| 6 |
|
| 7 |
# This file shows how to use Term::ShellUI to create a simple |
| 8 |
# command-line environment. It is similar to fileman.c, the |
| 9 |
# example that comes with the GNU Readline library. |
| 10 |
|
| 11 |
# This file is released under the MIT license. |
| 12 |
|
| 13 |
use strict; |
| 14 |
use lib '../lib'; |
| 15 |
use Term::ShellUI; |
| 16 |
use File::stat qw(:FIELDS); |
| 17 |
use Cwd; |
| 18 |
|
| 19 |
|
| 20 |
# Prevents us from doing anything harmful. |
| 21 |
# Have it return 1 to live on the edge. |
| 22 |
sub danger |
| 23 |
{ |
| 24 |
print "Not performing task -- too dangerous!\n"; |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
my $term = new Term::ShellUI(commands => get_commands()); |
| 30 |
$term->prompt(sub { getcwd() . " FM> " }); |
| 31 |
$term->run(); |
| 32 |
|
| 33 |
sub get_commands |
| 34 |
{ |
| 35 |
return { |
| 36 |
"cd" => { desc => "Change to directory DIR", |
| 37 |
args => sub { shift->complete_onlydirs(@_); }, |
| 38 |
maxargs => 1, |
| 39 |
proc => sub { chdir($_[0] || $ENV{HOME} || $ENV{LOGDIR}) or print("Could not cd: $!\n") } }, |
| 40 |
"delete" => { desc => "Delete FILEs", |
| 41 |
args => sub { shift->complete_onlyfiles(@_); }, |
| 42 |
minargs => 1, |
| 43 |
proc => sub { danger() && (unlink(@_) or warn "Could not delete: $!\n") } }, |
| 44 |
"?" => { syn => "help" }, |
| 45 |
"help" => { desc => "Print helpful information", |
| 46 |
args => sub { shift->help_args(undef, @_); }, |
| 47 |
meth => sub { shift->help_call(undef, @_); } }, |
| 48 |
"ls" => { syn => "list" }, |
| 49 |
"dir" => { syn => "ls" }, |
| 50 |
"list" => { desc => "List files in DIRs", |
| 51 |
args => sub { shift->complete_onlydirs(@_); }, |
| 52 |
proc => sub { system('ls', '-FClg', @_); } }, |
| 53 |
"pwd" => { desc => "Print the current working directory", |
| 54 |
maxargs => 0, |
| 55 |
proc => sub { system('pwd'); } }, |
| 56 |
"quit" => { desc => "Quit using Fileman", |
| 57 |
maxargs => 0, |
| 58 |
meth => sub { shift->exit_requested(1); } }, |
| 59 |
"rename" => { desc => "Rename FILE to NEWNAME", |
| 60 |
args => sub { shift->complete_files(@_); }, |
| 61 |
minargs => 2, maxargs => 2, |
| 62 |
proc => sub { danger() && system('mv', @_); } }, |
| 63 |
"stat" => { desc => "Print out statistics on FILEs", |
| 64 |
args => sub { shift->complete_files(@_); }, |
| 65 |
proc => \&print_stats }, |
| 66 |
"cat" => { syn => "view" }, |
| 67 |
"view" => { desc => "View the contents of FILEs", |
| 68 |
args => sub { shift->complete_onlyfiles(@_); }, |
| 69 |
proc => sub { system('cat', @_); } }, |
| 70 |
# these demonstrate how parts of Term::ShellUI work: |
| 71 |
echo => { desc => "Echoes the command-line arguments", |
| 72 |
proc => sub { print join(" ", @_), "\n"; } }, |
| 73 |
tok => { desc => "Print command-line arguments with clear separations", |
| 74 |
proc => sub { print "<" . join(">, <", @_), ">\n"; } }, |
| 75 |
debug_complete => { desc => "Turn on completion debugging", |
| 76 |
minargs => 1, maxargs => 1, args => "0=off 1=some, 2=more, 3=tons", |
| 77 |
proc => sub { $term->{debug_complete} = $_[0] }, |
| 78 |
}, |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
sub print_stats |
| 83 |
{ |
| 84 |
for my $file (@_) { |
| 85 |
stat($file) or die "No $file: $!\n"; # die only stops this command |
| 86 |
print "$file has $st_nlink link" . ($st_nlink==1?"":"s") . |
| 87 |
", and is $st_size byte" . ($st_size==1?"":"s") . " in length.\n"; |
| 88 |
print "Inode Last change at: " . localtime($st_ctime) . "\n"; |
| 89 |
print " Last access at: " . localtime($st_atime) . "\n"; |
| 90 |
print " Last modified at: " . localtime($st_mtime) . "\n"; |
| 91 |
} |
| 92 |
} |
| 93 |
|