[interchange-cvs] interchange - jon modified eg/te

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Wed Sep 4 00:20:05 2002


User:      jon
Date:      2002-09-04 04:19:06 GMT
Modified:  eg       te
Log:
Add some new options:

-f to handle files without field names on first line of file.
-n to number rows in comments.

Allow setting of persistent options in environment variable TE_OPTIONS.

Handle -i and -s with mixed-case search term (lowercase it first).

Don't escape " in search term, as it doesn't seem special.

Revert indentation to tabs coming in separate commit for clarity ...

Revision  Changes    Path
2.5       +49 -31    interchange/eg/te


rev 2.5, prev_rev 2.4
Index: te
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /var/cvs/interchange/eg/te,v
retrieving revision 2.4
retrieving revision 2.5
diff -u -u -r2.4 -r2.5
--- te	31 Aug 2002 06:19:00 -0000	2.4
+++ te	4 Sep 2002 04:19:05 -0000	2.5
@@ -4,8 +4,8 @@
=20
 =3Dhead1 NAME
=20
-te (table editor) - front-end for simplifying editing tab-delimited
-ASCII databases
+te (table editor) - front-end that simplifies editing tab-delimited
+ASCII tables
=20
 =3Dhead1 SYNOPSIS
=20
@@ -13,8 +13,9 @@
=20
 =3Dhead1 DESCRIPTION
=20
-This program makes it easier to edit tab-delimited ASCII databases,
-such as are used with Red Hat Interchange.
+This program makes it easier to edit tab-delimited ASCII tables, such
+as are used with Interchange (see icdevgroup.org), and can be exported
+from many popular spreadsheet and database applications.
=20
 It converts tab-delimited ASCII files that have one record per line into
 temporary files with one field per line, each line beginning with the
@@ -55,6 +56,9 @@
 variables VISUAL or EDITOR to point to your favorite text editor. If
 neither of those is set, my favorite editor, B<vi>(1) is used.
=20
+Options will also be read from environment variable TE_OPTIONS if it is
+set.
+
 =3Dhead1 LIMITATIONS
=20
 There is currently no way to add or delete entire columns from the
@@ -80,7 +84,7 @@
=20
 =3Dhead1 VERSION
=20
-$Id $
+$Id: te,v 2.5 2002/09/04 04:19:05 jon Exp $
=20
 =3Dhead1 CHANGELOG
=20
@@ -103,12 +107,17 @@
 solitary CR in the last field of a line is important, you'll want to
 change this behavior.
=20
-2002-08-40. Add option -s for starting value support (really only vi).
+2002-08-30. Add option -s for starting value support (really only vi).
=20
 	te -s os28004 <file>
=20
 Jumps to first occurrence of "os28004" in <file>. Option -i ignores case
-in the search.
+in the search. (By Mike Heins.)
+
+2002-09-02. Add option -f to handle files without field names.
+
+2002-09-03. Add option -n to number rows in comments. Allow setting of
+persistent options in environment variable TE_OPTIONS.
=20
 =3Dcut
=20
@@ -129,38 +138,36 @@
 Options:
         -i       Ignores case on vim jump search.
         -s TEXT  Jumps to first line where TEXT is. Only for vim.
+	-f       Do not look for field names on first line of file.
+	-n       Number rows in comments
+
+See 'man te' or 'perldoc $0' for more information.
+
 EOF
=20
-use vars qw/$opt_i $opt_s/;
-getopts('is:') or die "$@\n$USAGE";
+unshift @ARGV, Text::ParseWords::shellwords($ENV{TE_OPTIONS})
+	if defined $ENV{TE_OPTIONS};
+
+use vars qw/$opt_i $opt_s $opt_f $opt_n/;
+getopts('is:fn') or die "$@\n$USAGE";
=20
 die $USAGE unless @ARGV;
=20
 my @ED =3D Text::ParseWords::shellwords($ENV{VISUAL} || $ENV{EDITOR} || 'v=
i');
=20
-if($opt_s) {
-        my $pushed;
-        if($opt_i) {
-                push @ED, '-c';
-                $pushed++;
-                push @ED, q{set ic};
-        }
-        push @ED, '-c';
-        $opt_s =3D~ s:/:\\/:g;
-        $opt_s =3D~ s:":\\":g;
-        push @ED, qq{/$opt_s/};
+if ($opt_s) {
+	if ($opt_i) {
+		push @ED, '-c', 'set ic';
+		$opt_s =3D lc $opt_s;
+	}
+	$opt_s =3D~ s:/:\\/:g;
+	push @ED, '-c', qq{/$opt_s/};
 }
=20
 # run gvim in foreground mode, since it otherwise immediately returns
 # control to us and we never get the user's changes
 if($ED[0] =3D~ /\bgvim\b/) {
-        my $ffound;
-        for(@ED) {
-                next unless $_ eq '-f';
-                $ffound =3D 1;
-                last;
-        }
-        push @ED, '-f' unless $ffound;
+	push @ED, '-f' unless grep $_ eq '-f', @ED;
 }
=20
 for my $filename (@ARGV) {
@@ -178,24 +185,33 @@
                 warn "Error 'opening' $filename for reading: $!\n";
                 next;
         }
+
+	# get field names
         $_ =3D <IN>;
         s/\x0d?\x0a?$//;
-        die "Error in '$filename' header: null field name found\n" if /\t\=
t/;
         $fieldcount =3D tr/\t/\t/ + 1;
+	if ($opt_f) {
+		@fieldnames =3D map { "field$_" } (1 .. $fieldcount);
+		seek IN, 0, 0;
+	}
+	else {
+		die "Error in '$filename' header: null field name found\n" if /\t\t/;
         @fieldnames =3D split /\t/, $_, $fieldcount;
+	}
+
         ($name, $path) =3D fileparse($filename);
=20
         # I tried keeping the whole file in memory (for MD5's sake) instea=
d of
         # first writing to disk, but doing it this way turned out to be ab=
out 5
         # times faster and used 1/10th the memory on large files. (My benc=
hmark
-        # was a 12 MB products.txt database for Interchange.)
+	# was a 12 MB products.txt table for Interchange.)
=20
         $tmpfile =3D "$path.$name.tmp.$$";
         open OUT, ">$tmpfile" or die "Error opening '$tmpfile' for writing=
: $!\n";
         print STDERR "Prettifying $filename\n";
         print OUT <<EOF;
 #
-# This is a temporary file, automatically generated from the database file:
+# This is a temporary file, automatically generated from the data file:
 #
 # $filename
 #
@@ -203,8 +219,10 @@
 # format and will replace the original file.
 #
 EOF
+	my $rowcount =3D 0;
         while (<IN>) {
                 s/\x0d?\x0a?$//;
+		++$rowcount, print OUT "# row $rowcount\n" if $opt_n;
                 @fields =3D split /\t/, $_, $fieldcount;
                 for (my $i =3D 0; $i < @fieldnames; $i++) {
                         print OUT $fieldnames[$i], ":",
@@ -261,7 +279,7 @@
         print STDERR "Importing changes back into '$filename'\n";
         $newfile =3D "$path.$name.new.$$";
         open OUT, ">$newfile" or die "Error opening '$newfile' for writing=
: $!\n";
-        print OUT join("\t", @fieldnames), "\n";
+	print OUT join("\t", @fieldnames), "\n" unless $opt_f;
         my $tabcounter =3D 0;
         my $fieldpos =3D 0;
         my $done;