Skip to content

Commit

Permalink
Improve error messages length output in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
foteinigk committed Aug 7, 2024
1 parent 6d1a851 commit f5c4ab9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ require (
github.com/gosuri/uilive v0.0.4
github.com/manifoldco/promptui v0.8.0
github.com/mattn/go-isatty v0.0.14
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/urfave/cli/v2 v2.3.0
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/term v0.23.0 // indirect
gopkg.in/ini.v1 v1.62.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
Expand All @@ -102,6 +104,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rhysd/go-github-selfupdate v1.2.3 h1:iaa+J202f+Nc+A8zi75uccC8Wg3omaM7HDeimXA22Ag=
github.com/rhysd/go-github-selfupdate v1.2.3/go.mod h1:mp/N8zj6jFfBQy/XMYoWsmfzxazpPAODuqarmPDe2Rg=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
Expand Down Expand Up @@ -158,10 +162,14 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
Expand Down
24 changes: 21 additions & 3 deletions internal/txlib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (

"github.com/gosimple/slug"
"github.com/mattn/go-isatty"
"github.com/mattn/go-runewidth"
"github.com/transifex/cli/internal/txlib/config"
"github.com/transifex/cli/pkg/jsonapi"
"golang.org/x/term"

Check failure on line 16 in internal/txlib/utils.go

View workflow job for this annotation

GitHub Actions / test (1.16.x, ubuntu-latest)

//go:build comment without // +build comment
)

func figureOutBranch(branch string) string {
Expand Down Expand Up @@ -209,10 +211,26 @@ func isValidResolutionPolicy(policy string) (IsValid bool) {

}

// Define a type for the function signature
type getSizeFuncType func(fd int) (int, int, error)

// Package-level variable holding the function to get terminal size
var getSizeFunc getSizeFuncType = term.GetSize

func truncateMessage(message string) string {
maxLength := 80
if len(message) > maxLength {
return message[:maxLength-2] + ".."
width, _, err := getSizeFunc(int(os.Stdout.Fd()))
if err != nil {
width = 80
}

maxLength := width - 2
if maxLength < 0 {
maxLength = 0
}

if runewidth.StringWidth(message) > maxLength {
truncated := runewidth.Truncate(message, maxLength, "..")
return truncated
}
return message
}
29 changes: 12 additions & 17 deletions internal/txlib/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,40 +67,35 @@ func TestFigureOutResources(t *testing.T) {
}
}

func TestConflictResolution(t *testing.T) {
ResultHead := isValidResolutionPolicy("USE_HEAD")
assert.Equal(t, ResultHead, true)

ResultBase := isValidResolutionPolicy("USE_BASE")
assert.Equal(t, ResultBase, true)

WrongResult := isValidResolutionPolicy("WRONG_BASE")
if WrongResult == true {
t.Error("Should be error")
}

func mockGetSize(fd int) (int, int, error) {
return 80, 0, nil
}

func TestTruncateMessage(t *testing.T) {
// Backup the original function
originalGetSizeFunc := getSizeFunc
defer func() { getSizeFunc = originalGetSizeFunc }()

// Test with 80 character terminal width
getSizeFunc = mockGetSize
result := truncateMessage("short message")
assert.Equal(t, result, "short message")
assert.Equal(t, "short message", result)

result = truncateMessage(
"this is a long message that needs to be truncated because it exceeds " +
"the maximum length of 75 characters",
"this is a long message that needs to be truncated because it exceeds the maximum length of 75 characters",
)
assert.Equal(
t,
"this is a long message that needs to be truncated because it exceeds the max..",
result,
"this is a long message that needs to be truncated because it exceeds the maxim..",
)

result = truncateMessage(
"a message with exactly 75 characters - this message should not be truncated",
)
assert.Equal(
t,
result,
"a message with exactly 75 characters - this message should not be truncated",
result,
)
}

0 comments on commit f5c4ab9

Please sign in to comment.