Refactor stringutils and fix docker search output form#621
Refactor stringutils and fix docker search output form#621thaJeztah merged 1 commit intodocker:masterfrom
Conversation
ab7c166 to
5573edc
Compare
dnephin
left a comment
There was a problem hiding this comment.
Thanks!
I think this looks good, but wondering if we still need the Truncate.
| for _, r := range rs { | ||
| cw := charWidth(r) | ||
| displayWidth += cw | ||
| } |
There was a problem hiding this comment.
It seems like we know at this point if the displayWidth is greater than or equal to the maxWidth. Could we actually return here instead of calling TruncateByWidth() ?
for index, r := range rs {
width := charWidth(r)
switch {
case displayWidth + width == maxWidth && index + 1 == len(rs):
return s
case displayWidth + width >= maxWidth:
return string(rs[:index]) + "…"
}
displayWidth += width
}
return sIf this works, then I think we can simplify the maxwidth == 1 case as well and we could remove TruncateByWidth() entirely.
| newstr := Ellipsis(str, 0) | ||
| if newstr != "" { | ||
| t.Fatalf("Expected null, got %s", newstr) | ||
| } |
There was a problem hiding this comment.
These tests would be great as a test table.
var testcases = []struct{
source string
width int
expected string
} {
{source: "t🐳ststring", width: 0},
...
}
for _, testcase := range testcases {
assert.Equal(t, testcase.expected, Ellipsis(testcase.source, testcase.width))
}Also please use assert.Equal() instead of if x != y { t.Fatalf(...) }
There was a problem hiding this comment.
Modified. Sorry for the delay.
There was a problem hiding this comment.
Although the pr is merged, if u are free @dnephin , I have a low level quetion why should we use assert.Equal() instead of if x != y { t.Fatalf(...) }? What's the disadvantages of t.Fatalf? (☉㊀☉)
There was a problem hiding this comment.
@charrywanganthony
assert.Equal()can't lie. It will always print the real value for the two values being compared. If you writeif x != y { t.FatalF(msg) }themsgcould be incomplete, or misleading, or even completely wrong, which makes debugging the failure more difficult. You can always provide extramsgAndArgstoassert.Equal()as the 4+ argument.assert.Equal()is less verbose, so it keeps test smaller and easier to readassert.Equal()reads in the affirmative, where as conditions forif x != y ...are always the inverse. I wantx == y, but I have to writex != y.
These points may not be relevant in all cases, but it's easier to be consistent and just use it everywhere.
|
For the lint failure (should range over string, not []rune(string) (S1029) (gosimple)) I think this is one of the very rare cases where |
thaJeztah
left a comment
There was a problem hiding this comment.
Thanks @charrywanganthony looks great already!
| } | ||
|
|
||
| // TruncateByWidth truncates a string to fit within maxwidth. | ||
| // For maxwidth of 1, first char of string will return even if its width > 1. |
There was a problem hiding this comment.
Perhaps we need some more information here what "width" is, but I don't have a good suggestion at this moment.
There was a problem hiding this comment.
Modified. Sorry for the delay.
b39a0ea to
bb4e525
Compare
…iption has CJK character Signed-off-by: Chao Wang <[email protected]>
bb4e525 to
926b20f
Compare
Codecov Report
@@ Coverage Diff @@
## master #621 +/- ##
=========================================
- Coverage 49.69% 49.49% -0.2%
=========================================
Files 209 209
Lines 17258 17229 -29
=========================================
- Hits 8576 8528 -48
- Misses 8256 8268 +12
- Partials 426 433 +7 |
| ` | ||
| expectedTrunc := `IMAGE CREATED CREATED BY SIZE COMMENT | ||
| imageID1 24 hours ago /bin/bash ls && npm i && npm run test && k... 183MB Hi | ||
| imageID1 24 hours ago /bin/bash ls && npm i && npm run test && kar… 183MB Hi |
when the description has CJK character
Signed-off-by: Chao Wang [email protected]
- What I did

Accoring to @thaJeztah 's suggestion, I move this work from moby to docker/cli moby/pull35034
- How I did it
East Asian character always takes two position of a English charactor. Technical Reports about width of East Asian text I use the unicode library https://godoc.org/golang.org/x/text/width
- How to verify it
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)