| 1 |
#!/usr/bin/env perl |
| 2 |
# |
| 3 |
# This script generates a directory browser, which lists the working directory and allows you to open files or subdirectories |
| 4 |
# by double-clicking. |
| 5 |
# |
| 6 |
# Tcl/Tk -> Perl translation by Stephen O. Lidie. [email protected] 96/01/24 |
| 7 |
|
| 8 |
require 5.002; |
| 9 |
use English; |
| 10 |
use File::Basename; |
| 11 |
use Tk; |
| 12 |
use strict; |
| 13 |
sub browse; |
| 14 |
|
| 15 |
sub invokeself |
| 16 |
{ |
| 17 |
my @args = ($^X,__FILE__,@_); |
| 18 |
system(join(' ',@args)); |
| 19 |
} |
| 20 |
|
| 21 |
my $MW = MainWindow->new; |
| 22 |
|
| 23 |
# Create a scrollbar on the right side of the main window and a listbox on |
| 24 |
# the left side. |
| 25 |
|
| 26 |
my $scroll = $MW->Scrollbar(); |
| 27 |
$scroll->pack(-side => 'right', -fill => 'y'); |
| 28 |
my $list = $MW->Listbox( |
| 29 |
-yscrollcommand => ['set', $scroll], |
| 30 |
-relief => 'sunken', |
| 31 |
-width => 20, |
| 32 |
-height => 20, |
| 33 |
-setgrid => 'yes', |
| 34 |
); |
| 35 |
$list->pack(-side => 'left', -fill => 'both', -expand => 'yes'); |
| 36 |
$scroll->configure(-command => ['yview', $list]); |
| 37 |
$MW->minsize(1, 1); |
| 38 |
|
| 39 |
# Fill the listbox with a list of all the files in the directory. |
| 40 |
|
| 41 |
my $dir; |
| 42 |
if (scalar @ARGV > 0) { |
| 43 |
$dir = $ARGV[0]; |
| 44 |
} else { |
| 45 |
$dir = '.'; |
| 46 |
} |
| 47 |
foreach (<${dir}/*>) { |
| 48 |
$list->insert('end', basename($::ARG)); |
| 49 |
} |
| 50 |
|
| 51 |
# Set up bindings for the browser. |
| 52 |
|
| 53 |
$list->bind('all', '<Control-c>' => \&exit); |
| 54 |
$list->bind('<Double-Button-1>' => sub { |
| 55 |
my($listbox) = @_; |
| 56 |
foreach (split ' ', $listbox->get('active')) { |
| 57 |
browse $dir, $::ARG; |
| 58 |
} |
| 59 |
}); |
| 60 |
|
| 61 |
MainLoop; |
| 62 |
|
| 63 |
|
| 64 |
sub browse { |
| 65 |
|
| 66 |
# The procedure below is invoked to open a browser on a given file; |
| 67 |
# if the file is a directory then another instance of this program is |
| 68 |
# invoked; if the file is a regular file then an editor is invoked to |
| 69 |
# display the file. |
| 70 |
|
| 71 |
my($dir, $file) = @_; |
| 72 |
|
| 73 |
if ($dir ne '.') { |
| 74 |
$file = "$dir/$file"; |
| 75 |
} |
| 76 |
if (-d $file) { |
| 77 |
invokeself("$file &"); |
| 78 |
} else { |
| 79 |
if (-f $file) { |
| 80 |
print STDOUT "Viewing file $file ...\n"; |
| 81 |
if (defined $ENV{'EDITOR'}) { |
| 82 |
system "$ENV{'EDITOR'} $file &"; |
| 83 |
} else { |
| 84 |
system "vi $file &"; |
| 85 |
} |
| 86 |
} else { |
| 87 |
print STDOUT "\"$file\" isn't a directory or regular file\n"; |
| 88 |
} |
| 89 |
} # ifend |
| 90 |
|
| 91 |
} # end browse |