[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.8

1.1       andrew      1: package Text::Todo;
                      2:
1.7       andrew      3: # $RedRiver: Todo.pm,v 1.6 2010/01/09 05:15:20 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:
                     13: use Data::Dumper;
1.2       andrew     14:
1.1       andrew     15: use version; our $VERSION = qv('0.0.1');
                     16:
1.2       andrew     17: {
                     18:
1.5       andrew     19:     my %path_of;
1.2       andrew     20:     my %list_of;
1.8     ! andrew     21:     my %loaded_of;
1.1       andrew     22:
1.2       andrew     23:     sub new {
1.5       andrew     24:         my ( $class, $options ) = @_;
1.1       andrew     25:
1.2       andrew     26:         my $self = bless anon_scalar(), $class;
                     27:         my $ident = ident($self);
                     28:
1.5       andrew     29:         $path_of{$ident} = {
                     30:             todo_dir    => undef,
                     31:             todo_file   => 'todo.txt',
                     32:             done_file   => undef,
                     33:             report_file => undef,
                     34:         };
                     35:
                     36:         if ($options) {
                     37:             if ( ref $options eq 'HASH' ) {
                     38:                 foreach my $opt ( keys %{$options} ) {
                     39:                     if ( exists $path_of{$ident}{$opt} ) {
                     40:                         $self->_path_to( $opt, $options->{$opt} );
                     41:                     }
                     42:                     else {
                     43:                         carp "Invalid option [$opt]";
                     44:                     }
                     45:                 }
                     46:             }
                     47:             else {
                     48:                 if ( -d $options ) {
                     49:                     $self->_path_to( 'todo_dir', $options );
                     50:                 }
                     51:                 elsif ( $options =~ /\.txt$/ixms ) {
                     52:                     $self->_path_to( 'todo_file', $options );
                     53:                 }
                     54:                 else {
                     55:                     carp "Unknown options [$options]";
                     56:                 }
                     57:             }
                     58:         }
                     59:
                     60:         my $file = $self->_path_to('todo_file');
                     61:         if ( defined $file && -e $file ) {
                     62:             $self->load();
                     63:         }
1.2       andrew     64:
                     65:         return $self;
                     66:     }
                     67:
1.5       andrew     68:     sub _path_to {
                     69:         my ( $self, $type, $path ) = @_;
                     70:         my $ident = ident($self);
                     71:
                     72:         if ( $type eq 'todo_dir' ) {
                     73:             if ($path) {
                     74:                 $path_of{$ident}{$type} = $path;
                     75:             }
                     76:             return $path_of{$ident}{$type};
                     77:         }
                     78:
                     79:         if ($path) {
                     80:             my ( $volume, $directories, $file )
                     81:                 = File::Spec->splitpath($path);
                     82:             $path_of{$ident}{$type} = $file;
                     83:
                     84:             if ($volume) {
                     85:                 $directories = File::Spec->catdir( $volume, $directories );
                     86:             }
                     87:
                     88:             # XXX Should we save complete paths to each file, mebbe only if
                     89:             # the dirs are different?
                     90:             if ($directories) {
                     91:                 $path_of{$ident}{todo_dir} = $directories;
                     92:             }
                     93:         }
                     94:
                     95:         if ( $type =~ /(todo|done|report)_file/xms ) {
                     96:             if ( my ( $pre, $post )
                     97:                 = $path_of{$ident}{$type} =~ /^(.*)$1(.*)\.txt$/ixms )
                     98:             {
                     99:                 foreach my $f qw( todo done report ) {
                    100:                     if ( !defined $path_of{$ident}{ $f . '_file' } ) {
                    101:                         $path_of{$ident}{ $f . '_file' }
                    102:                             = $pre . $f . $post . '.txt';
                    103:                     }
                    104:                 }
                    105:             }
                    106:         }
                    107:
                    108:         if ( defined $path_of{$ident}{todo_dir} ) {
                    109:             return File::Spec->catfile( $path_of{$ident}{todo_dir},
                    110:                 $path_of{$ident}{$type} );
                    111:         }
                    112:
                    113:         return;
                    114:     }
                    115:
1.3       andrew    116:     sub file {
1.2       andrew    117:         my ( $self, $file ) = @_;
                    118:         my $ident = ident($self);
                    119:
1.5       andrew    120:         if ( defined $file && exists $path_of{$ident}{$file} ) {
                    121:             $file = $self->_path_to($file);
                    122:         }
                    123:         else {
                    124:             $file = $self->_path_to( 'todo_file', $file );
1.2       andrew    125:         }
                    126:
1.5       andrew    127:         return $file;
1.3       andrew    128:     }
                    129:
                    130:     sub load {
                    131:         my ( $self, $file ) = @_;
                    132:         my $ident = ident($self);
                    133:
1.8     ! andrew    134:         $loaded_of{$ident} = undef;
        !           135:
        !           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;
1.8     ! andrew    163:             next if !length $_;
1.2       andrew    164:             push @list, Text::Todo::Entry->new($_);
                    165:         }
                    166:         close $fh or croak "Couldn't close [$file]: $!";
                    167:
1.8     ! andrew    168:         return wantarray ? @list : \@list;
1.2       andrew    169:     }
                    170:
                    171:     sub save {
                    172:         my ( $self, $file ) = @_;
                    173:         my $ident = ident($self);
                    174:
1.5       andrew    175:         $file = $self->file($file);
                    176:         if ( !defined $file ) {
1.6       andrew    177:             croak q{todo file can't be found};
1.5       andrew    178:         }
1.2       andrew    179:
                    180:         open my $fh, '>', $file or croak "Couldn't open [$file]: $!";
                    181:         foreach my $e ( @{ $list_of{$ident} } ) {
1.3       andrew    182:             print {$fh} $e->text . "\n"
                    183:                 or croak "Couldn't print to [$file]: $!";
1.2       andrew    184:         }
                    185:         close $fh or croak "Couldn't close [$file]: $!";
                    186:
                    187:         return 1;
                    188:     }
                    189:
                    190:     sub list {
1.3       andrew    191:         my ($self) = @_;
1.2       andrew    192:         my $ident = ident($self);
1.6       andrew    193:
1.2       andrew    194:         return if !$list_of{$ident};
1.6       andrew    195:         return wantarray ? @{ $list_of{$ident} } : $list_of{$ident};
1.5       andrew    196:     }
                    197:
                    198:     sub listpri {
                    199:         my ($self) = @_;
                    200:
                    201:         my @list = grep { $_->priority } $self->list;
                    202:
                    203:         return wantarray ? @list : \@list;
1.2       andrew    204:     }
1.1       andrew    205:
1.3       andrew    206:     sub add {
                    207:         my ( $self, $entry ) = @_;
                    208:         my $ident = ident($self);
                    209:
1.5       andrew    210:         if ( !ref $entry ) {
                    211:             $entry = Text::Todo::Entry->new($entry);
                    212:         }
                    213:         elsif ( ref $entry ne 'Text::Todo::Entry' ) {
                    214:             croak(
                    215:                 'entry is a ' . ref($entry) . ' not a Text::Todo::Entry!' );
                    216:         }
                    217:
                    218:         push @{ $list_of{$ident} }, $entry;
                    219:
                    220:         return $entry;
                    221:     }
                    222:
1.6       andrew    223:     sub del {
1.5       andrew    224:         my ( $self, $src ) = @_;
                    225:         my $ident = ident($self);
                    226:
1.6       andrew    227:         my $id = $self->_find_entry_id($src);
1.5       andrew    228:
                    229:         my @list = $self->list;
1.6       andrew    230:         my $entry = splice @list, $id, 1;
1.5       andrew    231:         $list_of{$ident} = \@list;
                    232:
                    233:         return $entry;
                    234:     }
                    235:
                    236:     sub move {
                    237:         my ( $self, $entry, $dst ) = @_;
                    238:         my $ident = ident($self);
                    239:
                    240:         my $src  = $self->_find_entry_id($entry);
                    241:         my @list = $self->list;
                    242:
1.6       andrew    243:         splice @list, $dst, 0, splice @list, $src, 1;
1.5       andrew    244:
                    245:         $list_of{$ident} = \@list;
                    246:
                    247:         return 1;
                    248:     }
                    249:
1.6       andrew    250:     sub listproj {
1.5       andrew    251:         my ( $self, $entry, $dst ) = @_;
                    252:         my $ident = ident($self);
                    253:
                    254:         my %available_projects;
1.6       andrew    255:         foreach my $e ( $self->list ) {
1.5       andrew    256:             foreach my $p ( $e->projects ) {
                    257:                 $available_projects{$p} = 1;
                    258:             }
                    259:         }
                    260:
                    261:         my @projects = sort keys %available_projects;
                    262:
                    263:         return wantarray ? @projects : \@projects;
                    264:     }
                    265:
1.8     ! andrew    266:     sub archive { carp 'unsupported'; return }
        !           267:
        !           268:     sub addto {
        !           269:         my ( $self, $file, $entry ) = @_;
        !           270:         my $ident = ident($self);
        !           271:
        !           272:         $file = $self->file($file);
        !           273:         if ( !defined $file ) {
        !           274:             croak q{file can't be found};
        !           275:         }
        !           276:
        !           277:         open my $fh, '>>', $file or croak "Couldn't open [$file]: $!";
        !           278:         print {$fh} $entry, "\n"
        !           279:             or croak "Couldn't print to [$file]: $!";
        !           280:         close $fh or croak "Couldn't close [$file]: $!";
        !           281:
        !           282:         if ( defined $loaded_of{$ident} && $file eq $loaded_of{$ident} ) {
        !           283:             return $self->load($file);
        !           284:         }
        !           285:
        !           286:         return 1;
        !           287:     }
1.5       andrew    288:
                    289:     sub _find_entry_id {
                    290:         my ( $self, $entry ) = @_;
                    291:         my $ident = ident($self);
                    292:
1.3       andrew    293:         if ( ref $entry ) {
                    294:             if ( ref $entry ne 'Text::Todo::Entry' ) {
                    295:                 croak(    'entry is a '
                    296:                         . ref($entry)
                    297:                         . ' not a Text::Todo::Entry!' );
                    298:             }
1.5       andrew    299:
                    300:             my @list = $self->list;
                    301:             foreach my $id ( 0 .. $#list ) {
                    302:                 if ( $list[$id] eq $entry ) {
                    303:                     return $id;
                    304:                 }
                    305:             }
1.3       andrew    306:         }
1.5       andrew    307:         elsif ( $entry =~ /^\d+$/xms ) {
                    308:             return $entry;
1.3       andrew    309:         }
                    310:
1.5       andrew    311:         croak "Invalid entry [$entry]!";
1.3       andrew    312:     }
1.2       andrew    313: }
1.1       andrew    314:
1.2       andrew    315: 1;    # Magic true value required at end of module
1.1       andrew    316: __END__
                    317:
                    318: =head1 NAME
                    319:
1.4       andrew    320: Text::Todo - Perl interface to todo_txt files
1.1       andrew    321:
1.6       andrew    322: =head1 VERSION
                    323:
                    324: I will have to figure out how to include $VERSION in this somehow.
                    325:
                    326: Perhaps RCS Id is good enough?
                    327:
1.7       andrew    328:     $Id: Todo.pm,v 1.6 2010/01/09 05:15:20 andrew Exp $
1.1       andrew    329:
                    330: =head1 SYNOPSIS
                    331:
                    332:     use Text::Todo;
                    333:
                    334: =head1 DESCRIPTION
                    335:
1.4       andrew    336: For more information see L<http://todotxt.com>
1.1       andrew    337:
                    338: =head1 INTERFACE
                    339:
1.2       andrew    340: =head2 new
                    341:
                    342: =head2 load
                    343:
                    344: =head2 save
                    345:
1.3       andrew    346: =head2 file
                    347:
1.2       andrew    348: =head2 list
1.3       andrew    349:
                    350: =head2 add
1.1       andrew    351:
                    352: =head1 DIAGNOSTICS
                    353:
                    354: =for author to fill in:
                    355:     List every single error and warning message that the module can
                    356:     generate (even the ones that will "never happen"), with a full
                    357:     explanation of each problem, one or more likely causes, and any
                    358:     suggested remedies.
                    359:
                    360: =over
                    361:
                    362: =item C<< Error message here, perhaps with %s placeholders >>
                    363:
                    364: [Description of error here]
                    365:
                    366: =item C<< Another error message here >>
                    367:
                    368: [Description of error here]
                    369:
                    370: [Et cetera, et cetera]
                    371:
                    372: =back
                    373:
                    374:
                    375: =head1 CONFIGURATION AND ENVIRONMENT
                    376:
                    377: Text::Todo requires no configuration files or environment variables.
                    378:
1.4       andrew    379: Someday it should be able to read and use the todo.sh config file.
                    380:
1.1       andrew    381:
                    382: =head1 DEPENDENCIES
                    383:
                    384: =for author to fill in:
                    385:     A list of all the other modules that this module relies upon,
                    386:     including any restrictions on versions, and an indication whether
                    387:     the module is part of the standard Perl distribution, part of the
                    388:     module's distribution, or must be installed separately. ]
                    389:
                    390: None.
                    391:
                    392:
                    393: =head1 INCOMPATIBILITIES
                    394:
                    395: None reported.
                    396:
                    397:
                    398: =head1 BUGS AND LIMITATIONS
                    399:
                    400: No bugs have been reported.
                    401:
                    402: Please report any bugs or feature requests to
                    403: C<bug-text-todo@rt.cpan.org>, or through the web interface at
                    404: L<http://rt.cpan.org>.
                    405:
                    406:
                    407: =head1 AUTHOR
                    408:
                    409: Andrew Fresh  C<< <andrew@cpan.org> >>
                    410:
                    411:
                    412: =head1 LICENSE AND COPYRIGHT
                    413:
                    414: Copyright (c) 2009, Andrew Fresh C<< <andrew@cpan.org> >>. All rights reserved.
                    415:
                    416: This module is free software; you can redistribute it and/or
                    417: modify it under the same terms as Perl itself. See L<perlartistic>.
                    418:
                    419:
                    420: =head1 DISCLAIMER OF WARRANTY
                    421:
                    422: BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
                    423: FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
                    424: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
                    425: PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
                    426: EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                    427: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
                    428: ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
                    429: YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
                    430: NECESSARY SERVICING, REPAIR, OR CORRECTION.
                    431:
                    432: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
                    433: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
                    434: REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
                    435: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
                    436: OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
                    437: THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
                    438: RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
                    439: FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
                    440: SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
                    441: SUCH DAMAGES.

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