3 #=======================================================================
7 # Display word count statistics
10 # ©opyleft 2015– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
33 $progname =~ s/^.*\/(.*?)$/$1/;
34 our $VERSION = '0.2.1';
36 Getopt
::Long
::Configure
('bundling');
39 'colour|c' => \
$Opt{'colour'},
40 'data' => \
$Opt{'data'},
41 'help|h' => \
$Opt{'help'},
42 'quiet|q+' => \
$Opt{'quiet'},
43 'verbose|v+' => \
$Opt{'verbose'},
44 'version' => \
$Opt{'version'},
46 ) || die("$progname: Option error. Use -h for help.\n");
48 $Opt{'verbose'} -= $Opt{'quiet'};
49 $Opt{'help'} && usage
(0);
50 if ($Opt{'version'}) {
55 my $sql_error = 0; # Set to !0 if some sqlite3 error happened
65 print(format_data
(join('', <>)));
69 my $lockdir = "dat/STDprojnameDTS.lock";
71 until (mkdir($lockdir)) {
72 msg
(0, "$progname: $lockdir: Waiting for lockdir");
77 msg
(0, "$progname: Lockdir aquired");
80 if (-e
"dat/STDprojnameDTS.spar") {
81 warn("$progname: dat/STDprojnameDTS.spar already exists\n");
86 my $tmpfile = "dat/.STDprojnameDTS.spar.$$.tmp";
87 system("sqlite3 $tmpfile <dat/STDprojnameDTS.sql");
88 rename($tmpfile, "dat/STDprojnameDTS.spar");
89 my $spardata = `spar -d dat -s STDprojnameDTS`;
90 my $currdb = $spardata; # Current value stored in the database
91 $currdb =~ s/^.*?\n(.*?)\n.*$/$1/s;
92 msg
(1, "currdb after regexp = '$currdb'");
93 $currdb = int($currdb);
94 msg
(1, "currdb = '$currdb'");
95 $data = format_data
(join('', <>));
96 my $totwords = word_count
($data);
97 if ($currdb != $totwords) {
98 $spardata = `spar -d dat -s STDprojnameDTS -c $totwords`;
100 my $spar = $spardata; # Current goal to reach
102 my $spartime = $spardata; # Days, hours, minutes, etc behind/ahead
103 $spartime =~ s/^.*\n(.*?),.*?\n$/$1/s;
104 my $currstat = $totwords - $spar;
111 if ($Opt{'colour'}) {
112 $C_BOLD = `tput bold`;
113 $C_GREEN = `tput setaf 2`;
114 $C_RED = `tput setaf 1`;
115 $C_RESET = `tput sgr0`;
118 my $textcol = $C_BOLD . ($currstat < 0 ?
$C_RED : $C_GREEN);
121 "Status: %s%.2f %s(%s%s%s)\n" .
133 system("sqlite3 dat/STDprojnameDTS.spar .dump >$tmpfile");
134 rename($tmpfile, "dat/STDprojnameDTS.sql");
135 unlink("dat/STDprojnameDTS.spar");
145 $data =~ s/^.*?TEXT_BEGIN\s+-->\s*//s;
146 $data =~ s/\s*<!--\s+TEXT_END.*?$//s;
147 $data =~ s/<!--.*?-->//gs;
148 msg
(4, "data = '$data'");
158 msg
(3, "sql(): db = '$db'");
159 local(*CHLD_IN
, *CHLD_OUT
, *CHLD_ERR
);
162 my $pid = open3
(*CHLD_IN
, *CHLD_OUT
, *CHLD_ERR
, "sqlite3", $db) or (
164 msg
(0, "sql(): open3() error: $!"),
165 return("sql() error"),
167 msg
(3, "sql(): sql = '$sql'");
168 print(CHLD_IN
"$sql\n") or msg
(0, "sql(): print CHLD_IN error: $!");
170 @retval = <CHLD_OUT
>;
171 msg
(3, "sql(): retval = '" . join('|', @retval) . "'");
172 my @child_stderr = <CHLD_ERR
>;
173 if (scalar(@child_stderr)) {
174 msg
(0, "sqlite3 error: " . join('', @child_stderr));
177 return(join('', @retval));
184 $data =~ s/\s+/\n/sg;
185 my $retval = scalar(split(/\s+/, $data));
191 # Print program version {{{
192 print("$progname $VERSION\n");
198 # Send the help message to stdout {{{
201 if ($Opt{'verbose'}) {
207 Display word count statistics
209 Usage: $progname [options] [file [files [...]]]
214 Use colour in output; red if behind schedule, green if ahead.
216 Output text data instead of displaying stats.
220 Be more quiet. Can be repeated to increase silence.
222 Increase level of verbosity. Can be repeated.
224 Print version information.
232 # Print a status message to stderr based on verbosity level {{{
233 my ($verbose_level, $Txt) = @_;
235 if ($Opt{'verbose'} >= $verbose_level) {
236 print(STDERR
"$progname: $Txt\n");
244 # This program is free software; you can redistribute it and/or modify
245 # it under the terms of the GNU General Public License as published by
246 # the Free Software Foundation; either version 2 of the License, or (at
247 # your option) any later version.
249 # This program is distributed in the hope that it will be useful, but
250 # WITHOUT ANY WARRANTY; without even the implied warranty of
251 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
252 # See the GNU General Public License for more details.
254 # You should have received a copy of the GNU General Public License
255 # along with this program.
256 # If not, see L<http://www.gnu.org/licenses/>.
258 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :