Skip to content

Refactor stringutils and fix docker search output form#621

Merged
thaJeztah merged 1 commit intodocker:masterfrom
ChiuWang:displaystringutils
Oct 27, 2017
Merged

Refactor stringutils and fix docker search output form#621
thaJeztah merged 1 commit intodocker:masterfrom
ChiuWang:displaystringutils

Conversation

@ChiuWang
Copy link
Contributor

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
search result
- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I think this looks good, but wondering if we still need the Truncate.

for _, r := range rs {
cw := charWidth(r)
displayWidth += cw
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 s

If 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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(...) }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified. Sorry for the delay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? (☉㊀☉)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charrywanganthony

  • assert.Equal() can't lie. It will always print the real value for the two values being compared. If you write if x != y { t.FatalF(msg) } the msg could be incomplete, or misleading, or even completely wrong, which makes debugging the failure more difficult. You can always provide extra msgAndArgs to assert.Equal() as the 4+ argument.
  • assert.Equal() is less verbose, so it keeps test smaller and easier to read
  • assert.Equal() reads in the affirmative, where as conditions for if x != y ... are always the inverse. I want x == y, but I have to write x != y.

These points may not be relevant in all cases, but it's easier to be consistent and just use it everywhere.

@dnephin
Copy link
Contributor

dnephin commented Oct 13, 2017

For the lint failure (should range over string, not []rune(string) (S1029) (gosimple)) I think this is one of the very rare cases where []rune(string) is actually necessary, since we (may) need to index into the string. You may need to add // nolint: gosimple above the line

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we need some more information here what "width" is, but I don't have a good suggestion at this moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified. Sorry for the delay.

@ChiuWang ChiuWang force-pushed the displaystringutils branch 2 times, most recently from b39a0ea to bb4e525 Compare October 27, 2017 03:05
@codecov-io
Copy link

codecov-io commented Oct 27, 2017

Codecov Report

Merging #621 into master will decrease coverage by 0.19%.
The diff coverage is 95.23%.

@@            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

Copy link
Collaborator

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🐸

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

`
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ nice!

@thaJeztah thaJeztah merged commit e4940cb into docker:master Oct 27, 2017
@thaJeztah thaJeztah added this to the 17.11.0 milestone Oct 27, 2017
@ChiuWang ChiuWang deleted the displaystringutils branch October 30, 2017 01:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants