/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-01-04 12:46:10 UTC
  • Revision ID: edam@waxworlds.org-20120104124610-nrrs6mv1btg390ni
rewrite file parsing to cache the last line and only display it when we know the current line is not a section heading "underline" line.

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;
39
41
    "  -a, --all              display all sections\n".
40
42
    "  -e, --edit             edit your todo file\n".
41
43
    "  -h, --headers          show setion headers\n".
42
 
    "  -s, --section=SECTION  display this named section\n".
 
44
    "      --section=SECTION  display this named section\n".
43
45
    "      --help     display this help and exit\n".
44
46
    "      --version  output version information and exit\n";
45
47
    exit( 0 );
127
129
    exit
128
130
}
129
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
                }
 
147
        }
 
148
 
 
149
        # replace tabs with 4 spaces
 
150
        $line =~ s/\t/    /g;
 
151
        
 
152
        # display the line
 
153
        print $line;
 
154
}
 
155
 
 
156
 
130
157
# scan through file
131
 
my $next_section = '';
 
158
my $candidate_section = '';
132
159
my $section = '';
133
 
my $old_section = '';
 
160
my $last_line = '';
134
161
open FILE, "<$todo_dir/todo" or die "can't open todo file";
135
 
while( <FILE> ) {
136
 
    my $display = 0;
137
 
 
 
162
while( <FILE> )
 
163
{
138
164
    # detect the line after section headings, and thus sections
139
 
    if( /^[-=]{2,}/ && $next_section ne '' ) {
140
 
        $section = $next_section;
141
 
        $next_section = '';
 
165
    if( /^[-=]{2,}/ && $candidate_section ne '' ) {
 
166
        $section = $candidate_section;
 
167
        $candidate_section = '';
 
168
                $last_line = '';
 
169
                next;
142
170
    }
 
171
 
143
172
    # 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
 
173
    if( /^[-_\.A-Za-z0-9 ]+$/ ) {
 
174
        $candidate_section = $_;
 
175
                chomp $candidate_section;
 
176
        }
149
177
    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
 
        }
 
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( ( $section eq $display_section ) ||
 
186
                ( $section && $display_all ) )
 
187
        {
 
188
                $last_line = $_;
175
189
    }
 
190
        else {
 
191
                $last_line = '';
 
192
        }
176
193
}
 
194
display_line( $last_line, $section ) if( $last_line ne '' );