diff options
author | John Ankarström <john@ankarstrom.se> | 2021-05-26 15:37:00 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-05-26 15:37:00 +0200 |
commit | 0fcae069b0f4c08a11d39eea8cd05e83ae8423fa (patch) | |
tree | 22e3ec727a34bb8046a9a2f51fa704735a45c01c | |
parent | c90d68024af530aea52f899ed1a2362ca06768c9 (diff) | |
download | mum-0fcae069b0f4c08a11d39eea8cd05e83ae8423fa.tar.gz |
mum: Add 'l' command (view in pager)
-rwxr-xr-x | src/mum | 73 |
1 files changed, 48 insertions, 25 deletions
@@ -112,33 +112,21 @@ h: $b = $MESSAGE if not $b; $a = $b if not $a; - open my $mbox, '<', $MBOX or do { - warn "failed to open $MBOX: $!\n"; - next; - }; + print "$_\n" for get($a, $b); + } - for my $i ($a .. $b) { - $MESSAGES[$i-1] =~ /^M-Offset: (.*)$/m; - my $offset = $1; - $MESSAGES[$i-1] =~ /^M-Length: (.*)$/m; - my $length = $1; - - unless (defined $offset and defined $length) { - warn "ill-formatted message $i\n"; - next; - } - - seek $mbox, $offset, 0 or do { - warn "failed to retrieve message $i\n"; - next; - }; - - local $/ = \$length; - print scalar <$mbox>; - print "\n"; - } + # l + elsif (/^ (?&range)? l \Z $d/x) { + my ($a, $b); + $a = toloc(0, shift @RANGE) or next if @RANGE > 1; + $b = toloc($a, shift @RANGE) or next if @RANGE; + $b = $MESSAGE if not $b; + $a = $b if not $a; - close $mbox; + open my $pager, '|-', $ENV{PAGER} || 'less' + or die "failed to open pager: $!"; + print $pager "$_\n" for get($a, $b); + close $pager; } # | @@ -266,3 +254,38 @@ found: $loc = @MESSAGES if $loc > @MESSAGES; return $loc; } + +# Read messages from mbox +sub get { + my ($a, $b) = @_; # range to read + my @messages; # messages to return + + open my $mbox, '<', $MBOX or do { + warn "failed to open $MBOX: $!\n"; + next; + }; + + for my $i ($a .. $b) { + $MESSAGES[$i-1] =~ /^M-Offset: (.*)$/m; + my $offset = $1; + $MESSAGES[$i-1] =~ /^M-Length: (.*)$/m; + my $length = $1; + + unless (defined $offset and defined $length) { + warn "ill-formatted message $i\n"; + next; + } + + seek $mbox, $offset, 0 or do { + warn "failed to retrieve message $i\n"; + next; + }; + + local $/ = \$length; + push @messages, scalar <$mbox>; + } + + close $mbox; + + return @messages; +} |