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

Annotation of todotxt/Text-Todo/lib/Text/Todo.pm, Revision 1.12

1.1       andrew      1: package Text::Todo;
                      2:
1.12    ! andrew      3: # $RedRiver: Todo.pm,v 1.11 2010/01/10 01:01:45 andrew Exp $
1.1       andrew      4:
                      5: use warnings;
                      6: use strict;
                      7: use Carp;
                      8:
1.2       andrew      9: use Class::Std::Utils;
                     10: use Text::Todo::Entry;
1.5       andrew     11: use File::Spec;
                     12:
1.1       andrew     13: use version; our $VERSION = qv('0.0.1');
                     14:
1.2       andrew     15: {
                     16:
1.5       andrew     17:     my %path_of;
1.2       andrew     18:     my %list_of;
1.8       andrew     19:     my %loaded_of;
1.1       andrew     20:
1.2       andrew     21:     sub new {
1.5       andrew     22:         my ( $class, $options ) = @_;
1.1       andrew     23:
1.2       andrew     24:         my $self = bless anon_scalar(), $class;
                     25:         my $ident = ident($self);
                     26:
1.5       andrew     27:         $path_of{$ident} = {
                     28:             todo_dir    => undef,
                     29:             todo_file   => 'todo.txt',
                     30:             done_file   => undef,
                     31:             report_file => undef,
                     32:         };
                     33:
                     34:         if ($options) {
                     35:             if ( ref $options eq 'HASH' ) {
                     36:                 foreach my $opt ( keys %{$options} ) {
                     37:                     if ( exists $path_of{$ident}{$opt} ) {
                     38:                         $self->_path_to( $opt, $options->{$opt} );
                     39:                     }
                     40:                     else {
                     41:                         carp "Invalid option [$opt]";
                     42:                     }
                     43:                 }
                     44:             }
                     45:             else {
                     46:                 if ( -d $options ) {
                     47:                     $self->_path_to( 'todo_dir', $options );
                     48:                 }
                     49:                 elsif ( $options =~ /\.txt$/ixms ) {
                     50:                     $self->_path_to( 'todo_file', $options );
                     51:                 }
                     52:                 else {
                     53:                     carp "Unknown options [$options]";
                     54:                 }
                     55:             }
                     56:         }
                     57:
                     58:         my $file = $self->_path_to('todo_file');
                     59:         if ( defined $file && -e $file ) {
                     60:             $self->load();
                     61:         }
1.2       andrew     62:
                     63:         return $self;
                     64:     }
                     65:
1.5       andrew     66:     sub _path_to {
                     67:         my ( $self, $type, $path ) = @_;
                     68:         my $ident = ident($self);
                     69:
                     70:         if ( $type eq 'todo_dir' ) {
                     71:             if ($path) {
                     72:                 $path_of{$ident}{$type} = $path;
                     73:             }
                     74:             return $path_of{$ident}{$type};
                     75:         }
                     76:
                     77:         if ($path) {
                     78:             my ( $volume, $directories, $file )
                     79:                 = File::Spec->splitpath($path);
                     80:             $path_of{$ident}{$type} = $file;
                     81:
                     82:             if ($volume) {
                     83:                 $directories = File::Spec->catdir( $volume, $directories );
                     84:             }
                     85:
                     86:             # XXX Should we save complete paths to each file, mebbe only if
                     87:             # the dirs are different?
                     88:             if ($directories) {
                     89:                 $path_of{$ident}{todo_dir} = $directories;
                     90:             }
                     91:         }
                     92:
                     93:         if ( $type =~ /(todo|done|report)_file/xms ) {
                     94:             if ( my ( $pre, $post )
                     95:                 = $path_of{$ident}{$type} =~ /^(.*)$1(.*)\.txt$/ixms )
                     96:             {
                     97:                 foreach my $f qw( todo done report ) {
                     98:                     if ( !defined $path_of{$ident}{ $f . '_file' } ) {
                     99:                         $path_of{$ident}{ $f . '_file' }
                    100:                             = $pre . $f . $post . '.txt';
                    101:                     }
                    102:                 }
                    103:             }
                    104:         }
                    105:
                    106:         if ( defined $path_of{$ident}{todo_dir} ) {
                    107:             return File::Spec->catfile( $path_of{$ident}{todo_dir},
                    108:                 $path_of{$ident}{$type} );
                    109:         }
                    110:
                    111:         return;
                    112:     }
                    113:
1.3       andrew    114:     sub file {
1.2       andrew    115:         my ( $self, $file ) = @_;
                    116:         my $ident = ident($self);
                    117:
1.5       andrew    118:         if ( defined $file && exists $path_of{$ident}{$file} ) {
                    119:             $file = $self->_path_to($file);
                    120:         }
                    121:         else {
                    122:             $file = $self->_path_to( 'todo_file', $file );
1.2       andrew    123:         }
                    124:
1.5       andrew    125:         return $file;
1.3       andrew    126:     }
                    127:
                    128:     sub load {
                    129:         my ( $self, $file ) = @_;
                    130:         my $ident = ident($self);
                    131:
1.8       andrew    132:         $loaded_of{$ident} = undef;
                    133:
1.9       andrew    134:         $file = $self->file($file);
                    135:
1.8       andrew    136:         if ( $list_of{$ident} = $self->listfile($file) ) {
                    137:             $loaded_of{$ident} = $file;
                    138:             return 1;
                    139:         }
                    140:
                    141:         return;
                    142:     }
                    143:
                    144:     sub listfile {
                    145:         my ( $self, $file ) = @_;
                    146:
1.5       andrew    147:         $file = $self->file($file);
                    148:
                    149:         if ( !defined $file ) {
1.8       andrew    150:             carp q{file can't be found};
                    151:             return;
1.5       andrew    152:         }
                    153:
                    154:         if ( !-e $file ) {
1.8       andrew    155:             carp "file [$file] does not exist";
1.5       andrew    156:             return;
                    157:         }
1.2       andrew    158:
                    159:         my @list;
                    160:         open my $fh, '<', $file or croak "Couldn't open [$file]: $!";
                    161:         while (<$fh>) {
                    162:             s/\r?\n$//xms;
                    163:             push @list, Text::Todo::Entry->new($_);
                    164:         }
                    165:         close $fh or croak "Couldn't close [$file]: $!";
                    166:
1.8       andrew    167:         return wantarray ? @list : \@list;
1.2       andrew    168:     }
                    169:
                    170:     sub save {
                    171:         my ( $self, $file ) = @_;
                    172:         my $ident = ident($self);
                    173:
1.5       andrew    174:         $file = $self->file($file);
                    175:         if ( !defined $file ) {
1.6       andrew    176:             croak q{todo file can't be found};
1.5       andrew    177:         }
1.2       andrew    178:
                    179:         open my $fh, '>', $file or croak "Couldn't open [$file]: $!";
                    180:         foreach my $e ( @{ $list_of{$ident} } ) {
1.3       andrew    181:             print {$fh} $e->text . "\n"
                    182:                 or croak "Couldn't print to [$file]: $!";
1.2       andrew    183:         }
                    184:         close $fh or croak "Couldn't close [$file]: $!";
                    185:
1.9       andrew    186:         $loaded_of{$ident} = $file;
                    187:
1.2       andrew    188:         return 1;
                    189:     }
                    190:
                    191:     sub list {
1.3       andrew    192:         my ($self) = @_;
1.2       andrew    193:         my $ident = ident($self);
1.6       andrew    194:
1.2       andrew    195:         return if !$list_of{$ident};
1.6       andrew    196:         return wantarray ? @{ $list_of{$ident} } : $list_of{$ident};
1.5       andrew    197:     }
                    198:
                    199:     sub listpri {
                    200:         my ($self) = @_;
                    201:
                    202:         my @list = grep { $_->priority } $self->list;
                    203:
                    204:         return wantarray ? @list : \@list;
1.2       andrew    205:     }
1.1       andrew    206:
1.3       andrew    207:     sub add {
                    208:         my ( $self, $entry ) = @_;
                    209:         my $ident = ident($self);
                    210:
1.5       andrew    211:         if ( !ref $entry ) {
                    212:             $entry = Text::Todo::Entry->new($entry);
                    213:         }
                    214:         elsif ( ref $entry ne 'Text::Todo::Entry' ) {
                    215:             croak(
                    216:                 'entry is a ' . ref($entry) . ' not a Text::Todo::Entry!' );
                    217:         }
                    218:
                    219:         push @{ $list_of{$ident} }, $entry;
                    220:
                    221:         return $entry;
                    222:     }
                    223:
1.6       andrew    224:     sub del {
1.5       andrew    225:         my ( $self, $src ) = @_;
                    226:         my $ident = ident($self);
                    227:
1.6       andrew    228:         my $id = $self->_find_entry_id($src);
1.5       andrew    229:
                    230:         my @list = $self->list;
1.6       andrew    231:         my $entry = splice @list, $id, 1;
1.5       andrew    232:         $list_of{$ident} = \@list;
                    233:
                    234:         return $entry;
                    235:     }
                    236:
                    237:     sub move {
                    238:         my ( $self, $entry, $dst ) = @_;
                    239:         my $ident = ident($self);
                    240:
                    241:         my $src  = $self->_find_entry_id($entry);
                    242:         my @list = $self->list;
                    243:
1.6       andrew    244:         splice @list, $dst, 0, splice @list, $src, 1;
1.5       andrew    245:
                    246:         $list_of{$ident} = \@list;
                    247:
                    248:         return 1;
                    249:     }
                    250:
1.6       andrew    251:     sub listproj {
1.5       andrew    252:         my ( $self, $entry, $dst ) = @_;
                    253:         my $ident = ident($self);
                    254:
                    255:         my %available_projects;
1.6       andrew    256:         foreach my $e ( $self->list ) {
1.5       andrew    257:             foreach my $p ( $e->projects ) {
                    258:                 $available_projects{$p} = 1;
                    259:             }
                    260:         }
                    261:
                    262:         my @projects = sort keys %available_projects;
                    263:
                    264:         return wantarray ? @projects : \@projects;
                    265:     }
                    266:
1.9       andrew    267:     sub archive {
                    268:         my ($self) = @_;
                    269:         my $ident = ident($self);
                    270:
                    271:         if ( !defined $loaded_of{$ident}
                    272:             || $loaded_of{$ident} ne $self->file('todo_file') )
                    273:         {
                    274:             carp 'todo_file not loaded';
                    275:             return;
                    276:         }
                    277:
1.12    ! andrew    278:         my $changed = 0;
1.9       andrew    279:     ENTRY: foreach my $e ( $self->list ) {
                    280:             if ( $e->done ) {
                    281:                 if ( $self->addto( 'done_file', $e ) && $self->del($e) ) {
1.12    ! andrew    282:                     $changed++;
1.9       andrew    283:                 }
                    284:                 else {
                    285:                     carp q{Couldn't archive entry [} . $e->text . ']';
                    286:                     last ENTRY;
                    287:                 }
                    288:             }
1.12    ! andrew    289:             elsif ($e->text eq q{}) {
        !           290:                 if ($self->del($e)) {
        !           291:                     $changed++;
        !           292:                 }
        !           293:                 else {
        !           294:                     carp q{Couldn't delete blank entry};
        !           295:                     last ENTRY;
        !           296:                 }
        !           297:             }
1.9       andrew    298:         }
                    299:
1.12    ! andrew    300:         if ($changed) {
1.9       andrew    301:             $self->save;
                    302:         }
                    303:
1.12    ! andrew    304:         return $changed;
1.9       andrew    305:     }
1.8       andrew    306:
                    307:     sub addto {
                    308:         my ( $self, $file, $entry ) = @_;
                    309:         my $ident = ident($self);
                    310:
                    311:         $file = $self->file($file);
                    312:         if ( !defined $file ) {
                    313:             croak q{file can't be found};
                    314:         }
                    315:
1.9       andrew    316:         if ( ref $entry ) {
                    317:             if ( ref $entry eq 'Text::Todo::Entry' ) {
                    318:                 $entry = $entry->text;
                    319:             }
                    320:             else {
                    321:                 carp 'Unknown ref [' . ref($entry) . ']';
                    322:                 return;
                    323:             }
                    324:         }
                    325:
1.8       andrew    326:         open my $fh, '>>', $file or croak "Couldn't open [$file]: $!";
                    327:         print {$fh} $entry, "\n"
                    328:             or croak "Couldn't print to [$file]: $!";
                    329:         close $fh or croak "Couldn't close [$file]: $!";
                    330:
                    331:         if ( defined $loaded_of{$ident} && $file eq $loaded_of{$ident} ) {
                    332:             return $self->load($file);
                    333:         }
                    334:
                    335:         return 1;
                    336:     }
1.5       andrew    337:
                    338:     sub _find_entry_id {
                    339:         my ( $self, $entry ) = @_;
                    340:         my $ident = ident($self);
                    341:
1.3       andrew    342:         if ( ref $entry ) {
                    343:             if ( ref $entry ne 'Text::Todo::Entry' ) {
                    344:                 croak(    'entry is a '
                    345:                         . ref($entry)
                    346:                         . ' not a Text::Todo::Entry!' );
                    347:             }
1.5       andrew    348:
                    349:             my @list = $self->list;
                    350:             foreach my $id ( 0 .. $#list ) {
                    351:                 if ( $list[$id] eq $entry ) {
                    352:                     return $id;
                    353:                 }
                    354:             }
1.3       andrew    355:         }
1.5       andrew    356:         elsif ( $entry =~ /^\d+$/xms ) {
                    357:             return $entry;
1.3       andrew    358:         }
                    359:
1.5       andrew    360:         croak "Invalid entry [$entry]!";
1.3       andrew    361:     }
1.2       andrew    362: }
1.1       andrew    363:
1.2       andrew    364: 1;    # Magic true value required at end of module
1.1       andrew    365: __END__
                    366:
                    367: =head1 NAME
                    368:
1.4       andrew    369: Text::Todo - Perl interface to todo_txt files
1.1       andrew    370:
1.10      andrew    371:
1.6       andrew    372: =head1 VERSION
                    373:
1.10      andrew    374: Since the $VERSION can't be automatically included,
                    375: here is the RCS Id instead, you'll have to look up $VERSION.
1.6       andrew    376:
1.12    ! andrew    377:     $Id: Todo.pm,v 1.11 2010/01/10 01:01:45 andrew Exp $
1.1       andrew    378:
                    379: =head1 SYNOPSIS
                    380:
                    381:     use Text::Todo;
1.10      andrew    382:
                    383:     my $todo = Text::Todo->new('todo/todo.txt');
                    384:
                    385:     foreach my $e (sort { lc($_->text) cmp lc($e->text)} $todo->list) {
                    386:         print $e->text, "\n";
                    387:     }
                    388:
1.1       andrew    389:
                    390: =head1 DESCRIPTION
                    391:
1.10      andrew    392: This module is a basic interface to the todo.txt files as described by
                    393: Lifehacker and extended by members of their community.
                    394:
1.4       andrew    395: For more information see L<http://todotxt.com>
1.1       andrew    396:
1.10      andrew    397: This module supports the 3 axes of an effective todo list.
                    398: Priority, Project and Context.
                    399:
                    400: It does not support other notations or many of the more advanced features of
                    401: the todo.sh like plugins.
                    402:
                    403: It should be extensible, but and hopefully will be before a 1.0 release.
                    404:
                    405:
1.1       andrew    406: =head1 INTERFACE
                    407:
1.2       andrew    408: =head2 new
                    409:
1.10      andrew    410:     new({
                    411:         [ todo_dir    => 'directory', ]
                    412:         [ todo_file   => 'filename in todo_dir', ]
                    413:         [ done_file   => 'filename in todo_dir', ]
                    414:         [ report_file => 'filename in todo_dir', ]
                    415:         });
                    416:
                    417: Allows you to set each item individually.  todo_file defaults to todo.txt.
                    418:
                    419:     new('path/to/todo.txt');
                    420:
                    421: Automatically sets todo_dir to 'path/to', todo_file to 'todo.txt'
                    422:
                    423:     new('path/to')
                    424:
                    425: If you pass an existing directory to new, it will set todo_dir.
                    426:
                    427:
                    428: If you what you set matches (.*)todo(.*).txt it will automatically set
                    429: done_file to $1done$2.txt
                    430: and
                    431: report_file to $1report$2.txt.
                    432:
                    433: For example, new('todo/todo.shopping.txt') will set
                    434: todo_dir to 'todo',
                    435: todo_file to 'todo.shopping.txt',
                    436: done_file to 'done.shopping.txt',
                    437: and
                    438: report_file to 'report.shopping.txt'.
                    439:
1.9       andrew    440: =head2 file
                    441:
1.10      andrew    442: Allows you to read the paths to the files in use.
                    443: If as in the SYNOPSIS above you used $todo = new('todo/todo.txt').
                    444:
                    445:     $todo_file = $todo->file('todo_file');
                    446:
                    447: then, $todo_file eq 'todo/todo.txt'
                    448:
1.2       andrew    449: =head2 load
                    450:
1.10      andrew    451: Allows you to load a different file into the object.
                    452:
                    453:     $todo->load('done_file');
                    454:
                    455: This effects the other functions that act on the list.
                    456:
1.2       andrew    457: =head2 save
                    458:
1.10      andrew    459:     $todo->save(['new/path/to/todo']);
                    460:
                    461: Writes the list to the file. Either the current working file or something
                    462: that can be recognized by file().
                    463:
                    464: If you specify a filename it will save to that file and update the paths.
                    465: Additional changes to the object work on that file.
                    466:
1.9       andrew    467: =head2 list
                    468:
1.10      andrew    469:     my @todo_list = $todo->list;
                    470:
1.9       andrew    471: =head2 listpri
1.3       andrew    472:
1.10      andrew    473: Like list, but only returns entries that have priority set.
                    474:
                    475:     my @priority_list = $todo->listpri;
                    476:
1.9       andrew    477: =head2 listproj
1.3       andrew    478:
1.10      andrew    479: Returns projects in the list sorted by name.
                    480: If there were projects +GarageSale and +Shopping
                    481:
                    482:     my @projects = $todo->listproj;
                    483:
                    484: is the same as
                    485:
                    486:     @projects = ( 'GarageSale', 'Shopping' );
                    487:
1.3       andrew    488: =head2 add
1.9       andrew    489:
1.10      andrew    490: Adds a new entry to the list.
                    491: Can either be a Text::Todo::Entry object or plain text.
                    492:
                    493:     $todo->add('new todo entry');
                    494:
                    495: It then becomes $todo->list->[-1];
                    496:
1.9       andrew    497: =head2 del
                    498:
1.10      andrew    499: Remove an entry from the list, either the reference or by number.
                    500:
                    501:     $removed_entry = $todo->del($entry);
                    502:
                    503: $entry can either be an Text::Todo::Entry in the list or the index of the
                    504: entry to delete.
                    505:
                    506: Note that entries are 0 indexed (as expected in perl) not starting at line 1.
                    507:
1.9       andrew    508: =head2 move
                    509:
1.10      andrew    510:     $todo->move($entry, $new_pos);
                    511:
                    512: $entry can either be the number of the entry or the actual entry.
                    513: $new_pos is the new position to put it.
                    514:
                    515: Note that entries are 0 indexed (as expected in perl) not starting at line 1.
                    516:
1.9       andrew    517: =head2 archive
                    518:
1.10      andrew    519:     $todo->archive
                    520:
                    521: Iterates over the list and for each done entry,
                    522: addto('done_file')
                    523: and
                    524: del($entry).
                    525: If any were archived it will then
                    526: save()
                    527: and
                    528: load().
                    529:
1.9       andrew    530: =head2 addto
                    531:
1.10      andrew    532:     $todo->addto($file, $entry);
1.1       andrew    533:
1.10      andrew    534: Appends text to the file.
                    535: $file can be anyting recognized by file().
                    536: $entry can either be a Text::Todo::Entry or plain text.
1.1       andrew    537:
1.10      andrew    538: =head2 listfile
1.1       andrew    539:
1.10      andrew    540:     @list = $todo->listfile($file);
1.1       andrew    541:
1.10      andrew    542: Read a file and returns a list like $todo->list but does not update the
                    543: internal list that is being worked with.
                    544: $file can be anyting recognized by file().
1.1       andrew    545:
                    546:
1.10      andrew    547: =head1 DIAGNOSTICS
1.1       andrew    548:
1.10      andrew    549: Most methods return undef on failure.
1.1       andrew    550:
1.10      andrew    551: Some more important methods are fatal.
1.1       andrew    552:
                    553:
                    554: =head1 CONFIGURATION AND ENVIRONMENT
                    555:
                    556: Text::Todo requires no configuration files or environment variables.
                    557:
1.10      andrew    558: Someday it should be able to read and use the todo.sh config file.  This may
                    559: possibly be better done in a client that would use this module.
1.4       andrew    560:
1.1       andrew    561:
                    562: =head1 DEPENDENCIES
                    563:
1.10      andrew    564: Class::Std::Utils
                    565: File::Spec
                    566: version
1.1       andrew    567:
                    568:
                    569: =head1 INCOMPATIBILITIES
                    570:
                    571: None reported.
                    572:
                    573:
                    574: =head1 BUGS AND LIMITATIONS
                    575:
                    576: No bugs have been reported.
1.11      andrew    577:
                    578: Limitations:
                    579:
                    580: Currently there isn't an easy way to print out line numbers with the entry.
1.1       andrew    581:
                    582: Please report any bugs or feature requests to
                    583: C<bug-text-todo@rt.cpan.org>, or through the web interface at
                    584: L<http://rt.cpan.org>.
                    585:
                    586:
                    587: =head1 AUTHOR
                    588:
                    589: Andrew Fresh  C<< <andrew@cpan.org> >>
                    590:
                    591:
                    592: =head1 LICENSE AND COPYRIGHT
                    593:
                    594: Copyright (c) 2009, Andrew Fresh C<< <andrew@cpan.org> >>. All rights reserved.
                    595:
                    596: This module is free software; you can redistribute it and/or
                    597: modify it under the same terms as Perl itself. See L<perlartistic>.
                    598:
                    599:
                    600: =head1 DISCLAIMER OF WARRANTY
                    601:
                    602: BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
                    603: FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
                    604: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
                    605: PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
                    606: EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                    607: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
                    608: ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
                    609: YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
                    610: NECESSARY SERVICING, REPAIR, OR CORRECTION.
                    611:
                    612: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
                    613: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
                    614: REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
                    615: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
                    616: OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
                    617: THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
                    618: RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
                    619: FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
                    620: SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
                    621: SUCH DAMAGES.

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