Skip to content

Commit ca77c86

Browse files
authored
Merge pull request #236 from transifex/error_length
Improve error messages length output in CLI
2 parents 6d1a851 + 1930223 commit ca77c86

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

internal/txlib/utils.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/mattn/go-isatty"
1313
"github.com/transifex/cli/internal/txlib/config"
1414
"github.com/transifex/cli/pkg/jsonapi"
15+
"golang.org/x/term"
1516
)
1617

1718
func figureOutBranch(branch string) string {
@@ -209,8 +210,21 @@ func isValidResolutionPolicy(policy string) (IsValid bool) {
209210

210211
}
211212

213+
type getSizeFuncType func(fd int) (int, int, error)
214+
215+
var getSizeFunc getSizeFuncType = term.GetSize
216+
212217
func truncateMessage(message string) string {
213-
maxLength := 80
218+
width, _, err := getSizeFunc(int(os.Stdout.Fd()))
219+
if err != nil {
220+
width = 80
221+
}
222+
223+
maxLength := width - 2
224+
if maxLength < 0 {
225+
maxLength = 0
226+
}
227+
214228
if len(message) > maxLength {
215229
return message[:maxLength-2] + ".."
216230
}

internal/txlib/utils_test.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,40 +67,35 @@ func TestFigureOutResources(t *testing.T) {
6767
}
6868
}
6969

70-
func TestConflictResolution(t *testing.T) {
71-
ResultHead := isValidResolutionPolicy("USE_HEAD")
72-
assert.Equal(t, ResultHead, true)
73-
74-
ResultBase := isValidResolutionPolicy("USE_BASE")
75-
assert.Equal(t, ResultBase, true)
76-
77-
WrongResult := isValidResolutionPolicy("WRONG_BASE")
78-
if WrongResult == true {
79-
t.Error("Should be error")
80-
}
81-
70+
func mockGetSize(fd int) (int, int, error) {
71+
return 80, 0, nil
8272
}
8373

8474
func TestTruncateMessage(t *testing.T) {
75+
// Backup the original function
76+
originalGetSizeFunc := getSizeFunc
77+
defer func() { getSizeFunc = originalGetSizeFunc }()
78+
79+
// Test with 80 character terminal width
80+
getSizeFunc = mockGetSize
8581
result := truncateMessage("short message")
86-
assert.Equal(t, result, "short message")
82+
assert.Equal(t, "short message", result)
8783

8884
result = truncateMessage(
89-
"this is a long message that needs to be truncated because it exceeds " +
90-
"the maximum length of 75 characters",
85+
"this is a long message that needs to be truncated because it exceeds the maximum length of 75 characters",
9186
)
9287
assert.Equal(
9388
t,
89+
"this is a long message that needs to be truncated because it exceeds the max..",
9490
result,
95-
"this is a long message that needs to be truncated because it exceeds the maxim..",
9691
)
9792

9893
result = truncateMessage(
9994
"a message with exactly 75 characters - this message should not be truncated",
10095
)
10196
assert.Equal(
10297
t,
103-
result,
10498
"a message with exactly 75 characters - this message should not be truncated",
99+
result,
105100
)
106101
}

0 commit comments

Comments
 (0)