1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-31 22:22:14 +02:00

more generic way of supporting custom pagers

This commit is contained in:
Jesse Duffield 2020-03-01 21:00:44 +11:00
parent 355f1615ab
commit 54241d8ab9
3 changed files with 33 additions and 22 deletions

View File

@ -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) 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),
}
pagerTemplate := c.Config.GetUserConfig().GetString("git.paging.pager")
return utils.ResolvePlaceholderString(pagerTemplate, templateValues)
}
func (c *GitCommand) colorArg() string {
pager := c.Config.GetUserConfig().GetString("git.pager")
if pager == "diff-so-fancy" || pager == "" {
return "always"
}
return "never"
return c.Config.GetUserConfig().GetString("git.paging.colorArg")
}

View File

@ -258,6 +258,9 @@ func GetDefaultConfig() []byte {
commitLength:
show: true
git:
paging:
colorArg: always
useConfig: false
merging:
manualCommit: false
skipHookPrefix: 'WIP'

View File

@ -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