diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-03-01 21:00:44 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-03-04 00:12:23 +1100 |
commit | 54241d8ab9cc36abd084ec5124b73869d4ad6143 (patch) | |
tree | 3b6e56f6a0529077e8229b92902979d36c54f9ae /pkg/commands | |
parent | 355f1615aba5b0b75485596fee0ae93d054081d4 (diff) |
more generic way of supporting custom pagers
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git.go | 47 |
1 files changed, 29 insertions, 18 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") } |