[BACK]Return to Todo.pm CVS log [TXT][DIR] Up to [local] / todotxt / Text-Todo / lib / Text

Diff for /todotxt/Text-Todo/lib/Text/Todo.pm between version 1.5 and 1.9

version 1.5, 2010/01/09 05:00:21 version 1.9, 2010/01/09 06:54:15
Line 1 
Line 1 
 package Text::Todo;  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 warnings;
 use strict;  use strict;
Line 18 
Line 18 
   
     my %path_of;      my %path_of;
     my %list_of;      my %list_of;
       my %loaded_of;
   
     sub new {      sub new {
         my ( $class, $options ) = @_;          my ( $class, $options ) = @_;
Line 130 
Line 131 
         my ( $self, $file ) = @_;          my ( $self, $file ) = @_;
         my $ident = ident($self);          my $ident = ident($self);
   
           $loaded_of{$ident} = undef;
   
         $file = $self->file($file);          $file = $self->file($file);
   
           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 ) {          if ( !defined $file ) {
             croak "todo file can't be found";              carp q{file can't be found};
               return;
         }          }
   
         if ( !-e $file ) {          if ( !-e $file ) {
             carp "todo file [$file] does not exist";              carp "file [$file] does not exist";
             return;              return;
         }          }
   
         my @list;          my @list;
         my $line = 1;  
         open my $fh, '<', $file or croak "Couldn't open [$file]: $!";          open my $fh, '<', $file or croak "Couldn't open [$file]: $!";
         while (<$fh>) {          while (<$fh>) {
             s/\r?\n$//xms;              s/\r?\n$//xms;
               next if !length $_;
             push @list, Text::Todo::Entry->new($_);              push @list, Text::Todo::Entry->new($_);
         }          }
         close $fh or croak "Couldn't close [$file]: $!";          close $fh or croak "Couldn't close [$file]: $!";
         $list_of{$ident} = \@list;  
   
         return 1;          return wantarray ? @list : \@list;
     }      }
   
     sub save {      sub save {
Line 160 
Line 176 
   
         $file = $self->file($file);          $file = $self->file($file);
         if ( !defined $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]: $!";          open my $fh, '>', $file or croak "Couldn't open [$file]: $!";
Line 170 
Line 186 
         }          }
         close $fh or croak "Couldn't close [$file]: $!";          close $fh or croak "Couldn't close [$file]: $!";
   
           $loaded_of{$ident} = $file;
   
         return 1;          return 1;
     }      }
   
     sub list {      sub list {
         my ($self) = @_;          my ($self) = @_;
         my $ident = ident($self);          my $ident = ident($self);
         return if !$list_of{$ident};  
   
         my @list = @{ $list_of{$ident} };          return if !$list_of{$ident};
           return wantarray ? @{ $list_of{$ident} } : $list_of{$ident};
         return wantarray ? @list : \@list;  
     }      }
   
     sub listpri {      sub listpri {
Line 208 
Line 224 
         return $entry;          return $entry;
     }      }
   
     sub del {      sub del {
         my ( $self, $src ) = @_;          my ( $self, $src ) = @_;
         my $ident = ident($self);          my $ident = ident($self);
   
         my $id  = $self->_find_entry_id($src);          my $id = $self->_find_entry_id($src);
   
         my @list = $self->list;          my @list = $self->list;
         my $entry = splice( @list, $id, 1 );          my $entry = splice @list, $id, 1;
         $list_of{$ident} = \@list;          $list_of{$ident} = \@list;
   
         return $entry;          return $entry;
Line 228 
Line 244 
         my $src  = $self->_find_entry_id($entry);          my $src  = $self->_find_entry_id($entry);
         my @list = $self->list;          my @list = $self->list;
   
         splice( @list, $dst, 0, splice( @list, $src, 1 ) );          splice @list, $dst, 0, splice @list, $src, 1;
   
         $list_of{$ident} = \@list;          $list_of{$ident} = \@list;
   
         return 1;          return 1;
     }      }
   
     sub listproj {      sub listproj {
         my ( $self, $entry, $dst ) = @_;          my ( $self, $entry, $dst ) = @_;
         my $ident = ident($self);          my $ident = ident($self);
   
         my %available_projects;          my %available_projects;
         foreach my $e ($self->list) {          foreach my $e ( $self->list ) {
             foreach my $p ( $e->projects ) {              foreach my $p ( $e->projects ) {
                 $available_projects{$p} = 1;                  $available_projects{$p} = 1;
             }              }
Line 251 
Line 267 
         return wantarray ? @projects : \@projects;          return wantarray ? @projects : \@projects;
     }      }
   
     sub archive  { carp "unsupported\n", return }      sub archive {
           my ($self) = @_;
           my $ident = ident($self);
   
     sub addto    { carp "unsupported\n", return }          if ( !defined $loaded_of{$ident}
     sub listfile { carp "unsupported\n", return }              || $loaded_of{$ident} ne $self->file('todo_file') )
           {
               carp 'todo_file not loaded';
               return;
           }
   
           my $archived = 0;
       ENTRY: foreach my $e ( $self->list ) {
               if ( $e->done ) {
                   if ( $self->addto( 'done_file', $e ) && $self->del($e) ) {
                       $archived++;
                   }
                   else {
                       carp q{Couldn't archive entry [} . $e->text . ']';
                       last ENTRY;
                   }
               }
           }
   
           if ($archived) {
               $self->save;
           }
   
           return $archived;
       }
   
       sub addto {
           my ( $self, $file, $entry ) = @_;
           my $ident = ident($self);
   
           $file = $self->file($file);
           if ( !defined $file ) {
               croak q{file can't be found};
           }
   
           if ( ref $entry ) {
               if ( ref $entry eq 'Text::Todo::Entry' ) {
                   $entry = $entry->text;
               }
               else {
                   carp 'Unknown ref [' . ref($entry) . ']';
                   return;
               }
           }
   
           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 {      sub _find_entry_id {
         my ( $self, $entry ) = @_;          my ( $self, $entry ) = @_;
         my $ident = ident($self);          my $ident = ident($self);
Line 289 
Line 362 
   
 Text::Todo - Perl interface to todo_txt files  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$
   
 =head1 SYNOPSIS  =head1 SYNOPSIS
   
     use Text::Todo;      use Text::Todo;
Line 302 
Line 382 
   
 =head2 new  =head2 new
   
   =head2 file
   
 =head2 load  =head2 load
   
 =head2 save  =head2 save
   
 =head2 file  
   
 =head2 list  =head2 list
   
   =head2 listpri
   
   =head2 listproj
   
 =head2 add  =head2 add
   
   =head2 del
   
   =head2 move
   
   =head2 archive
   
   =head2 addto
   
   =head2 listfile
   
 =head1 DIAGNOSTICS  =head1 DIAGNOSTICS
   

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.9

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>