Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # added an undo feature <Fri May 11 17:38:57 CDT 2012>
- use feature qw/switch/;
- use File::Basename;
- use File::Copy;
- my $todo = "$ENV{HOME}/TODO";
- my $todo_dir = "$ENV{HOME}/.tood";
- my $todo_tmp = "/tmp/tood_TODO";
- my @todo_backups = sort {
- ($b =~ /\.(\d+)$/)[0] <=> ($a =~ /\.(\d+)$/)[0]
- ||
- uc($a) cmp uc($b)
- } glob "$todo_dir/TODO.{[0-9],[0-9][0-9]}";
- my $todo_nbackups = 10;
- sub usage
- {
- print "Usage: ", basename($0) , " <subcommand> [<arguments>]\n";
- print "Subcommands\n";
- print "\tshow\n";
- print "\tadd <new_task>\n";
- print "\tins <position> <new_task>\n";
- print "\tmv <position> <new_position>\n";
- print "\trep <position> <new_task>\n";
- print "\ttail <position> <tail text>\n";
- print "\trm <position>\n";
- print "\tundo\n";
- }
- sub show
- {
- if (! -z $todo)
- {
- open(TODO, '<', $todo);
- $line = 1;
- while(<TODO>)
- {
- if ($line < 10)
- {
- print " ";
- }
- print "[$line] $_";
- $line++;
- }
- }
- else {
- print "Nothing to do!\n";
- }
- }
- sub add
- {
- open(TODO, '>>', $todo);
- if (@_ > 0) { print TODO join (" ", @_) . "\n";}
- else
- {
- while (<STDIN>)
- {
- print TODO;
- }
- }
- close(TODO);
- }
- sub insert
- {
- my $pos = 0;
- my @new_tasks = ();
- if (@_ and $_[0] =~ /[0-9]+/)
- {
- ($pos, @new_tasks) = @_;
- }
- else
- {
- $pos = 1;
- @new_tasks = @_;
- }
- open(TODO, '<', $todo);
- @items = <TODO>;
- close(TODO);
- if ($pos > @items)
- {
- add(@new_tasks);
- return;
- }
- #$pos > 0 or $pos = 1;
- open(TODO, '>', $todo);
- $line_num = 1;
- foreach $item (@items)
- {
- if ($line_num == $pos)
- {
- add(@new_tasks);
- open(TODO, '>>', $todo);
- }
- print TODO $item;
- $line_num++;
- }
- close(TODO);
- }
- sub remove
- {
- my @completed_tasks = @_;
- open(TODO, '<', $todo);
- @items = <TODO>;
- close(TODO);
- open(TODO, '>', $todo);
- $line_num = 1;
- foreach $item (@items)
- {
- $print_item = 1;
- foreach (@completed_tasks)
- {
- if ($line_num == $_)
- {
- $print_item = 0;
- $removed = $item;
- last;
- }
- }
- if ($print_item)
- {
- print TODO $item;
- }
- $line_num++;
- }
- close(TODO);
- $removed;
- }
- sub make_tmp_TODO
- {
- copy($todo, $todo_tmp);
- }
- sub unbackup_TODO
- {
- copy("$todo_dir/TODO.0", $todo);
- for (reverse @todo_backups)
- {
- m/\.([0-9]+)$/;
- my $new_number = $1;
- $new_number = ($new_number - 1);
- ($new_number >= 0) and
- copy($_, "$todo_dir/TODO." . $new_number);
- }
- }
- sub back_up_TODO
- {
- # rename the old ones
- system("diff", $todo, $todo_tmp);
- $return = $?;
- if ($return)
- {
- for (@todo_backups)
- {
- m/\.([0-9]+)$/;
- my $new_number = $1;
- $new_number = ($new_number + 1);
- ($new_number < $todo_nbackups) and
- copy($_, "$todo_dir/TODO." . $new_number);
- }
- copy($todo_tmp, "$todo_dir/TODO.0");
- }
- }
- given (shift @ARGV)
- {
- # save the current TODO file as .tood/TODO.#
- # where # is the count of other such files +1
- make_tmp_TODO();
- when (/show|ls|list/)
- {
- show;
- }
- when ("add")
- {
- add(@ARGV);
- }
- when ("ins")
- {
- insert(@ARGV);
- }
- when ("rm")
- {
- remove(@ARGV);
- }
- when ("mv")
- {
- # TODO: allow multiple items to move
- chomp($task = remove(shift));
- insert(shift, $task);
- }
- when ("tail")
- {
- chomp($task = remove($ARGV[0]));
- insert(shift, $task . shift);
- }
- when ("rep")
- {
- remove($ARGV[0]);
- insert(@ARGV);
- }
- when ("undo")
- {
- unbackup_TODO();
- }
- default
- {
- usage;
- }
- # if the real TODO does not differ from
- # the recently made backup, then just delete it
- }
- back_up_TODO();
- # vim:set ft=perl
Advertisement
Add Comment
Please, Sign In to add comment