/todo

To get this branch, use:
bzr branch http://bzr.ed.am/todo

« back to all changes in this revision

Viewing changes to todo

  • Committer: edam
  • Date: 2011-12-10 20:46:56 UTC
  • Revision ID: edam@waxworlds.org-20111210204656-ivc37tya904ogje5
fixed version info and error about failing to start the editor

Show diffs side-by-side

added added

removed removed

1
1
#!/usr/bin/perl
2
2
 
3
 
use feature "state";
4
 
 
5
3
use strict;
6
4
use warnings;
7
5
use Getopt::Long;
25
23
    'a|all' => \$display_all,
26
24
    'e|edit' => \$mode_edit,
27
25
    'h|headers' => \$display_headers,
28
 
    's|section=s' => \$display_section,
 
26
    'section=s' => \$display_section,
29
27
    'help' => \$mode_help,
30
28
    'version' => \$mode_version,
31
29
) ) {
36
34
# help mode
37
35
if( $mode_help ) {
38
36
    print "todo - display your todo file\n\n".
39
 
    "Usage: $app_name [OPTIONS]\n\n".
40
 
    "Options:\n".
41
 
    "  -a, --all              display all sections\n".
42
 
    "  -e, --edit             edit your todo file\n".
43
 
    "  -h, --headers          show setion headers\n".
44
 
    "  -s, --section=SECTION  display this named section\n".
45
 
    "      --help     display this help and exit\n".
46
 
    "      --version  output version information and exit\n";
 
37
        "Usage: $app_name [OPTIONS]\n\n".
 
38
        "Options:\n".
 
39
        "  -a, --all              display all sections\n".
 
40
        "  -e, --edit             edit your todo file\n".
 
41
        "  -h, --headers          show setion headers\n".
 
42
        "  -s, --section=SECTION  display this named section\n".
 
43
        "      --help     display this help and exit\n".
 
44
        "      --version  output version information and exit\n";
47
45
    exit( 0 );
48
46
}
49
47
 
50
48
# version mode
51
49
if( $mode_version ) {
52
50
    print "todo 1.0\n".
53
 
        "Copyright (C) 2011 Tim Marston.\n".
54
 
        "http://ed.am/software/todo\n";
 
51
                "Copyright (C) 2011 Tim Marston.\n".
 
52
                "http://ed.am/software/todo\n";
55
53
    exit( 0 );
56
54
}
57
55
 
71
69
if( $mode_edit )
72
70
{
73
71
    # create the todo directory, as necessary
74
 
    if( ! -d $todo_dir ) {
75
 
        mkdir $todo_dir or die "couldn't create todo directory";
76
 
        `bzr init --no-aliases -q "$todo_dir"`;
77
 
        $? == 0 or die "couldn't init bzr repo";
78
 
    }
79
 
    
80
 
    # create a default todo file, as necessary
81
 
    if( ! -f "$todo_dir/todo" ) {
82
 
        open FILE, ">$todo_dir/todo" or die "couldn't create default todo file";
83
 
        my $content = <<"EOT";
 
72
        if( ! -d $todo_dir ) {
 
73
                mkdir $todo_dir or die "couldn't create todo directory";
 
74
                `bzr init --no-aliases -q "$todo_dir"`;
 
75
                $? == 0 or die "couldn't init bzr repo";
 
76
        }
 
77
        
 
78
        # create a default todo file, as necessary
 
79
        if( ! -f "$todo_dir/todo" ) {
 
80
                open FILE, ">$todo_dir/todo" or die "couldn't create default todo file";
 
81
                my $content = <<"EOT";
84
82
<!-- This file uses Markdown syntax. For more info about Markdown
85
83
     syntax, see http://daringfireball.net/projects/markdown/syntax.
86
84
 
96
94
 
97
95
EOT
98
96
        print FILE $content or die "couldn't write default todo file";
99
 
        close FILE;
100
 
        `bzr add --no-aliases -q "$todo_dir/todo"`;
101
 
        $? == 0 or die "couldn't add todo file to bzr repo";
102
 
    }
 
97
                close FILE;
 
98
                `bzr add --no-aliases -q "$todo_dir/todo"`;
 
99
                $? == 0 or die "couldn't add todo file to bzr repo";
 
100
        }
103
101
 
104
102
    # determine editor from environment, default to "emacs -nw"
105
103
    my $editor = $ENV{ 'EDITOR' };
107
105
    my @exec_array = split( / +/, $editor );
108
106
    push( @exec_array, "$todo_dir/todo" );
109
107
 
110
 
    # detect emacs and try to use markdown-mode
111
 
    $exec_array[ 0 ] eq "emacs" and
112
 
        push( @exec_array, '--funcall=markdown-mode' );
 
108
        # detect emacs and try to use markdown-mode
 
109
        $exec_array[ 0 ] eq "emacs" and
 
110
                push( @exec_array, '--funcall=markdown-mode' );
113
111
 
114
112
    # edit todo file
115
113
    system( @exec_array );
116
 
    $? == 0 or
117
 
        die "can't start editor, check EDITOR envionment variable";
118
 
 
119
 
    # check for changes and commit it
120
 
    $output = `bzr status --no-aliases "$todo_dir/todo"`;
121
 
    $? == 0 or die "couldn't check bzr rerpo status";
122
 
    chomp $output;
123
 
    if( $output ne "" ) {
124
 
        `bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
125
 
        $? == 0 or die "couldn't commit to bzr repo";
126
 
    }
127
 
 
128
 
    # after editing, exit
129
 
    exit
130
 
}
131
 
 
132
 
# function to display a line
133
 
sub display_line
134
 
{
135
 
        my ( $line, $section ) = @_;
136
 
        state $old_section = '';
137
 
 
138
 
        # detect section change
139
 
        if( $section ne $old_section ) {
140
 
                $old_section = $section;
141
 
                
142
 
                # display section heading
143
 
                if( $display_headers || $display_all ) {
144
 
                        print "$section\n".
145
 
                                ( "=" x length( $section ) )."\n";
146
 
                }
 
114
        $? == 0 or
 
115
                die "can't start editor, check EDITOR envionment variable";
 
116
 
 
117
        # check for changes and commit it
 
118
        $output = `bzr status --no-aliases "$todo_dir/todo"`;
 
119
        $? == 0 or die "couldn't check bzr rerpo status";
 
120
        chomp $output;
 
121
        if( $output ne "" ) {
 
122
                `bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
 
123
                $? == 0 or die "couldn't commit to bzr repo";
147
124
        }
148
125
 
149
 
        # replace tabs with 4 spaces
150
 
        $line =~ s/\t/    /g;
151
 
        
152
 
        # display the line
153
 
        print $line;
 
126
        # after editing, exit
 
127
        exit
154
128
}
155
129
 
156
 
 
157
130
# scan through file
158
 
my $candidate_section = '';
 
131
my $next_section = '';
159
132
my $section = '';
160
 
my $last_line = '';
 
133
my $old_section = '';
161
134
open FILE, "<$todo_dir/todo" or die "can't open todo file";
162
 
while( <FILE> )
163
 
{
 
135
while( <FILE> ) {
 
136
    my $display = 0;
 
137
 
164
138
    # detect the line after section headings, and thus sections
165
 
    if( /^[-=]{2,}/ && $candidate_section ne '' ) {
166
 
        $section = $candidate_section;
167
 
        $candidate_section = '';
168
 
                $last_line = '';
169
 
                next;
 
139
    if( /^[-=]{2,}/ && $next_section ne '' ) {
 
140
                $section = $next_section;
 
141
                $next_section = '';
170
142
    }
171
 
 
172
143
    # detect section headings
173
 
    if( /^[-_\.A-Za-z0-9 ]+$/ ) {
174
 
        $candidate_section = $_;
175
 
                chomp $candidate_section;
176
 
        }
 
144
    elsif( /^[A-Z]+$/ ) {
 
145
                $next_section = $_;
 
146
                chomp( $next_section );
 
147
    }
 
148
    # we have neither a section heading nor the line after
177
149
    else {
178
 
        $candidate_section = '';
179
 
        }
180
 
 
181
 
        # display last line
182
 
        display_line( $last_line, $section ) if( $last_line ne '' );
183
 
 
184
 
    # display line
185
 
        if( ( lc( $section ) eq lc( $display_section ) ) ||
186
 
                ( $section && $display_all ) )
187
 
        {
188
 
                $last_line = $_;
 
150
                $next_section = '';
 
151
 
 
152
                # display line
 
153
                if( ( $section eq $display_section ) ||
 
154
                        ( $section && $display_all ) )
 
155
                {
 
156
                        # detect section change
 
157
                        if( $section ne $old_section ) {
 
158
                                $old_section = $section;
 
159
                                
 
160
                                # display section heading
 
161
                                if( $display_headers ||
 
162
                                        $display_all )
 
163
                                {
 
164
                                        print "$section\n".
 
165
                                                ( "=" x length( $section ) )."\n";
 
166
                                }
 
167
                        }
 
168
 
 
169
                        # replace tabs with 4 spaces
 
170
                        s/\t/    /g;
 
171
 
 
172
                        # display the line
 
173
                        print;
 
174
                }
189
175
    }
190
 
        else {
191
 
                $last_line = '';
192
 
        }
193
176
}
194
 
display_line( $last_line, $section ) if( $last_line ne '' );