/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: 2012-08-01 19:57:56 UTC
  • Revision ID: tim@ed.am-20120801195756-qaxahzf9sikcbv6e
removed --list option because it was confusing and unnecessary

Show diffs side-by-side

added added

removed removed

1
1
#!/usr/bin/perl
2
2
 
 
3
use feature "state";
 
4
 
3
5
use strict;
4
6
use warnings;
5
7
use Getopt::Long;
23
25
    'a|all' => \$display_all,
24
26
    'e|edit' => \$mode_edit,
25
27
    'h|headers' => \$display_headers,
26
 
    'section=s' => \$display_section,
 
28
    's|section=s' => \$display_section,
27
29
    'help' => \$mode_help,
28
30
    'version' => \$mode_version,
29
 
) ) {
 
31
) || @ARGV > 0 ) {
30
32
    print "Try `$app_name --help' for more information.\n";
31
33
    exit( 1 );
32
34
}
34
36
# help mode
35
37
if( $mode_help ) {
36
38
    print "todo - display your todo file\n\n".
 
39
        #01234567890123456789012345678901234567890123456789012345678901234567890123456789
37
40
    "Usage: $app_name [OPTIONS]\n\n".
38
41
    "Options:\n".
39
42
    "  -a, --all              display all sections\n".
40
43
    "  -e, --edit             edit your todo file\n".
41
44
    "  -h, --headers          show setion headers\n".
42
 
    "  -s, --section=SECTION  display this named section\n".
 
45
    "  -s, --section=SECTION  display sections matching the regular expression\n".
43
46
    "      --help     display this help and exit\n".
44
 
    "      --version  output version information 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";
45
55
    exit( 0 );
46
56
}
47
57
 
48
58
# version mode
49
59
if( $mode_version ) {
50
60
    print "todo 1.0\n".
51
 
        "Copyright (C) 2011 Tim Marston.\n".
 
61
        "Copyright (C) 2011, 2012 Tim Marston.\n".
52
62
        "http://ed.am/software/todo\n";
53
63
    exit( 0 );
54
64
}
100
110
    }
101
111
 
102
112
    # determine editor from environment, default to "emacs -nw"
103
 
    my $editor = $ENV{ 'EDITOR' };
104
 
    defined $editor or $editor = 'emacs -nw';
 
113
        my $editor;
 
114
        $editor = $ENV{ 'EDITOR' };
 
115
        defined $editor or $editor = 'emacs -nw';
105
116
    my @exec_array = split( / +/, $editor );
106
117
    push( @exec_array, "$todo_dir/todo" );
107
118
 
127
138
    exit
128
139
}
129
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
                }
 
156
        }
 
157
 
 
158
        # replace tabs with 4 spaces
 
159
        $line =~ s/\t/    /g;
 
160
        
 
161
        # display the line
 
162
        print $line;
 
163
}
 
164
 
 
165
 
130
166
# scan through file
131
 
my $next_section = '';
 
167
my $candidate_section = '';
132
168
my $section = '';
133
 
my $old_section = '';
 
169
my $last_line = '';
134
170
open FILE, "<$todo_dir/todo" or die "can't open todo file";
135
 
while( <FILE> ) {
136
 
    my $display = 0;
137
 
 
 
171
while( <FILE> )
 
172
{
138
173
    # detect the line after section headings, and thus sections
139
 
    if( /^[-=]{2,}/ && $next_section ne '' ) {
140
 
        $section = $next_section;
141
 
        $next_section = '';
 
174
    if( /^[-=]{2,}/ && $candidate_section ne '' ) {
 
175
        $section = $candidate_section;
 
176
        $candidate_section = '';
 
177
                $last_line = '';
 
178
                next;
142
179
    }
 
180
 
143
181
    # detect section headings
144
 
    elsif( /^[-_\.A-Za-z0-9 ]+$/ ) {
145
 
        $next_section = $_;
146
 
        chomp( $next_section );
147
 
    }
148
 
    # we have neither a section heading nor the line after
 
182
    if( /^[-_\.A-Za-z0-9 ]+$/ ) {
 
183
        $candidate_section = $_;
 
184
                chomp $candidate_section;
 
185
        }
149
186
    else {
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
 
        }
 
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 = $_;
175
198
    }
 
199
        else {
 
200
                $last_line = '';
 
201
        }
176
202
}
 
203
display_line( $last_line, $section ) if( $last_line ne '' );