App-VOJournal
view release on metacpan or search on metacpan
lib/App/VOJournal.pm view on Meta::CPAN
# file.
#
# $basedir - the start directory
# $next_file - the name of the file we want to use next
# $f - optional, a hash with function references to aid testing/debugging
#
sub _find_last_file {
my ($basedir,$next_file,$f) = @_;
my $last_file = '';
my $got_it = 0;
my $wanted = sub {
my $this_file = $File::Find::name;
return if ($got_it);
#
# We get the files in reverse order, therefore the first matching
# file is already the one we are looking for.
#
# If we got the file we signal this with $got_it.
#
if ($this_file =~ qr|^$basedir/\d{4}/\d{2}/\d{8}[.]otl$|
&& 0 < ($this_file cmp $last_file)
&& 0 >= ($this_file cmp $next_file)) {
$last_file = $this_file;
$got_it = 1;
}
#
# The following is only to aid in testing or debugging.
#
if ($f->{wanted}) {
$f->{wanted}->($this_file,$last_file,$next_file,$got_it);
}
};
#
# Concentrate on the files whose path matches the pattern of journal
# files and ignore the rest with this preprocess function for
# File::Find::find().
#
# Sort the filenames reverse and ignore all following directory listings
# if we have already found the last file;
#
my $preprocess = sub {
my @files = ();
if ($got_it) {
# leave it empty
}
elsif ($File::Find::dir =~ /^$basedir$/) {
@files = grep { /^\d{4}$/ } @_;
}
elsif ($File::Find::dir =~ m|^$basedir/\d{4}$|) {
@files = grep { /^\d{2}$/ } @_;
}
elsif ($File::Find::dir =~ m|^$basedir/\d{4}/\d{2}$|) {
@files = grep { /^\d{8}\.otl$/ } @_;
}
return sort {$b cmp $a} @files;
};
find({wanted => $wanted,
preprocess => $preprocess,
untaint => 1, # needed when running in taint mode
no_chdir => 1, # we don't need to chdir
},$basedir);
return $last_file;
} # _find_last_file()
sub _use_last_file {
my ($path,$last_file,$opt,$header) = @_;
my $votl = App::VOJournal::VOTL->new();
$votl->read_file_unchecked_boxes($last_file);
if ($opt->{header}) {
$votl->insert_line(0,$header);
}
$votl->write_file($path);
} # _use_old_file
=head1 COMMANDLINE OPTIONS
=head2 --basedir $dir
Use C<< $dir >> instead of C<< $ENV{HOME}/journal >> as base directory for
the journal files.
=head2 --date [YYYY[MM]]DD
Use a different date than today.
One or two digits change the day in the current month.
Three or four digits change the day and month in the current year.
Eight digits change day, month and year.
The program will not test for a valid date. That means, if you specify
'--date 0230' on the command line, the file for February 30th this year
would be opened.
=head2 --editor $path_to_editor
Use this option to specify an editor other than C<vim> to edit
the journal file.
=head2 --[no]header
Normally every journal file starts with some header lines, indicating the
day the journal is written.
If the option C<--noheader> is given on the command line, this header lines
will be omitted.
=head2 --[no]resume
Look for open checkboxes in the last journal file and carry them forward
to this days journal file before opening it.
This only works if there is no journal file for this day.
The default is C<< --resume >>
=head2 --version
( run in 1.917 second using v1.01-cache-2.11-cpan-7fcb06a456a )