+# caller needs to "-t $self->{1}" to check if tty
+sub start_pager {
+ my ($self) = @_;
+ my $env = $self->{env};
+ my $fh = popen_rd([qw(git var GIT_PAGER)], $env);
+ chomp(my $pager = <$fh> // '');
+ close($fh) or warn "`git var PAGER' error: \$?=$?";
+ return if $pager eq 'cat' || $pager eq '';
+ $env->{LESS} //= 'FRX';
+ $env->{LV} //= '-c';
+ $env->{COLUMNS} //= 80; # TODO TIOCGWINSZ
+ $env->{MORE} //= 'FRX' if $^O eq 'freebsd';
+ pipe(my ($r, $w)) or return warn "pipe: $!";
+ my $rdr = { 0 => $r, 1 => $self->{1}, 2 => $self->{2} };
+ $self->{1} = $w;
+ $self->{2} = $w if -t $self->{2};
+ $self->{'pager.pid'} = spawn([$pager], $env, $rdr);
+ $env->{GIT_PAGER_IN_USE} = 'true'; # we may spawn git
+}
+