* fix(diff): add --no-pager and package header to git diff Previously, each package's diff was shown via a separate `git diff` invocation with Show(), allowing git to invoke its own pager (e.g. less) independently for each package. This caused inconsistent pagination: large diffs opened a pager, small diffs scrolled past in the terminal. Changes: - Add --no-pager flag to git diff so git doesn't open its own pager per package; all diffs now stream consistently to stdout. - Print a package name header before each diff so users can easily identify which package each diff belongs to when reviewing multiple packages. Closes #1243 * bunch up diffs and render in one go Co-authored-by: Jo Garnier <me@jguer.space>
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package menus
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"golang.org/x/term"
|
|
)
|
|
|
|
// runPager pages content through a single pager process. Overridable in tests.
|
|
var runPager = pageThroughPager
|
|
|
|
// isStdoutTerminal reports whether stdout is a terminal. Overridable in tests.
|
|
var isStdoutTerminal = func() bool {
|
|
return term.IsTerminal(int(os.Stdout.Fd()))
|
|
}
|
|
|
|
// resolvePager returns the pager command string.
|
|
// Priority: config pager → PAGER → less → cat.
|
|
func resolvePager(pagerConfig string) string {
|
|
if pagerConfig != "" {
|
|
return pagerConfig
|
|
}
|
|
|
|
if pager := os.Getenv("PAGER"); pager != "" {
|
|
return pager
|
|
}
|
|
|
|
if _, err := exec.LookPath("less"); err == nil {
|
|
return "less"
|
|
}
|
|
|
|
return "cat"
|
|
}
|
|
|
|
// pageThroughPager runs the configured pager with content on stdin.
|
|
// The pager command is user-controlled (config / $PAGER), same model as $EDITOR.
|
|
func pageThroughPager(ctx context.Context, content, pagerConfig string) error {
|
|
pager := resolvePager(pagerConfig)
|
|
cmd := exec.CommandContext(ctx, "sh", "-c", pager)
|
|
cmd.Stdin = strings.NewReader(content)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
cmd.Env = os.Environ()
|
|
|
|
if os.Getenv("LESS") == "" {
|
|
// S: chop long lines; R: raw ANSI; X: no termcap init; F: quit if one screen
|
|
cmd.Env = append(cmd.Env, "LESS=SRXF")
|
|
}
|
|
|
|
return cmd.Run()
|
|
}
|