File: showdir.pl

package info (click to toggle)
pdmenu 1.2.59
  • links: PTS
  • area: main
  • in suites: potato
  • size: 536 kB
  • ctags: 251
  • sloc: ansic: 1,999; sh: 297; perl: 75; makefile: 60
file content (89 lines) | stat: -rwxr-xr-x 2,649 bytes parent folder | download | duplicates (10)
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
#!/usr/bin/perl
#
# Syntax: showdir.pl <directory> <old-directory> <menuname>
#         editdir.pl <directory> <old-directory> <menuname>
#
# If called as showdir, it will display files. If called as editdir, 
# it edits files.
#
# Where <directory> is the directory to display, and <old-directory>
# is the optional name of the directory they were on previously.
# Doen't specify <old-directory> yourself, it is only used internally by the
# program.
#
# Generates a pdmenurc file at stdout that lets the user view files in the 
# directory (or edit them if the program is run as "editdir.pl"), and change
# to other directories. The file will define a menu named 
# "show_directory_<directory>", unless the <menuname> option is passed, 
# in which case the menu will be named <namename>

if ($0=~m/editdir/ ne undef) {
	$mode='edit';
	$editor=$ENV{EDITOR} || $ENV{VISUAL} || 'pico';
}
else {
	$mode='show';
}

$dir=shift;
$dir.='/' if $dir=~m#/$# eq undef; # make sure there is a trailing / on $dir.
$olddir=shift;
$olddir.='/' if $olddir=~m#/$# eq undef; # make sure there is a trailing / on $olddir.
$menuname=shift;
$menuname="show_directory_$dir" if !$menuname;

print "menu:$menuname:Files in $dir:";
print "Select a file to $mode it or select a directory to move into it.\n";

opendir(PWD,$dir);
@dirlist=readdir(PWD);
foreach $fn (sort(@dirlist)) {
	# Make a version of $fn that has ':' and '\' and '_' characters
	# escaped out.
	$fne=$fn;
	$fne=~s/\\/\\\\/g;
	$fne=~s/:/\\:/g;
	$fne=~s/_/\\_/g;

	if (-d "$dir/$fn" && $fn ne '.' && $fn ne '..') {
		# See if the directory the selected is the directory we were just on 
		# previously.
		if ("$dir$fn/" ne $olddir) {
			print "group:$fne/\n";
			print "exec::makemenu:$0 $dir$fne/ $dir\n";
			print "show:::show_directory_$dir$fne/\n";
			print "remove:::show_directory_$dir$fne/\n";
			print "endgroup\n";
		}
		else {
			print "exit:$fne/\n";
		}
	}
	elsif ($fn eq '..' && $dir ne '/') {
		# Go down one directory.
		($parent)=$dir=~m:^(.*)/.*?/:;
		$parent.='/';
		# If they select to go down one directory, and the result is the directory
		# we were just on previously, then exit the menu instead of displaying
		# a new one.
		if ($parent ne $olddir) {
			print "group:$fne/\n";
			print "exec::makemenu:$0 $parent $dir\n";
			print "show:::show_directory_$parent\n";
			print "remove:::show_directory_$parent\n";
			print "endgroup\n";
		}
		else {
			print "exit:$fne/\n";
		}
	}
	elsif ($fn ne '.' && $fn ne '..') {
		if ($mode eq 'show') {
			print "exec:$fne:display:cat $dir$fn 2>/dev/null\n";
		}
		else {
			print "exec:$fne:pause:$editor $dir$fn\n";
		}
	}
}
closedir(PWD);