LineEditor.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 100 CC 10 SCAR 43.0
CC
10
Cov
0%
line_editor
110.0
find_editor
2.0
BEGIN
2.0
CRAP 74.0
1 package Text::LineEditor;
2 #
3 # ObLegalStuff:
4 # Copyright (c) 1998 Bek Oberin. All rights reserved. This program is
5 # free software; you can redistribute it and/or modify it under the
6 # same terms as Perl itself.
7 #
8 # Last updated by gossamer on Sun Sep 6 21:03:23 EST 1998
9 #
10  
11 use strict;
12 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
13  
14 require Exporter;
15  
16 use Term::ReadLine;
17  
18 @ISA = qw(Exporter);
19 @EXPORT = qw( line_editor );
20 @EXPORT_OK = qw();
21  
22 $VERSION = "0.03";
23  
24 #
25 # Constants
26 #
27  
28 # When starting editing
29 my $Initial_Prompt =
30 "Enter your message: ('.' by itself on a line to end, ~h for help)";
31  
32 # Before each line is input
33 my $Line_Prompt = "> ";
34  
35 # Before editor messages
36 my $Message_Prompt = "*** ";
37  
38 my $Tempfile_Name = "/tmp/lineeditor.$$";
39  
40 #
41 # Function
42 #
43  
44 sub find_editor {
45 return $ENV{"VISUAL"} || $ENV{"EDITOR"} || "vim" || "vi" || "pico";
46 }
47  
48 =head1 NAME
49  
50 Text::LineEditor - simple line editor
51  
52 =head1 SYNOPSIS
53  
54 use Text::LineEditor;
55  
56 $text = &line_editor();
57  
58 =head1 DESCRIPTION
59  
60 C<Text::LineEditor> implements a -very- simple editor like Berkley
61 mail used to use.
62  
63 =head1 FUNCTIONS
64  
65 =item line_editor ();
66  
67 Returns the text entered. If the text has been abandoned with
68 ~x it returns an empty string.
69  
70 =head1 EDITING COMMANDS
71  
72 To append text, just type.
73 To end, type '.' by itself on a line.
74 Tilde commands for editing (by themselves on a line):
75 ~h This help message
76 ~e, ~v Edit message using visual editor
77 ~w <filename>
78 Write current text to file <filename>
79 ~p Print current text using 'lpr'
80 ~. End message as normal
81 ~x Quit, abandon text
82  
83 =cut
84 sub line_editor {
85 my $text;
86  
87 my $finished = 0;
88  
89 my $Input = new Term::ReadLine 'LineEditor';
90  
91 print $Initial_Prompt . "\n";
92 while (!$finished) {
93  
94 my $line = $Input->readline($Line_Prompt); # NB readline() removes \n
95  
96 if ($line =~ m/^\.$/) {
97 # dot by itself - end
98 $finished++;
99  
100 } elsif ($line =~ m/^~(.)(.*)$/) {
101 # Something magic
102  
103 if ($1 eq 'h') {
104 # give help
105 print <<"EOT";
106 To append text, just type.
107 To end, type '.' by itself on a line.
108 Tilde commands for editing (by themselves on a line):
109 ~h This help message
110 ~e, ~v Edit message using visual editor
111 ~w <filename>
112 Write current text to file <filename>
113 ~p Print current text using 'lpr'
114 ~. End message as normal
115 ~x Quit, abandon text
116 EOT
117 } elsif ($1 eq '.') {
118 # same as regular ending
119 $finished++;
120 } elsif ($1 eq 'w') {
121 # write to a file
122 open(OUTFILE, ">$2");
123 print OUTFILE $text;
124 close(OUTFILE);
125 } elsif ($1 eq 'p') {
126 # print
127 open PRINTER, "| lpr" ||
128 warn "Can't open printer: $!\n";
129 print PRINTER $text;
130 close PRINTER;
131 } elsif (($1 eq 'e') || ($1 eq 'v')) {
132 # visual editor
133 my $editor = &find_editor;
134 if ($editor) {
135 open(OUTFILE, ">$Tempfile_Name");
136 print OUTFILE $text;
137 close(OUTFILE);
138  
139 system($editor, $Tempfile_Name);
140  
141 open(OUTFILE, "<$Tempfile_Name");
142 undef $/;
143 $text = <OUTFILE>;
144 close(OUTFILE);
145  
146 #print "DEBUG: '$text'\n";
147 } else {
148 print $Message_Prompt . "Can't find an editor to use!\n";
149 }
150  
151 } elsif ($1 eq 'x') {
152 $finished++;
153 $text = "";
154 } else {
155 print $Message_Prompt . "Unknown tilde escape '$line'\n";
156 }
157  
158 } else {
159 # regular line
160  
161 $text .= $line . "\n";
162 }
163 }
164  
165 return $text;
166 }
167  
168 #
169 # End.
170 #
171 1;