Annotation of todotxt/Text-Todo/bin/todo.pl, Revision 1.5
1.1 andrew 1: #!/usr/bin/perl
1.5 ! andrew 2: # $RedRiver: todo.pl,v 1.4 2010/01/10 23:26:04 andrew Exp $
1.1 andrew 3: ########################################################################
4: # todo.pl *** a perl version of todo.sh. Uses Text::Todo.
5: #
6: # 2010.01.07 #*#*# andrew fresh <andrew@cpan.org>
7: ########################################################################
8: # Copyright 2010 Andrew Fresh, all rights reserved
9: #
10: # This program is free software; you can redistribute it and/or modify
11: # it under the same terms as Perl itself.
12: ########################################################################
13: use strict;
14: use warnings;
15:
16: use Data::Dumper;
1.2 andrew 17:
18: use Getopt::Std;
1.1 andrew 19: use Text::Todo;
20:
21: use version; our $VERSION = qv('0.0.1');
22:
1.2 andrew 23: # option defaults
24: my $config_file = $ENV{HOME} . '/todo.cfg';
1.3 andrew 25: CONFIG: foreach my $f ( $config_file, $ENV{HOME} . '/.todo.cfg', ) {
26: if ( -e $f ) {
27: $config_file = $f;
28: last CONFIG;
29: }
30: }
1.2 andrew 31:
32: my %actions = (
1.3 andrew 33: add => \&add,
34: addto => \&addto,
35: append => \&append,
36: archive => \&archive,
37: command => \&command,
38: del => \&del,
39: depri => \&depri,
40: do => \&mark_done,
41: help => \&help,
1.2 andrew 42: list => \&list,
1.3 andrew 43: listall => \&listall,
44: listcon => \&listcon,
45: listfile => \&listfile,
46: listpri => \&listpri,
47: listproj => \&listproj,
48: move => \&move,
49: prepend => \&prepend,
50: pri => \&pri,
51: replace => \&replace,
52: report => \&report,
1.2 andrew 53: );
54:
55: my %aliases = (
56: a => 'add',
57: app => 'append',
58: rm => 'del',
59: dp => 'depri',
60: ls => 'list',
61: lsa => 'listall',
62: lsc => 'listcon',
63: lf => 'listfile',
1.3 andrew 64: lsp => 'listpri',
1.2 andrew 65: lsprj => 'listproj',
66: mv => 'move',
67: prep => 'prepend',
68: p => 'pri',
69: );
70:
71: my %opts;
72: getopts( '@+d:fhpPntvV', \%opts );
73:
74: my $action = shift @ARGV;
75: if ( $action && $action eq 'command' ) {
76:
77: # We don't support action scripts so . . .
78: $action = shift @ARGV;
79: }
80: if ( $action && exists $aliases{$action} ) {
81: $action = $aliases{$action};
82: }
83:
84: if ( $opts{h} || !$action ) {
85: usage( $opts{h} );
86: }
87:
88: my @unsupported = grep { defined $opts{$_} } qw( @ + f h p P n t v V );
89: if (@unsupported) {
1.3 andrew 90: warn 'Unsupported options: ' . ( join q{, }, @unsupported ) . "\n";
1.2 andrew 91: }
92:
93: if ( $opts{d} ) {
94: $config_file = $opts{d};
95: }
96:
97: if ( exists $actions{$action} ) {
98: my $config = read_config($config_file);
99: my $action = $actions{$action}->( $config, @ARGV );
100: }
101: else {
102: usage();
103: }
104:
1.3 andrew 105: sub add {
106: my ( $config, $entry ) = @_;
107: if ( !$entry ) {
108: die "usage: todo.pl add 'item'\n";
109: }
110:
111: my $todo = Text::Todo->new($config);
112: if ( $todo->add($entry) ) {
113: my @list = $todo->list;
114: my $lines = scalar @list;
115:
116: print "TODO: '$entry' added on line $lines\n";
117:
118: return $lines;
119: }
120: die "Unable to add [$entry]\n";
121: }
122:
123: sub addto {
124: my ( $config, $file, $entry ) = @_;
125: if ( !( $file && $entry ) ) {
126: die "usage: todo.pl addto DEST 'TODO ITEM'\n";
127: }
128:
129: my $todo = Text::Todo->new($config);
130:
131: $file = $todo->file($file);
132: if ( $todo->addto( $file, $entry ) ) {
133: my @list = $todo->listfile($file);
134: my $lines = scalar @list;
135:
136: print "TODO: '$entry' added to $file on line $lines\n";
137:
138: return $lines;
139: }
140: die "Unable to add [$entry]\n";
141: }
142:
1.4 andrew 143: sub append {
144: my ( $config, $line, $text ) = @_;
145: if ( !( $line && $text && $line =~ /^\d+$/xms ) ) {
146: die 'usage: todo.pl append ITEM# "TEXT TO APPEND"' . "\n";
147: }
148:
149: my $todo = Text::Todo->new($config);
150: my $entry = $todo->list->[ $line - 1 ];
151:
152: if ( $entry->append($text) && $todo->save ) {
153: return printf "%02d: %s\n", $line, $entry->text;
154: }
155: die "Unable to append\n";
156: }
157:
1.5 ! andrew 158: sub archive {
! 159: my ( $config ) = @_;
! 160: my $todo = Text::Todo->new($config);
! 161:
! 162: my $file = $todo->file;
! 163:
! 164: my $archived = $todo->archive;
! 165: if (defined $archived) {
! 166: return print "TODO: $file archived.\n";
! 167: }
! 168: die "Unable to archive $file\n";
! 169: }
! 170:
1.3 andrew 171: sub command { return &unsupported }
172: sub del { return &unsupported }
173: sub depri { return &unsupported }
174: sub mark_done { return &unsupported }
175: sub help { return &unsupported }
176:
1.2 andrew 177: sub list {
1.3 andrew 178: my ( $config, $term ) = @_;
179: my $todo = Text::Todo->new($config);
180:
181: my @list = _number_list( $todo->list );
182: my $shown = _show_sorted_list( $term, @list );
183:
184: return _show_list_footer( $shown, scalar @list, $config->{todo_file} );
185: }
186:
187: sub listall {
188: my ( $config, $term ) = @_;
189: my $todo = Text::Todo->new($config);
190:
191: my @list = _number_list(
192: $todo->listfile('todo_file'),
193: $todo->listfile('done_file'),
194: );
195: my $shown = _show_sorted_list( $term, @list );
196:
197: return _show_list_footer( $shown, scalar @list, $config->{'todo_dir'} );
198: }
199:
200: sub listcon {
1.2 andrew 201: my ($config) = @_;
202: my $todo = Text::Todo->new($config);
1.3 andrew 203: return print map {"\@$_\n"} $todo->listcon;
204: }
1.2 andrew 205:
1.3 andrew 206: sub listfile {
207: my ( $config, $file, $term ) = @_;
208: if ( !$file ) {
209: die "usage: todo.pl listfile SRC [TERM]\n";
1.2 andrew 210: }
1.3 andrew 211: my $todo = Text::Todo->new($config);
212:
213: my @list = _number_list( $todo->listfile($file) );
214: my $shown = _show_sorted_list( $term, @list );
215:
216: return _show_list_footer( $shown, scalar @list, $file );
217: }
218:
219: sub listpri {
220: my ( $config, $pri ) = @_;
221:
222: my $todo = Text::Todo->new($config);
223:
224: my @list = _number_list( $todo->listfile('todo_file') );
225: my @pri_list;
226: if ($pri) {
227: $pri = uc $pri;
228: if ( $pri !~ /^[A-Z]$/xms ) {
229: die "usage: todo.pl listpri PRIORITY\n",
230: "note: PRIORITY must a single letter from A to Z.\n";
231: }
232: @pri_list = grep {
233: defined $_->{entry}->priority
234: && $_->{entry}->priority eq $pri
235: } @list;
236: }
237: else {
238: @pri_list = grep { $_->{entry}->priority } @list;
239: }
240:
241: my $shown = _show_sorted_list( undef, @pri_list );
242:
243: return _show_list_footer( $shown, scalar @list, $config->{todo_file} );
244: }
245:
246: sub listproj {
247: my ($config) = @_;
248: my $todo = Text::Todo->new($config);
249: return print map {"\+$_\n"} $todo->listproj;
250: }
251:
252: sub move { return &unsupported }
253: sub prepend { return &unsupported }
254: sub pri { return &unsupported }
255: sub replace { return &unsupported }
256: sub report { return &unsupported }
257:
258: sub _number_list {
259: my (@list) = @_;
260:
261: my $line = 1;
262: return map { { line => $line++, entry => $_, } } @list;
263: }
264:
265: sub _show_sorted_list {
266: my ( $term, @list ) = @_;
267: $term = defined $term ? quotemeta($term) : '';
268:
269: my $shown = 0;
1.4 andrew 270: my @sorted
271: = map { sprintf "%02d %s", $_->{line}, $_->{entry}->text }
272: sort { lc $a->{entry}->text cmp lc $b->{entry}->text } @list;
273:
274: foreach my $line ( grep {/$term/xms} @sorted ) {
275: print $line, "\n";
1.3 andrew 276: $shown++;
277: }
278:
279: return $shown;
280: }
281:
282: sub _show_list_footer {
283: my ( $shown, $total, $file ) = @_;
284:
285: $shown ||= 0;
286: $total ||= 0;
287:
288: print "-- \n";
289: print "TODO: $shown of $total tasks shown from $file\n";
290:
291: return 1;
1.2 andrew 292: }
293:
294: sub unsupported { die "Unsupported action\n" }
295:
296: sub usage {
297: my ($long) = @_;
298:
299: print <<'EOL';
300: * command list taken from todo.sh for compatibility
301: Usage: todo.pl [-fhpantvV] [-d todo_config] action
302: EOL
303:
304: if ($long) {
305: print <<'EOL';
1.3 andrew 306:
1.2 andrew 307: Actions:
308: add|a "THING I NEED TO DO +project @context"
309: addto DEST "TEXT TO ADD"
310: append|app NUMBER "TEXT TO APPEND"
311: archive
312: command [ACTIONS]
313: del|rm NUMBER [TERM]
314: dp|depri NUMBER
315: do NUMBER
316: help
317: list|ls [TERM...]
318: listall|lsa [TERM...]
319: listcon|lsc
320: listfile|lf SRC [TERM...]
321: listpri|lsp [PRIORITY]
322: listproj|lsprj
323: move|mv NUMBER DEST [SRC]
324: prepend|prep NUMBER "TEXT TO PREPEND"
325: pri|p NUMBER PRIORITY
326: replace NUMBER "UPDATED TODO"
327: report
328: EOL
329: }
330: else {
331: print <<'EOL';
332: Try 'todo.pl -h' for more information.
333: EOL
334: }
335:
336: exit;
337: }
338:
339: sub read_config {
340: my ($file) = @_;
341:
342: my %config;
1.3 andrew 343: open my $fh, '< ', $file or die "Unable to open [$file]: $!";
1.2 andrew 344: LINE: while (<$fh>) {
345: s/\r?\n$//xms;
346: s/\s*\#.*$//xms;
347: next LINE unless $_;
348:
349: if (s/^\s*export\s+//xms) {
350: my ( $key, $value ) = /^([^=]+)\s*=\s*"?(.*?)"?\s*$/xms;
351: if ($key) {
352: foreach my $k ( keys %config ) {
353: $value =~ s/\$\Q$k\E/$config{$k}/gxms;
354: $value =~ s/\${\Q$k\E}/$config{$k}/gxms;
355: }
356: foreach my $k ( keys %ENV ) {
357: $value =~ s/\$\Q$k\E/$ENV{$k}/gxms;
358: $value =~ s/\${\Q$k\E}/$ENV{$k}/gxms;
359: }
360: $value =~ s/\$\w+//gxms;
361: $value =~ s/\${\w+}//gxms;
362:
363: $config{$key} = $value;
364: }
365: }
366: }
367: close $fh;
1.1 andrew 368:
1.2 andrew 369: my %lc_config;
370: foreach my $k ( keys %config ) {
371: $lc_config{ lc($k) } = $config{$k};
372: }
1.1 andrew 373:
1.2 andrew 374: return \%lc_config;
1.1 andrew 375: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>