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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#!/usr/bin/perl
#***************************************************************************
# EasyView -- Unix File Viewer/Editor Interface
# (a "practical" demo for menu.pl)
#
# Notes: Perl4 - Requires curseperl
# Perl5 - Requires William Setzer's "Curses" extension
#
# Author: Steven L. Kunz
# Networked Applications
# Iowa State University Computation Center
# Ames, IA 50011
# Email: skunz@iastate.edu
#
# Date: February 1997
#****************************************************************************
# Perl5+Curses ONLY!
# Comment these lines for use with Perl4/curseperl
BEGIN { $Curses::OldCurses = 1; }
use Curses; # PerlMenu needs "Curses"
use perlmenu; # Main menu package (Perl5 only)
require "./menuutil.pl"; # For "pause" and "print_nl" routines.
# Perl4/curseperl ONLY!
# Uncomment these lines for use with Perl4/curseperl
# (Did you remember to run "create_menu.pl"?)
#require "./menu.pl"; # Main menu package (Perl4 only)
#require "./menuutil.pl"; # For "pause" and "print_nl" routines.
$SIG{'INT'} = 'cleanup'; # Set signal handler
$| = 1; # Flush after every write to stdout
$last_arrow = 0; # For arrow latching
$last_top = 0; # For arrow latching
#
# MAIN_MENU -- Main (top level) menu
#
while (1) {
&menu_init(1,"EasyView Version 4.0");
&menu_item("Exit","%UP%");
&menu_item("List files in current directory","dir_list");
&menu_item("Page through text files","page_file");
&menu_item("Edit text files","edit_file");
$subr = &menu_display("",$last_arrow,$last_top);
if ($subr eq "%UP%") { &cleanup; }
if ($subr ne "") { &$subr; } # Call subroutine selected
}
#**********
# DIR_LIST -- Provide directory list
#**********
sub dir_list { &dir_select(0,".","Directory Contents"); }
#***********
# PAGE_FILE -- Page through a file in the current directory
#
# Arguments: None
#
# Returns: Nothing
#
# Note: Uses file as an unnumbered menu
#***********
sub page_file {
local($filename,$last_arrow,$last_top);
# Call utility function to select file
$filename = &dir_select(1,".","Select one or more files to page through");
if (($filename eq "%UP%") || ($filename eq "%NONE%")) { return; }
# Split up selections, page through each
split(/[,]/,$filename); # Put return in @_
foreach (@_) { &menu_pager($_); }
}
#**********
# MENU_PAGER -- A simple pager written in menu routines
#**********
sub menu_pager {
local($filename) = @_;
# Load file as an unnumbered menu - let menu_display do the paging
#
# Special thanks: The tab expansion used here was lifted from the
# "pager" program distributed with perl.4.36 in the "usub" directory.
# Don't know who wrote it but it fit the bill. SLK
#
&menu_init(0,"File: $filename");
open(TEMP,$filename);
while (<TEMP>) {
s/^(\t+)/' ' x length($1)/e;
&expand($_) if /\t/;
&menu_item($_,"");
}
&menu_display("",$last_arrow,$last_top);
close(TEMP);
}
# Expand tabs to blanks
sub expand {
while (($off = index($_[0],"\t")) >= 0) {
substr($_[0], $off, 1) = ' ' x (8 - $off % 8);
}
}
#***********
# EDIT_FILE -- Edit a file in the current directory
#***********
sub edit_file {
&clear;
$filename = &dir_select(1,".","Select one or more files to edit");
if (($filename eq "%UP%") || ($filename eq "%NONE%")) { return; }
# Split up selections, edit each
split(/[,]/,$filename); # Put return in @_
foreach (@_) { system("vi $_"); }
}
#************
# DIR_SELECT -- Load a formatted directory list into a menu.
#
# Arguments: Boolean flag indicating numbered menu (1=yes), directory
# name string and top-title string for menu
#
# Returns: File name (or "%UP%" or "%NONE%")
#************
sub dir_select {
local($numbered,$directory,$title) = @_;
local($last_arrow,$last_top) = 0;
opendir(DIR,$directory);
&menu_init($numbered,$title);
dir_entry:
while ($filename = readdir(DIR)) {
next dir_entry if ($filename eq ".");
next dir_entry if ($filename eq "..");
next if (! -f $filename);
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
$blksize,$blocks) = stat($filename);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
$mon++;
$sel_action = $filename;
$filename=$filename.substr(" ",0,20-length($filename));
$sel_text = sprintf("%s%s%02d/%02d/%02d %02d:%02d %s\n",
$filename,$filler,$mon,$mday,$year,$hour,$sec,getpwuid($uid));
&menu_item($sel_text,$sel_action);
}
if ($numbered) {
$fn = &menu_display_mult("","");
} else {
$fn = &menu_display("");
}
$fn;
}
#**********
# Cleanup routine (called upon exit)
#**********
sub cleanup {
&clear;
&refresh;
&endwin;
exit;
}
|