File: browse

package info (click to toggle)
perl-tk 1%3A804.036%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,284 kB
  • sloc: ansic: 349,560; perl: 52,292; sh: 12,678; makefile: 5,700; asm: 3,565; ada: 1,681; pascal: 1,082; cpp: 1,006; yacc: 883; cs: 879
file content (91 lines) | stat: -rwxr-xr-x 2,038 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/env perl
#
# This script generates a directory browser, which lists the working directory and allows you to open files or subdirectories
# by double-clicking.
#
# Tcl/Tk -> Perl translation by Stephen O. Lidie.  lusol@Lehigh.EDU  96/01/24

require 5.002;
use English;
use File::Basename;
use Tk;
use strict;
sub browse;

sub invokeself
{
 my @args = ($^X,__FILE__,@_);
 system(join(' ',@args));
}

my $MW = MainWindow->new;

# Create a scrollbar on the right side of the main window and a listbox on
# the left side.

my $scroll = $MW->Scrollbar();
$scroll->pack(-side => 'right', -fill => 'y');
my $list = $MW->Listbox(
    -yscrollcommand => ['set', $scroll],
    -relief => 'sunken',
    -width => 20,
    -height => 20,
    -setgrid => 'yes',
);
$list->pack(-side => 'left', -fill => 'both', -expand => 'yes');
$scroll->configure(-command => ['yview', $list]);
$MW->minsize(1, 1);

# Fill the listbox with a list of all the files in the directory.

my $dir;
if (scalar @ARGV > 0) {
    $dir = $ARGV[0];
} else {
    $dir = '.';
}
foreach (<${dir}/*>) {
    $list->insert('end', basename($::ARG));
}

# Set up bindings for the browser.

$list->bind('all', '<Control-c>' => \&exit);
$list->bind('<Double-Button-1>' => sub {
    my($listbox) = @_;
    foreach (split ' ', $listbox->get('active')) {
	browse $dir, $::ARG;
    }
});

MainLoop;


sub browse {

    # The procedure below is invoked to open a browser on a given file;
    # if the file is a directory then another instance of this program is
    # invoked; if the file is a regular file then an editor is invoked to
    # display the file.

    my($dir, $file) = @_;

    if ($dir ne '.') {
	$file = "$dir/$file";
    }
    if (-d $file) {
	invokeself("$file &");
    } else {
	if (-f $file) {
	    print STDOUT "Viewing file $file ...\n";
	    if (defined $ENV{'EDITOR'}) {
		system "$ENV{'EDITOR'} $file &";
	    } else {
		system "vi $file &";
	    }
	} else {
	    print STDOUT "\"$file\" isn't a directory or regular file\n";
	}
    } # ifend

} # end browse