Annotation of todotxt/Text-Todo/bin/todo.pl, Revision 1.7
1.1 andrew 1: #!/usr/bin/perl
1.6 andrew 2: # $RedRiver: todo.pl,v 1.5 2010/01/10 23:37:12 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:
1.6 andrew 88: my @unsupported = grep { defined $opts{$_} } qw( @ + f h p P t v V );
1.2 andrew 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 {
1.7 ! andrew 106: my ( $config, @entry ) = @_;
! 107: if ( !@entry ) {
1.3 andrew 108: die "usage: todo.pl add 'item'\n";
109: }
110:
1.7 ! andrew 111: my $entry = join q{ }, @entry;
! 112:
1.3 andrew 113: my $todo = Text::Todo->new($config);
114: if ( $todo->add($entry) ) {
115: my @list = $todo->list;
116: my $lines = scalar @list;
117:
118: print "TODO: '$entry' added on line $lines\n";
119:
120: return $lines;
121: }
122: die "Unable to add [$entry]\n";
123: }
124:
125: sub addto {
1.7 ! andrew 126: my ( $config, $file, @entry ) = @_;
! 127: if ( !( $file && @entry ) ) {
1.3 andrew 128: die "usage: todo.pl addto DEST 'TODO ITEM'\n";
129: }
130:
1.7 ! andrew 131: my $entry = join q{ }, @entry;
! 132:
1.3 andrew 133: my $todo = Text::Todo->new($config);
134:
135: $file = $todo->file($file);
136: if ( $todo->addto( $file, $entry ) ) {
137: my @list = $todo->listfile($file);
138: my $lines = scalar @list;
139:
140: print "TODO: '$entry' added to $file on line $lines\n";
141:
142: return $lines;
143: }
144: die "Unable to add [$entry]\n";
145: }
146:
1.4 andrew 147: sub append {
1.7 ! andrew 148: my ( $config, $line, @text) = @_;
! 149: if ( !( $line && @text && $line =~ /^\d+$/xms ) ) {
1.4 andrew 150: die 'usage: todo.pl append ITEM# "TEXT TO APPEND"' . "\n";
151: }
1.7 ! andrew 152:
! 153: my $text = join q{ }, @text;
1.4 andrew 154:
155: my $todo = Text::Todo->new($config);
156: my $entry = $todo->list->[ $line - 1 ];
157:
158: if ( $entry->append($text) && $todo->save ) {
159: return printf "%02d: %s\n", $line, $entry->text;
160: }
161: die "Unable to append\n";
162: }
163:
1.5 andrew 164: sub archive {
165: my ( $config ) = @_;
166: my $todo = Text::Todo->new($config);
167:
168: my $file = $todo->file;
169:
170: my $archived = $todo->archive;
171: if (defined $archived) {
172: return print "TODO: $file archived.\n";
173: }
174: die "Unable to archive $file\n";
175: }
176:
1.3 andrew 177: sub command { return &unsupported }
1.6 andrew 178:
179: sub del {
180: my ( $config, $line ) = @_;
181: if ( !( $line && $line =~ /^\d+$/xms ) ) {
182: die 'usage: todo.pl del ITEM#' . "\n";
183: }
184: my $todo = Text::Todo->new($config);
185:
186: my $entry = $todo->list->[$line - 1];
187: print "Delete '" . $entry->text . "'? (y/n)\n";
188: warn "XXX No delete confirmation currently!\n";
189:
190: if ($opts{n}) {
191: if ($todo->del($entry) && $todo->save) {
192: return print 'TODO: \'', $entry->text, "' deleted.\n";
193: }
194: }
195: else {
196: my $text = $entry->text;
197: if ($entry->replace(q{}) && $todo->save) {
198: return print 'TODO: \'', $text, "' deleted.\n";
199: }
200: }
201:
202: die "Unable to delete entry\n";
203: }
204:
1.3 andrew 205: sub depri { return &unsupported }
206: sub mark_done { return &unsupported }
207: sub help { return &unsupported }
208:
1.2 andrew 209: sub list {
1.3 andrew 210: my ( $config, $term ) = @_;
211: my $todo = Text::Todo->new($config);
212:
213: my @list = _number_list( $todo->list );
214: my $shown = _show_sorted_list( $term, @list );
215:
216: return _show_list_footer( $shown, scalar @list, $config->{todo_file} );
217: }
218:
219: sub listall {
220: my ( $config, $term ) = @_;
221: my $todo = Text::Todo->new($config);
222:
223: my @list = _number_list(
224: $todo->listfile('todo_file'),
225: $todo->listfile('done_file'),
226: );
227: my $shown = _show_sorted_list( $term, @list );
228:
229: return _show_list_footer( $shown, scalar @list, $config->{'todo_dir'} );
230: }
231:
232: sub listcon {
1.2 andrew 233: my ($config) = @_;
234: my $todo = Text::Todo->new($config);
1.3 andrew 235: return print map {"\@$_\n"} $todo->listcon;
236: }
1.2 andrew 237:
1.3 andrew 238: sub listfile {
239: my ( $config, $file, $term ) = @_;
240: if ( !$file ) {
241: die "usage: todo.pl listfile SRC [TERM]\n";
1.2 andrew 242: }
1.3 andrew 243: my $todo = Text::Todo->new($config);
244:
245: my @list = _number_list( $todo->listfile($file) );
246: my $shown = _show_sorted_list( $term, @list );
247:
248: return _show_list_footer( $shown, scalar @list, $file );
249: }
250:
251: sub listpri {
252: my ( $config, $pri ) = @_;
253:
254: my $todo = Text::Todo->new($config);
255:
256: my @list = _number_list( $todo->listfile('todo_file') );
257: my @pri_list;
258: if ($pri) {
259: $pri = uc $pri;
260: if ( $pri !~ /^[A-Z]$/xms ) {
261: die "usage: todo.pl listpri PRIORITY\n",
262: "note: PRIORITY must a single letter from A to Z.\n";
263: }
264: @pri_list = grep {
265: defined $_->{entry}->priority
266: && $_->{entry}->priority eq $pri
267: } @list;
268: }
269: else {
270: @pri_list = grep { $_->{entry}->priority } @list;
271: }
272:
273: my $shown = _show_sorted_list( undef, @pri_list );
274:
275: return _show_list_footer( $shown, scalar @list, $config->{todo_file} );
276: }
277:
278: sub listproj {
279: my ($config) = @_;
280: my $todo = Text::Todo->new($config);
281: return print map {"\+$_\n"} $todo->listproj;
282: }
283:
284: sub move { return &unsupported }
285: sub prepend { return &unsupported }
286: sub pri { return &unsupported }
287: sub replace { return &unsupported }
288: sub report { return &unsupported }
289:
290: sub _number_list {
291: my (@list) = @_;
292:
293: my $line = 1;
294: return map { { line => $line++, entry => $_, } } @list;
295: }
296:
297: sub _show_sorted_list {
298: my ( $term, @list ) = @_;
299: $term = defined $term ? quotemeta($term) : '';
300:
301: my $shown = 0;
1.4 andrew 302: my @sorted
303: = map { sprintf "%02d %s", $_->{line}, $_->{entry}->text }
304: sort { lc $a->{entry}->text cmp lc $b->{entry}->text } @list;
305:
306: foreach my $line ( grep {/$term/xms} @sorted ) {
307: print $line, "\n";
1.3 andrew 308: $shown++;
309: }
310:
311: return $shown;
312: }
313:
314: sub _show_list_footer {
315: my ( $shown, $total, $file ) = @_;
316:
317: $shown ||= 0;
318: $total ||= 0;
319:
320: print "-- \n";
321: print "TODO: $shown of $total tasks shown from $file\n";
322:
323: return 1;
1.2 andrew 324: }
325:
326: sub unsupported { die "Unsupported action\n" }
327:
328: sub usage {
329: my ($long) = @_;
330:
331: print <<'EOL';
332: * command list taken from todo.sh for compatibility
333: Usage: todo.pl [-fhpantvV] [-d todo_config] action
334: EOL
335:
336: if ($long) {
337: print <<'EOL';
1.3 andrew 338:
1.2 andrew 339: Actions:
340: add|a "THING I NEED TO DO +project @context"
341: addto DEST "TEXT TO ADD"
342: append|app NUMBER "TEXT TO APPEND"
343: archive
344: command [ACTIONS]
345: del|rm NUMBER [TERM]
346: dp|depri NUMBER
347: do NUMBER
348: help
349: list|ls [TERM...]
350: listall|lsa [TERM...]
351: listcon|lsc
352: listfile|lf SRC [TERM...]
353: listpri|lsp [PRIORITY]
354: listproj|lsprj
355: move|mv NUMBER DEST [SRC]
356: prepend|prep NUMBER "TEXT TO PREPEND"
357: pri|p NUMBER PRIORITY
358: replace NUMBER "UPDATED TODO"
359: report
360: EOL
361: }
362: else {
363: print <<'EOL';
364: Try 'todo.pl -h' for more information.
365: EOL
366: }
367:
368: exit;
369: }
370:
371: sub read_config {
372: my ($file) = @_;
373:
374: my %config;
1.3 andrew 375: open my $fh, '< ', $file or die "Unable to open [$file]: $!";
1.2 andrew 376: LINE: while (<$fh>) {
377: s/\r?\n$//xms;
378: s/\s*\#.*$//xms;
379: next LINE unless $_;
380:
381: if (s/^\s*export\s+//xms) {
382: my ( $key, $value ) = /^([^=]+)\s*=\s*"?(.*?)"?\s*$/xms;
383: if ($key) {
384: foreach my $k ( keys %config ) {
385: $value =~ s/\$\Q$k\E/$config{$k}/gxms;
386: $value =~ s/\${\Q$k\E}/$config{$k}/gxms;
387: }
388: foreach my $k ( keys %ENV ) {
389: $value =~ s/\$\Q$k\E/$ENV{$k}/gxms;
390: $value =~ s/\${\Q$k\E}/$ENV{$k}/gxms;
391: }
392: $value =~ s/\$\w+//gxms;
393: $value =~ s/\${\w+}//gxms;
394:
395: $config{$key} = $value;
396: }
397: }
398: }
399: close $fh;
1.1 andrew 400:
1.2 andrew 401: my %lc_config;
402: foreach my $k ( keys %config ) {
403: $lc_config{ lc($k) } = $config{$k};
404: }
1.1 andrew 405:
1.2 andrew 406: return \%lc_config;
1.1 andrew 407: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>