From b4dc9c9f75eab8321d49dcb01fab82090f6138af Mon Sep 17 00:00:00 2001 From: Rob Muhlestein Date: Sat, 26 Nov 2022 16:28:11 -0500 Subject: [PATCH] Add FixPagerEnv --- z/page.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/z/page.go b/z/page.go index 74e41ea..770eaaa 100644 --- a/z/page.go +++ b/z/page.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/exec" + "strings" "github.com/rwxrob/to" ) @@ -11,6 +12,25 @@ import ( // NoPager disables all paging. var NoPager bool +// FixPagerEnv sets environment variables for +// different pagers to get them to support color ANSI escapes. FRX is +// added to LESS and LV is set to -c. (These are the same fixes used by +// the git diff command.) +func FixPagerEnv() { + less := os.Getenv(`LESS`) + if strings.Index(less, `R`) < 0 { + less += `R` + } + if strings.Index(less, `F`) < 0 { + less += `F` + } + if strings.Index(less, `X`) < 0 { + less += `X` + } + os.Setenv(`LESS`, less) + os.Setenv(`LV`, `-c`) +} + // FindPager returns a full path to a pager binary if it can find one on // the system: // @@ -22,6 +42,7 @@ func FindPager() string { if NoPager { return "" } + FixPagerEnv() path := os.Getenv(`PAGER`) if path == "" { path, _ = exec.LookPath(path)