Skip to content

Instantly share code, notes, and snippets.

@djanatyn
Created July 18, 2011 06:16
Show Gist options
  • Save djanatyn/1088662 to your computer and use it in GitHub Desktop.
Save djanatyn/1088662 to your computer and use it in GitHub Desktop.
it is finished.
#!/usr/bin/perl -w
use strict;
sub error {
my ($error) = @_;
print $error;
print "ptd - a todo list utility.\n";
print "usage:\n";
print " ptd <action> [options]\n";
print "actions:\n";
print "-l list all entries\n";
print "-a [entry] add [entry] as a todo item\n";
print "-t [number] toggle an item's todo/done status\n";
print "-r remove all completed tasks from the list\n";
print "-r [number] remove a specific task from the list\n";
exit;
}
sub addEntry {
my ($entry) = @_;
open(TODO, '>>./.ptd');
print TODO "todo: $entry \n";
print "added '$entry' to todo list.\n";
exit;
}
sub countItems {
my ($target) = @_;
open(TODO, './.ptd') or die 'please create a .ptd file';
@_ = ();
for $_ (<TODO>) { push @_, $1 if /^$target:(.*)$/; }
close TODO;
return @_;
}
sub printTodo {
my @todo = countItems('todo'); my @done = countItems('done');
my $x = 1;
print "todo:\n";
for my $todo (@todo) { print "$x:$todo\n"; $x++; }
print "done:\n";
for my $done (@done) { print "$x:$done\n"; $x++; }
exit;
}
sub toggleItem {
my ($num) = @_; my @items = ('');
open(TODO, './.ptd'); my @lines = <TODO>; close TODO;
for my $_ (@lines) { if (/^todo:/) { push @items, $_; } }
for my $_ (@lines) { if (/^done:/) { push @items, $_; } }
open(TODO, '>./.ptd');
for $_ (@lines) {
if (/$items[$num]/) { s/(todo|done)/$1 eq 'todo' ? 'done' : 'todo'/e; }
print TODO;
}
close TODO;
print "toggled status of $items[$num]";
exit;
}
sub removeItems {
open(TODO, './.ptd'); my @lines = <TODO>; close TODO;
my @newlines; my $removed = 0;
for my $_ (@lines) {
unless (/^done/) {push @newlines, $_; };
if (/^done/) { $removed++; };
}
open(TODO, '>./.ptd'); for $_ (@newlines) { print TODO; }; close TODO;
print "removed $removed completed items\n";
exit;
}
sub removeItem {
my ($num) = @_; my @items = ('');
open(TODO, './.ptd'); my @lines = <TODO>; close TODO;
for my $_ (@lines) { if (/^todo:/) { push @items, $_; } }
for my $_ (@lines) { if (/^done:/) { push @items, $_; } }
open(TODO, '>./.ptd');
for my $_ (@lines) { unless (/$items[$num]/) { print TODO; } } close TODO;
print "removed $items[$num]";
}
my $args = $#ARGV;
$_ = $ARGV[0];
if ($args == -1) { error("please specify at least one arguement\n\n"); }
if ($args == 0) {
if (/-l/) { printTodo; }
if (/-r/) { removeItems; }
else { error("i don't understand what you're saying\n\n"); }
}
if ($args == 1) {
if (/-a/) { addEntry($ARGV[1]); }
if (/-t/) { toggleItem($ARGV[1]); }
if (/-r/) { removeItem($ARGV[1]); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment