From d7f7f59419c69580f22d7b74eb5d399412ad5483 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Sat, 16 Mar 2013 17:20:31 +0100 Subject: [PATCH] tig.pl: Spawn a pager. --- gitconfig.m4 | 2 +- tig.pl | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gitconfig.m4 b/gitconfig.m4 index 5b16dad..1977f2e 100644 --- a/gitconfig.m4 +++ b/gitconfig.m4 @@ -120,7 +120,7 @@ include(../lib.m4) # precise enough control over formats and colors. # # tig = log --pretty=oneline --graph --all --decorate --abbrev-commit - tig = ! TIG | less + tig = ! TIG # Create backup of uncommitted and untracked changes. ssb = "! git stash save --include-untracked \ diff --git a/tig.pl b/tig.pl index 3939deb..0d80e1f 100755 --- 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 $!; +} -- 2.44.1