=================================================================== RCS file: /cvs/todotxt/Text-Todo/lib/Text/Todo.pm,v retrieving revision 1.5 retrieving revision 1.8 diff -u -r1.5 -r1.8 --- todotxt/Text-Todo/lib/Text/Todo.pm 2010/01/09 05:00:21 1.5 +++ todotxt/Text-Todo/lib/Text/Todo.pm 2010/01/09 06:26:43 1.8 @@ -1,6 +1,6 @@ package Text::Todo; -# $RedRiver: Todo.pm,v 1.4 2010/01/06 20:07:16 andrew Exp $ +# $RedRiver: Todo.pm,v 1.6 2010/01/09 05:15:20 andrew Exp $ use warnings; use strict; @@ -18,6 +18,7 @@ my %path_of; my %list_of; + my %loaded_of; sub new { my ( $class, $options ) = @_; @@ -130,28 +131,41 @@ my ( $self, $file ) = @_; my $ident = ident($self); + $loaded_of{$ident} = undef; + + if ( $list_of{$ident} = $self->listfile($file) ) { + $loaded_of{$ident} = $file; + return 1; + } + + return; + } + + sub listfile { + my ( $self, $file ) = @_; + $file = $self->file($file); if ( !defined $file ) { - croak "todo file can't be found"; + carp q{file can't be found}; + return; } if ( !-e $file ) { - carp "todo file [$file] does not exist"; + carp "file [$file] does not exist"; return; } my @list; - my $line = 1; open my $fh, '<', $file or croak "Couldn't open [$file]: $!"; while (<$fh>) { s/\r?\n$//xms; + next if !length $_; push @list, Text::Todo::Entry->new($_); } close $fh or croak "Couldn't close [$file]: $!"; - $list_of{$ident} = \@list; - return 1; + return wantarray ? @list : \@list; } sub save { @@ -160,7 +174,7 @@ $file = $self->file($file); if ( !defined $file ) { - croak "todo file can't be found"; + croak q{todo file can't be found}; } open my $fh, '>', $file or croak "Couldn't open [$file]: $!"; @@ -176,11 +190,9 @@ sub list { my ($self) = @_; my $ident = ident($self); - return if !$list_of{$ident}; - my @list = @{ $list_of{$ident} }; - - return wantarray ? @list : \@list; + return if !$list_of{$ident}; + return wantarray ? @{ $list_of{$ident} } : $list_of{$ident}; } sub listpri { @@ -208,14 +220,14 @@ return $entry; } - sub del { + sub del { my ( $self, $src ) = @_; my $ident = ident($self); - my $id = $self->_find_entry_id($src); + my $id = $self->_find_entry_id($src); my @list = $self->list; - my $entry = splice( @list, $id, 1 ); + my $entry = splice @list, $id, 1; $list_of{$ident} = \@list; return $entry; @@ -228,19 +240,19 @@ my $src = $self->_find_entry_id($entry); my @list = $self->list; - splice( @list, $dst, 0, splice( @list, $src, 1 ) ); + splice @list, $dst, 0, splice @list, $src, 1; $list_of{$ident} = \@list; return 1; } - sub listproj { + sub listproj { my ( $self, $entry, $dst ) = @_; my $ident = ident($self); my %available_projects; - foreach my $e ($self->list) { + foreach my $e ( $self->list ) { foreach my $p ( $e->projects ) { $available_projects{$p} = 1; } @@ -251,11 +263,29 @@ return wantarray ? @projects : \@projects; } - sub archive { carp "unsupported\n", return } + sub archive { carp 'unsupported'; return } - sub addto { carp "unsupported\n", return } - sub listfile { carp "unsupported\n", return } + sub addto { + my ( $self, $file, $entry ) = @_; + my $ident = ident($self); + $file = $self->file($file); + if ( !defined $file ) { + croak q{file can't be found}; + } + + open my $fh, '>>', $file or croak "Couldn't open [$file]: $!"; + print {$fh} $entry, "\n" + or croak "Couldn't print to [$file]: $!"; + close $fh or croak "Couldn't close [$file]: $!"; + + if ( defined $loaded_of{$ident} && $file eq $loaded_of{$ident} ) { + return $self->load($file); + } + + return 1; + } + sub _find_entry_id { my ( $self, $entry ) = @_; my $ident = ident($self); @@ -289,6 +319,13 @@ Text::Todo - Perl interface to todo_txt files +=head1 VERSION + +I will have to figure out how to include $VERSION in this somehow. + +Perhaps RCS Id is good enough? + + $Id: Todo.pm,v 1.8 2010/01/09 06:26:43 andrew Exp $ =head1 SYNOPSIS