Skip to content

feat: colored diff for fmt command #5652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ require (
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
github.com/GaijinEntertainment/go-exhaustruct/v3 v3.3.1
github.com/OpenPeeDeeP/depguard/v2 v2.2.1
github.com/alecthomas/chroma/v2 v2.15.0
github.com/alecthomas/go-check-sumtype v0.3.1
github.com/alexkohler/nakedret/v2 v2.0.6
github.com/alexkohler/prealloc v1.0.0
@@ -148,6 +149,7 @@ require (
github.com/chavacava/garif v0.1.0 // indirect
github.com/dave/dst v0.27.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/ebitengine/purego v0.8.2 // indirect
github.com/ettle/strcase v0.2.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
6 changes: 4 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions pkg/commands/fmt.go
Original file line number Diff line number Diff line change
@@ -20,8 +20,9 @@ import (
type fmtOptions struct {
config.LoaderOptions

diff bool // Flag only.
stdin bool // Flag only.
diff bool // Flag only.
diffColored bool // Flag only.
stdin bool // Flag only.
}

type fmtCommand struct {
@@ -70,6 +71,7 @@ func newFmtCommand(logger logutils.Log, info BuildInfo) *fmtCommand {
setupFormattersFlagSet(c.viper, fs)

fs.BoolVarP(&c.opts.diff, "diff", "d", false, color.GreenString("Display diffs instead of rewriting files"))
fs.BoolVar(&c.opts.diffColored, "diff-colored", false, color.GreenString("Display diffs instead of rewriting files (with colors)"))
fs.BoolVar(&c.opts.stdin, "stdin", false, color.GreenString("Use standard input for piping source files"))

c.cmd = fmtCmd
@@ -102,7 +104,7 @@ func (c *fmtCommand) preRunE(_ *cobra.Command, _ []string) error {

matcher := processors.NewGeneratedFileMatcher(c.cfg.Formatters.Exclusions.Generated)

opts, err := goformat.NewRunnerOptions(c.cfg, c.opts.diff, c.opts.stdin)
opts, err := goformat.NewRunnerOptions(c.cfg, c.opts.diff, c.opts.diffColored, c.opts.stdin)
if err != nil {
return fmt.Errorf("build walk options: %w", err)
}
23 changes: 18 additions & 5 deletions pkg/goformat/runner.go
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import (
"regexp"
"strings"

"github.com/alecthomas/chroma/v2/quick"
rpdiff "github.com/rogpeppe/go-internal/diff"

"github.com/golangci/golangci-lint/v2/pkg/config"
@@ -128,9 +129,19 @@ func (c *Runner) process(path string, stdout io.Writer, in io.Reader) error {
if c.opts.diff {
newName := filepath.ToSlash(path)
oldName := newName + ".orig"
_, err = stdout.Write(rpdiff.Diff(oldName, input, newName, output))
if err != nil {
return err

patch := rpdiff.Diff(oldName, input, newName, output)

if c.opts.colors {
err = quick.Highlight(stdout, string(patch), "diff", "terminal", "native")
if err != nil {
return err
}
} else {
_, err = stdout.Write(patch)
if err != nil {
return err
}
}

c.exitCode = 1
@@ -168,10 +179,11 @@ type RunnerOptions struct {
patterns []*regexp.Regexp
generated string
diff bool
colors bool
stdin bool
}

func NewRunnerOptions(cfg *config.Config, diff, stdin bool) (RunnerOptions, error) {
func NewRunnerOptions(cfg *config.Config, diff, diffColored, stdin bool) (RunnerOptions, error) {
basePath, err := fsutils.GetBasePath(context.Background(), cfg.Run.RelativePathMode, cfg.GetConfigDir())
if err != nil {
return RunnerOptions{}, fmt.Errorf("get base path: %w", err)
@@ -180,7 +192,8 @@ func NewRunnerOptions(cfg *config.Config, diff, stdin bool) (RunnerOptions, erro
opts := RunnerOptions{
basePath: basePath,
generated: cfg.Formatters.Exclusions.Generated,
diff: diff,
diff: diff || diffColored,
colors: diffColored,
stdin: stdin,
}