/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:34:05 UTC
  • Revision ID: edam@waxworlds.org-20111210203405-tfo68eb3s7hwmhld
added default todo file content and changed main todo section name to "TODO"

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