#!/usr/bin/perl use strict; use warnings; use Getopt::Std; our $VERSION = '0.8.6'; $Getopt::Std::STANDARD_HELP_VERSION = 'true'; my $help = ' syntax: wcpercent [--help] wcpercent [-d ] [ -f ] [-g ] [ -l ] [] . . . --help prints this help text and exits: invoking the help argument causes all other arguments to be disregarded by this utility -d takes a path relative to the current working directory, or an absolute path, as the directory on which the utility operates (deprecated: use the arguments instead, as this is included only for backward compatibility) -f takes a path and filename to specify the logfile that should be used by wcpercent instead of the default, ~/wcrecord.txt -- this option disables the -l option -g uses the wordcount goal argument provided to calculate percentage of the goal that has been achieved thus far -l takes a path relative to the current working directory, or an absolute path, as the directory in which logfiles are to be generated -n this option turns off logging after all options, you may specify as many directory and filename paths as you wish, separated by spaces -- both directories and specific files may be specified here: supplying such arguments automatically disables the -d option description: wcpercent (aka "word count percent") returns a percentage value indicating how much of a wordcount goal you have reached, based on the contents of a specified directory. The default directory path is ~/nanowrimo, and the default wordcount goal is 50,000 words. If no arguments are supplied when the script is called, these are the values that will be used. The file used as the logfile (by default, ~/wcrecord.txt) must be created before this script is run -- perhaps by use of the touch utility at the shell. credits: Chad L. Perrin (author) contact: http://apotheon.org/contact.php?contact=webmaster license: This software may be distributed under terms of the CCD CopyWrite license. See the webpage at http://ccd.apotheon.org for licensing details. '; our ($opt_d, $opt_f, $opt_g, $opt_l, $opt_n); getopts('d:g:f:l:n'); defaults(); my $total; if ($ARGV[0]) { $total = wc(); } else { # legacy support -- to be removed $total = `wc -w $opt_d |grep total`; $total =~ s/\s//g; $total =~ s/total//; } my $goal = $opt_g / 100; my $percent = $total / $goal; if ($percent <= 100) { print $percent; } else { print 100; } my @log; if (-e $opt_f) { my @logfile; open(LOGFILE, "<$opt_f"); push(@logfile, $_) while ; @log = split(/\s/,$logfile[-1]); } push @log, ''; my $date = localtime; my $newlog = "$total :: $percent :: $date\n"; logging() unless ($log[0] eq $total); ### subroutines ### sub logging { exit if $opt_n; open(WCRECORD, ">>$opt_f") || open(WCRECORD, ">$opt_f"); print WCRECORD $newlog; close(WCRECORD); } sub defaults { my @localtime = localtime; my $odate = ($localtime[5] + 1900) . ($localtime[7] + 1); $opt_d ||= '~/nanowrimo'; $opt_d .= '/*'; $opt_f ||= $opt_l . $odate; $opt_g ||= '50000'; $opt_l ||= '~/wclog'; $opt_l .= '/'; } sub wc { my $words = 0; while (<>) { my @words = split(' ', $_); $words++ foreach @words; } return $words; } sub HELP_MESSAGE { print $help; }