chiark / gitweb /
Implement setup_pager just like in git
authorKirill Smelkov <kirr@landau.phys.spbu.ru>
Wed, 21 Jan 2009 20:18:42 +0000 (23:18 +0300)
committermartin f. krafft <madduck@debian.org>
Fri, 23 Jan 2009 04:59:36 +0000 (15:59 +1100)
setup_pager() spawns a pager process and redirect the rest of our output
to it.

This will be needed to fix `tg patch` output in the next commit.

Signed-off-by: Kirill Smelkov <kirr@landau.phys.spbu.ru>
tg.sh

diff --git a/tg.sh b/tg.sh
index 8c23d26..b64fc3a 100644 (file)
--- a/tg.sh
+++ b/tg.sh
@@ -243,6 +243,44 @@ do_help()
        fi
 }
 
+## Pager stuff
+
+# isatty FD
+isatty()
+{
+       test -t $1
+}
+
+# setup_pager
+# Spawn pager process and redirect the rest of our output to it
+setup_pager()
+{
+       isatty 1 || return 0
+
+       # TG_PAGER = GIT_PAGER | PAGER | less
+       # NOTE: GIT_PAGER='' is significant
+       TG_PAGER=${GIT_PAGER-${PAGER-less}}
+
+       [ -z "$TG_PAGER"  -o  "$TG_PAGER" = "cat" ]  && return 0
+
+
+       # now spawn pager
+       export LESS=${LESS:-FRSX}       # as in pager.c:pager_preexec()
+
+       _pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
+       _pager_fifo="$_pager_fifo_dir/0"
+       mkfifo -m 600 "$_pager_fifo"
+
+       "$TG_PAGER" < "$_pager_fifo" &
+       exec > "$_pager_fifo"           # dup2(pager_fifo.in, 1)
+
+       # this is needed so e.g. `git diff` will still colorize it's output if
+       # requested in ~/.gitconfig with color.diff=auto
+       export GIT_PAGER_IN_USE=1
+
+       # atexit(close(1); wait pager)
+       trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
+}
 
 ## Startup