add |more support to list+show commands

This commit is contained in:
Thomas von Dein
2017-04-05 23:30:41 +02:00
parent c82afe44ea
commit da13bf31d5

View File

@@ -21,7 +21,7 @@ no strict 'refs';
use Data::Dumper; use Data::Dumper;
$Data::Interactive::Inspect::VERSION = 0.06; $Data::Interactive::Inspect::VERSION = 0.07;
use vars qw(@ISA); use vars qw(@ISA);
@@ -330,6 +330,17 @@ sub _failidx {
return $self->_fail("<key> must be a number, since we're inside an array\n"); return $self->_fail("<key> must be a number, since we're inside an array\n");
} }
sub _more {
my ($self, $out) = @_;
if (open LESS, "|$self->{more}") {
print LESS $out;
close LESS;
}
else {
print $out;
}
}
sub quit { sub quit {
my $self = shift; my $self = shift;
$self->{quit} = 1; $self->{quit} = 1;
@@ -526,13 +537,7 @@ sub dump {
return $out; return $out;
} }
else { else {
if (open LESS, "|$self->{more}") { $self->_more($out);
print LESS $out;
close LESS;
}
else {
print $out;
}
} }
return 1; return 1;
@@ -586,8 +591,8 @@ sub list {
$self->show; $self->show;
} }
else { else {
print join "\n", sort keys %{$self->{db}}; my $out = join("\n", sort keys %{$self->{db}}) . "\n";
print "\n"; $self->_more($out);
} }
return 1; return 1;
@@ -595,24 +600,27 @@ sub list {
sub show { sub show {
my ($self, $indent) = @_; my ($self, $indent) = @_;
my $out;
if (ref($self->{db}) =~ /array/i) { if (ref($self->{db}) =~ /array/i) {
my $pos = 0; my $pos = 0;
foreach my $item (@{$self->{db}}) { foreach my $item (@{$self->{db}}) {
print "$pos:\n"; $out .= "$pos:\n";
if (ref($item)) { if (ref($item)) {
$self->_showhash($item, " "); $out .= $self->_showhash($item, " ");
} }
else { else {
print " $item\n"; $out .= " $item\n";
} }
$pos++; $pos++;
} }
} }
else { else {
$self->_showhash($self->{db}); $out = $self->_showhash($self->{db});
} }
$self->_more($out);
return 1; return 1;
} }
@@ -623,18 +631,21 @@ sub _showhash {
$indent = ''; $indent = '';
} }
my $out;
foreach my $key (sort keys %{$db}) { foreach my $key (sort keys %{$db}) {
printf "%s%-30s", $indent, $key; $out .= sprintf "%s%-30s", $indent, $key;
if (ref($db->{$key}) =~ /hash/i) { if (ref($db->{$key}) =~ /hash/i) {
print "{ .. }\n"; $out .= "{ .. }\n";
} }
elsif (ref($db->{$key}) =~ /array/i) { elsif (ref($db->{$key}) =~ /array/i) {
print "[ .. ]\n"; $out .= "[ .. ]\n";
} }
else { else {
print "\"$db->{$key}\"\n"; $out .= "\"$db->{$key}\"\n";
} }
} }
return $out;
} }
sub enter { sub enter {
@@ -1148,6 +1159,6 @@ and/or modify it under the same terms as Perl itself.
=head1 VERSION =head1 VERSION
This is the manual page for L<Data::Interactive::Inspect> Version 0.06. This is the manual page for L<Data::Interactive::Inspect> Version 0.07.
=cut =cut