summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-05-26 15:37:00 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-05-26 15:37:00 +0200
commit0fcae069b0f4c08a11d39eea8cd05e83ae8423fa (patch)
tree22e3ec727a34bb8046a9a2f51fa704735a45c01c
parentc90d68024af530aea52f899ed1a2362ca06768c9 (diff)
downloadmum-0fcae069b0f4c08a11d39eea8cd05e83ae8423fa.tar.gz
mum: Add 'l' command (view in pager)
-rwxr-xr-xsrc/mum73
1 files changed, 48 insertions, 25 deletions
diff --git a/src/mum b/src/mum
index 0d6eadb..3b8fdb7 100755
--- a/src/mum
+++ b/src/mum
@@ -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;
+}