Debian Codegraph @

sqlite_formatter

raw ◀ ▶
1 Debian patch touch this file
•1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 #
5 # Pretty printer for from sqlite3 output.
6 # Assumes column headers are enabled with
7 # .header on
8 # in ~/.sqliterc
9 # [email protected] March 2010
10 #
11 # This program reads input from STDIN and writes to STDOUT. A typical
12 # invocation looks like this:
13 # sqlite3 code.db 'select project,file,nCode from t' | sqlite_formatter
14
15 my $sqlite_separator = '\|';
16
17 my @lines = ();
18
19 # ingest all lines
20 while (<>) {
21 next if /^\s*$/; # skip blank
22 next if /^--\s+/; # skip comments
23 chomp;
24 push @lines, $_;
25 }
26
27 # split on |, the default sqlite3 output separator
28 my @column_width = ();
29 my $nCols = 0;
30 my @data = (); # $data[$row][$column] = SQLite result
31 for (my $row = 0; $row < scalar @lines; $row++) {
32 my @cols = split($sqlite_separator, $lines[$row]);
33 $nCols = scalar @cols;
34 for (my $col = 0; $col < $nCols; $col++) {
35 $data[$row][$col] = $cols[$col];
36 $column_width[$col] = 0 unless defined $column_width[$col];
37 $column_width[$col] = length($cols[$col]) if
38 $column_width[$col] < length($cols[$col]);
39 }
40 }
41
42 # write the results
43 my $format_str = "";
44 for (my $row = 0; $row < scalar @data; $row++) {
45 # insert a separator row
46 if ($row == 1) {
47 for (my $col = 0; $col < $nCols; $col++) {
48 print '_' x $column_width[$col], " ";
49 }
50 print "\n";
51 }
52 for (my $col = 0; $col < $nCols; $col++) {
53 if ($data[$row][$col] =~ /^[-]?\d+$/) {
54 # an integer, align to right
55 $format_str = "%" . $column_width[$col] . "d ";
56 } else {
57 $format_str = "%-" . $column_width[$col] . "s ";
58 } # could use something for floating point numbers too...
59 printf $format_str, $data[$row][$col];
60 }
61 print "\n";
62 }