2ck

todo.pl

2ck
Apr 29th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.36 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # added an undo feature <Fri May 11 17:38:57 CDT 2012>
  3. use feature qw/switch/;
  4. use File::Basename;
  5. use File::Copy;
  6.  
  7. my $todo = "$ENV{HOME}/TODO";
  8. my $todo_dir = "$ENV{HOME}/.tood";
  9. my $todo_tmp = "/tmp/tood_TODO";
  10. my @todo_backups = sort {
  11.     ($b =~ /\.(\d+)$/)[0] <=> ($a =~ /\.(\d+)$/)[0]
  12.         ||
  13.         uc($a) cmp uc($b)
  14. } glob "$todo_dir/TODO.{[0-9],[0-9][0-9]}";
  15. my $todo_nbackups = 10;
  16. sub usage
  17. {
  18.     print "Usage: ", basename($0) , " <subcommand> [<arguments>]\n";
  19.     print "Subcommands\n";
  20.     print "\tshow\n";
  21.     print "\tadd <new_task>\n";
  22.     print "\tins <position> <new_task>\n";
  23.     print "\tmv <position> <new_position>\n";
  24.     print "\trep <position> <new_task>\n";
  25.     print "\ttail <position> <tail text>\n";
  26.     print "\trm <position>\n";
  27.     print "\tundo\n";
  28. }
  29.  
  30. sub show
  31. {
  32.     if (! -z $todo)
  33.     {
  34.         open(TODO, '<', $todo);
  35.         $line = 1;
  36.    
  37.         while(<TODO>)
  38.         {
  39.             if ($line < 10)
  40.             {
  41.                 print " ";
  42.             }
  43.             print "[$line] $_";
  44.             $line++;
  45.         }
  46.     }
  47.     else {
  48.         print "Nothing to do!\n";
  49.     }
  50. }
  51.  
  52. sub add
  53. {
  54.     open(TODO, '>>', $todo);
  55.     if (@_ > 0) { print TODO join (" ", @_) . "\n";}
  56.     else
  57.     {
  58.         while (<STDIN>)
  59.         {
  60.             print TODO;
  61.         }
  62.     }
  63.     close(TODO);
  64. }
  65.  
  66. sub insert
  67. {
  68.     my $pos = 0;
  69.     my @new_tasks = ();
  70.     if (@_ and $_[0] =~ /[0-9]+/)
  71.     {
  72.         ($pos, @new_tasks) = @_;
  73.     }
  74.     else
  75.     {
  76.         $pos = 1;
  77.         @new_tasks = @_;
  78.     }
  79.     open(TODO, '<', $todo);
  80.     @items = <TODO>;
  81.     close(TODO);
  82.     if ($pos > @items)
  83.     {
  84.         add(@new_tasks);
  85.         return;
  86.     }
  87.     #$pos > 0 or $pos = 1;
  88.     open(TODO, '>', $todo);
  89.     $line_num = 1;
  90.     foreach $item (@items)
  91.     {
  92.         if ($line_num == $pos)
  93.         {
  94.             add(@new_tasks);
  95.             open(TODO, '>>', $todo);
  96.         }
  97.         print TODO $item;
  98.         $line_num++;
  99.     }
  100.     close(TODO);
  101. }
  102.  
  103. sub remove
  104. {
  105.     my @completed_tasks = @_;
  106.     open(TODO, '<', $todo);
  107.     @items = <TODO>;
  108.     close(TODO);
  109.     open(TODO, '>', $todo);
  110.     $line_num = 1;
  111.     foreach $item (@items)
  112.     {
  113.         $print_item = 1;
  114.         foreach (@completed_tasks)
  115.         {
  116.             if ($line_num == $_)
  117.             {
  118.                $print_item = 0;
  119.                $removed = $item;
  120.                last;
  121.             }
  122.         }
  123.         if ($print_item)
  124.         {
  125.             print TODO $item;
  126.         }
  127.         $line_num++;
  128.     }
  129.     close(TODO);
  130.     $removed;
  131. }
  132.  
  133. sub make_tmp_TODO
  134. {
  135.     copy($todo, $todo_tmp);
  136. }
  137.  
  138. sub unbackup_TODO
  139. {
  140.         copy("$todo_dir/TODO.0", $todo);
  141.         for (reverse @todo_backups)
  142.         {
  143.             m/\.([0-9]+)$/;
  144.             my $new_number = $1;
  145.             $new_number = ($new_number - 1);
  146.             ($new_number >= 0) and
  147.                 copy($_, "$todo_dir/TODO." . $new_number);
  148.         }
  149. }
  150.  
  151. sub back_up_TODO
  152. {
  153.     # rename the old ones
  154.     system("diff", $todo, $todo_tmp);
  155.     $return  = $?;
  156.     if ($return)
  157.     {
  158.         for (@todo_backups)
  159.         {
  160.             m/\.([0-9]+)$/;
  161.             my $new_number = $1;
  162.             $new_number = ($new_number + 1);
  163.             ($new_number < $todo_nbackups) and
  164.                 copy($_, "$todo_dir/TODO." . $new_number);
  165.         }
  166.         copy($todo_tmp, "$todo_dir/TODO.0");
  167.     }
  168. }
  169. given (shift @ARGV)
  170. {
  171.     # save the current TODO file as .tood/TODO.#
  172.     # where # is the count of other such files +1
  173.     make_tmp_TODO();
  174.     when (/show|ls|list/)
  175.     {
  176.         show;
  177.     }
  178.     when ("add")
  179.     {
  180.         add(@ARGV);
  181.     }
  182.     when ("ins")
  183.     {
  184.         insert(@ARGV);
  185.     }
  186.     when ("rm")
  187.     {
  188.         remove(@ARGV);
  189.     }
  190.     when ("mv")
  191.     {
  192.         # TODO: allow multiple items to move
  193.         chomp($task = remove(shift));
  194.         insert(shift, $task);
  195.     }
  196.     when ("tail")
  197.     {
  198.         chomp($task = remove($ARGV[0]));
  199.         insert(shift, $task . shift);
  200.     }
  201.     when ("rep")
  202.     {
  203.         remove($ARGV[0]);
  204.         insert(@ARGV);
  205.     }
  206.     when ("undo")
  207.     {
  208.         unbackup_TODO();
  209.     }
  210.     default
  211.     {
  212.         usage;
  213.     }
  214.     # if the real TODO does not differ from
  215.     # the recently made backup, then just delete it
  216. }
  217. back_up_TODO();
  218.  
  219. # vim:set ft=perl
Advertisement
Add Comment
Please, Sign In to add comment