]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - tig.pl
tig.pl: Spawn a pager.
[config/dotfiles.git] / tig.pl
diff --git a/tig.pl b/tig.pl
index 3939deb1f5f25bfed13b957fc3fcd43caa6eb2ac..0d80e1fcb56b55a0d7dc1141624458e746e11539 100755 (executable)
--- a/tig.pl
+++ b/tig.pl
@@ -43,6 +43,21 @@ my $format = '%x00'         # separator from --graph
 my @cmd = ('git', 'log', '--all', '--graph', "--format=$format");
 open my $fh, '-|', @cmd or die $!;
 
+my $pager = $ENV{PAGER};
+# Try to find an usable pager without searching $PATH.
+if (not defined $pager) {
+    foreach my $path (qw(/usr/bin/less /bin/less /usr/bin/more /bin/more)) {
+        next if not -x $path;
+
+        $pager = $path;
+        last;
+    }
+}
+# Use a pager if STDOUT is a terminal.
+if (-t STDOUT and defined $pager) {
+    open STDOUT, '|-', $pager or die $!;
+}
+
 while (my $line = <$fh>) {
     # History graph line.
     if ($line =~ m{^([|/\\_ ]+)$}) {
@@ -94,3 +109,9 @@ while (my $line = <$fh>) {
 }
 
 close $fh or die $!;
+
+# Necessary for the redirection to a pager or the pager terminates after our
+# script finishes without displaying all data.
+if (defined $pager) {
+    close STDOUT or die $!;
+}