From 54241d8ab9cc36abd084ec5124b73869d4ad6143 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 1 Mar 2020 21:00:44 +1100 Subject: more generic way of supporting custom pagers --- pkg/commands/git.go | 47 +++++++++++++++++++++++++++++------------------ pkg/config/app_config.go | 3 +++ pkg/gui/tasks_adapter.go | 5 +---- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index e41648ed3..37a5525ea 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -7,6 +7,7 @@ import ( "os/exec" "path/filepath" "regexp" + "strconv" "strings" "time" @@ -1129,26 +1130,36 @@ func (c *GitCommand) GetReflogCommits() ([]*Commit, error) { return commits, nil } -func (c *GitCommand) GetPager(width int) (string, error) { - pager := c.Config.GetUserConfig().GetString("git.pager") - switch pager { - case "": - return "", nil - case "diff-so-fancy": - return "diff-so-fancy", nil - case "ydiff": - return fmt.Sprintf("ydiff -s --wrap --width=%d", width/2-6), nil - case "delta": - return "delta --dark", nil - default: - return "", errors.New("pager not supported. Pick one of diff-so-fancy, ydiff, delta, or nothing") +func (c *GitCommand) ConfiguredPager() string { + if os.Getenv("GIT_PAGER") != "" { + return os.Getenv("GIT_PAGER") } + if os.Getenv("PAGER") != "" { + return os.Getenv("PAGER") + } + output, err := c.OSCommand.RunCommandWithOutput("git config --get-all core.pager") + if err != nil { + return "" + } + trimmedOutput := strings.TrimSpace(output) + return strings.Split(trimmedOutput, "\n")[0] } -func (c *GitCommand) colorArg() string { - pager := c.Config.GetUserConfig().GetString("git.pager") - if pager == "diff-so-fancy" || pager == "" { - return "always" +func (c *GitCommand) GetPager(width int) string { + useConfig := c.Config.GetUserConfig().GetBool("git.paging.useConfig") + if useConfig { + pager := c.ConfiguredPager() + return strings.Split(pager, "| less")[0] + } + + templateValues := map[string]string{ + "columnWidth": strconv.Itoa(width/2 - 6), } - return "never" + + pagerTemplate := c.Config.GetUserConfig().GetString("git.paging.pager") + return utils.ResolvePlaceholderString(pagerTemplate, templateValues) +} + +func (c *GitCommand) colorArg() string { + return c.Config.GetUserConfig().GetString("git.paging.colorArg") } diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 1adedc780..2104aa45e 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -258,6 +258,9 @@ func GetDefaultConfig() []byte { commitLength: show: true git: + paging: + colorArg: always + useConfig: false merging: manualCommit: false skipHookPrefix: 'WIP' diff --git a/pkg/gui/tasks_adapter.go b/pkg/gui/tasks_adapter.go index 38d7ce36b..fc2b0a3bf 100644 --- a/pkg/gui/tasks_adapter.go +++ b/pkg/gui/tasks_adapter.go @@ -28,10 +28,7 @@ func (gui *Gui) newCmdTask(viewName string, cmd *exec.Cmd) error { func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd) error { width, _ := gui.getMainView().Size() - pager, err := gui.GitCommand.GetPager(width) - if err != nil { - return err - } + pager := gui.GitCommand.GetPager(width) if pager == "" { // if we're not using a custom pager we don't need to use a pty -- cgit v1.2.3