Skip to content

Commit

Permalink
cmd/shfmt: swap out pkg/diff for internal/diff
Browse files Browse the repository at this point in the history
This means one less module dependency, which is a small win.
internal/diff does not handle colors, but it is twenty lines to do,
so it's not worth adding or keeping a dependency just for that.
  • Loading branch information
mvdan committed Apr 27, 2024
1 parent 7394d55 commit 95901a8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
41 changes: 34 additions & 7 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
"strings"

maybeio "github.com/google/renameio/v2/maybe"
diffpkg "github.com/pkg/diff"
diffwrite "github.com/pkg/diff/write"
diffpkg "github.com/rogpeppe/go-internal/diff"
"golang.org/x/term"
"mvdan.cc/editorconfig"

Expand Down Expand Up @@ -504,12 +503,32 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
}
}
if diff.val {
opts := []diffwrite.Option{}
if color {
opts = append(opts, diffwrite.TerminalColor())
diffBytes := diffpkg.Diff(path+".orig", src, path, res)
if !color {
os.Stdout.Write(diffBytes)
return errChangedWithDiff
}
if err := diffpkg.Text(path+".orig", path, src, res, os.Stdout, opts...); err != nil {
return fmt.Errorf("computing diff: %s", err)
// The first three lines are the header with the filenames, including --- and +++,
// and are marked in bold.
current := terminalBold
os.Stdout.WriteString(current)
for i, line := range bytes.SplitAfter(diffBytes, []byte("\n")) {
last := current
switch {
case i < 3: // the first three lines are bold
case bytes.HasPrefix(line, []byte("@@")):
current = terminalCyan
case bytes.HasPrefix(line, []byte("-")):
current = terminalRed
case bytes.HasPrefix(line, []byte("+")):
current = terminalGreen
default:
current = terminalReset
}
if current != last {
os.Stdout.WriteString(current)
}
os.Stdout.Write(line)
}
return errChangedWithDiff
}
Expand All @@ -519,3 +538,11 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
}
return nil
}

const (
terminalGreen = "\u001b[32m"
terminalRed = "\u001b[31m"
terminalCyan = "\u001b[36m"
terminalReset = "\u001b[0m"
terminalBold = "\u001b[1m"
)
8 changes: 5 additions & 3 deletions cmd/shfmt/testdata/script/diff.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cmp stdout input.sh.filediff
! stderr .

! exec shfmt -d input.sh input.sh
stdout -count=2 'input.sh.orig'
stdout -count=4 'input.sh.orig'

env FORCE_COLOR=true
stdin input.sh
Expand All @@ -36,20 +36,22 @@ foo

bar
-- input.sh.stdindiff --
diff <standard input>.orig <standard input>
--- <standard input>.orig
+++ <standard input>
@@ -1,4 +1,3 @@
- foo
-
+foo

-
bar
-- input.sh.filediff --
diff input.sh.orig input.sh
--- input.sh.orig
+++ input.sh
@@ -1,4 +1,3 @@
- foo
-
+foo

-
bar
6 changes: 3 additions & 3 deletions cmd/shfmt/testdata/script/editorconfig.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ stdout -count=1 'input\.sh$'
stdout -count=1 'ignored\.sh$'
stderr -count=1 'ignored\.sh.* must be followed by'
! exec shfmt -d input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh
stdout -count=1 'input\.sh$'
stdout -count=1 'ignored\.sh$'
stdout -count=2 'input\.sh$'
stdout -count=2 'ignored\.sh$'
stderr -count=1 'ignored\.sh.* must be followed by'
! exec shfmt input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh
stdout -count=1 'indented'
Expand All @@ -83,7 +83,7 @@ stdout -count=1 'input\.sh$'
! stdout 'ignored\.sh'
! stderr .
! exec shfmt --apply-ignore -d input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh
stdout -count=1 'input\.sh$'
stdout -count=2 'input\.sh$'
! stdout 'ignored\.sh'
! stderr .
exec shfmt --apply-ignore input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/renameio/v2 v2.0.0
github.com/muesli/cancelreader v0.2.2
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/rogpeppe/go-internal v1.12.0
golang.org/x/sync v0.7.0
golang.org/x/sys v0.19.0
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
Expand Down

0 comments on commit 95901a8

Please sign in to comment.