Skip to content

Comments

Fix docker search output form when the description has CJK character#35034

Closed
ChiuWang wants to merge 1 commit intomoby:masterfrom
ChiuWang:ellipsis
Closed

Fix docker search output form when the description has CJK character#35034
ChiuWang wants to merge 1 commit intomoby:masterfrom
ChiuWang:ellipsis

Conversation

@ChiuWang
Copy link
Contributor

- What I did
fixes docker search output form when the description has CJK character
- How I did it
Every Chinese or Japanese character takes two position of a English charactor,
But technically many other languages or special symbol should be checked. But I didn't
find a search result case.
- How to verify it
search result
- Description for the changelog

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

@GordonTheTurtle
Copy link

Please sign your commits following these rules:
https://github.com/moby/moby/blob/master/CONTRIBUTING.md#sign-your-work
The easiest way to do this is to amend the last commit:

$ git clone -b "ellipsis" [email protected]:charrywanganthony/moby.git somewhere
$ cd somewhere
$ git commit --amend -s --no-edit
$ git push -f

Amending updates the existing PR. You DO NOT need to open a new one.

@GordonTheTurtle GordonTheTurtle added dco/no Automatically set by a bot when one of the commits lacks proper signature status/0-triage labels Sep 29, 2017
@GordonTheTurtle GordonTheTurtle removed the dco/no Automatically set by a bot when one of the commits lacks proper signature label Sep 29, 2017
@AkihiroSuda
Copy link
Member

SGTM, anyone can verify current behavior with other writing systems such as Arabic/Hebrew or Mongolian?

@ChiuWang
Copy link
Contributor Author

ChiuWang commented Sep 29, 2017

As far as i know,any system with usual Chinese(Japanese) fonts will display the character by East_Asian_Width=Wide unicode.org. Because it's a verification for description so far, i set a general check range//UTF-8 Kana, CJK, Hangul (from 3000 to DFFF).
If it proves valuable, i could make it more accrurate and support more languages later. @AkihiroSuda

@thaJeztah
Copy link
Member

ping @ripcurld0 you may know some images with other character sets ^^

@thaJeztah
Copy link
Member

@charrywanganthony looks like you need to gofmt;

08:23:58 pkg/stringutils/stringutils.go:1::warning: file is not gofmted with -s (gofmt)
08:23:58 pkg/stringutils/stringutils_test.go:1::warning: file is not gofmted with -s (gofmt)
08:23:58 pkg/stringutils/stringutils.go:58:4:warning: should replace displayLen += 1 with displayLen++ (golint)
08:23:58 pkg/stringutils/stringutils.go:66:10:warning: should omit 2nd value from range; this loop is equivalent to `for i := range ...` (golint)
08:23:58 pkg/stringutils/stringutils.go:72:9:warning: should omit 2nd value from range; this loop is equivalent to `for i := range ...` (golint)
08:23:58 pkg/stringutils/stringutils.go:1::warning: file is not goimported (goimports)
08:23:58 pkg/stringutils/stringutils_test.go:1::warning: file is not goimported (goimports)

@thaJeztah thaJeztah added status/2-code-review status/failing-ci Indicates that the PR in its current state fails the test suite and removed status/0-triage labels Sep 29, 2017
@AkihiroSuda
Copy link
Member

https://en.m.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms contains some full-width symbols, half-width Katakana and half-width Hangul Jamo

@boaz0
Copy link
Contributor

boaz0 commented Oct 1, 2017

@thaJeztah the current code supports Hebrew characters pretty good. No need for code change. However, Hebrew text is printed in reverse.

$ docker search ripcurld00dx/boaz

@ChiuWang
Copy link
Contributor Author

ChiuWang commented Oct 9, 2017

I update the commit @thaJeztah , and set a more precise range for @AkihiroSuda . :)

@AkihiroSuda
Copy link
Member

@ChiuWang
Copy link
Contributor Author

ChiuWang commented Oct 9, 2017

Thx, this lib looks right for us. I'll check it soon.

@ChiuWang
Copy link
Contributor Author

I did the change but met a problem.

03:17:03 The result of vndr differs

03:17:03  D vendor/golang.org/x/text/width/gen.go
03:17:03  D vendor/golang.org/x/text/width/gen_common.go
03:17:03  D vendor/golang.org/x/text/width/gen_trieval.go
03:17:03  M vendor/golang.org/x/text/width/kind_string.go
03:17:03  M vendor/golang.org/x/text/width/tables.go
03:17:03  M vendor/golang.org/x/text/width/trieval.go

03:17:03 Please vendor your package with github.com/LK4D4/vndr.

When i use vndr i will get the information
unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: Service Unavailable) because of Chinese net can't reach golang.org.

How can I change vendor.conf manually to avoid the error

@AkihiroSuda
Copy link
Member

AkihiroSuda commented Oct 10, 2017

Please just put golang.org/x/text/width/gen.go <commit hash> to vendor.conf

Please just put golang.org/x/text <commit hash> to vendor.conf

EDIT: fix copy-paste error

Copy link
Member

Choose a reason for hiding this comment

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

Please avoid using magic numbers

@ChiuWang ChiuWang force-pushed the ellipsis branch 2 times, most recently from 6bad739 to 640bf2f Compare October 10, 2017 08:03
Copy link
Member

@AkihiroSuda AkihiroSuda Oct 10, 2017

Choose a reason for hiding this comment

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

These are already defined as width.EastAsianAmbiguous

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx for your help, I correct this, but the vendor problem still exists. @_@

@ChiuWang ChiuWang force-pushed the ellipsis branch 3 times, most recently from 2c48d5e to a16d694 Compare October 10, 2017 08:26
vendor.conf Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Please remove *.go lines and just update commit hash of golang.org/x/text

Copy link
Member

Choose a reason for hiding this comment

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

golang.org/x/text/width/gen.go

This was just my copy-paste error. I meant golang.org/x/text <commit hash>.
Sorry for causing confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

problem still exists. @-@ Do I need to update the whole text package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Vendor check passed after I update text package. :)

@AkihiroSuda
Copy link
Member

看起来不错,除了 vendor.conf之外 (LGTM except vendor.conf)

cc @yongtang @coolljt0725 请看一下 (PTAL) ?

Copy link
Member

Choose a reason for hiding this comment

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

Is this expected? I don't think the whale emoji is a wide character?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The character do takes two positions of a narrow one. And the test will pass only in this way, after we adopt https://godoc.org/golang.org/x/text/width

Copy link
Member

Choose a reason for hiding this comment

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

Ah! Yes, you're right, I thought it only took a single width, but I see now.

Sorry for the noise 😊

@AkihiroSuda AkihiroSuda added rebuild/powerpc and removed status/failing-ci Indicates that the PR in its current state fails the test suite labels Oct 11, 2017
@thaJeztah
Copy link
Member

WindowsRS1 failure is a known issue, and not related to the changes in this PR; #32219

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.

My concern with this PR is that it changes the behavior of the Ellipsis(), but not Truncate(). Therefore, these functions act different, which is very inconsistent.

For example, the following result in "t🐳s":

stringutils.Truncate("t🐳s", 3)

But using ellipsis results in "t🐳":

stringutils.Ellipsis("t🐳s", 3)

Looking at this package, I think it has become a collection of not-so-related functionality, and its scope is becoming more vague with this change. Let me try to explain that;
Ellipsis() is clearly targeted at displaying strings. Truncate() on the other hand is more ambigious; when used for displaying perhaps the string should be truncated at display width, not number of characters. However, because the purpose of the package is not well defined, it can be used in other contexts, and those can be problematic (e.g. limit a string's length to a maximum number of characters for storage).

I see this package has also been mentioned in #32989 (see @dnephin's gist), and based on the above, I think we should "dismantle" this package, and move code to places it will be used.

Having a look where things are used, and what we can do:

Function Where it's used What to do
GenerateRandomAlphaOnlyString() Only used in test (integration-cli) integration-cli: RandomTmpDirPath() already mentions that it should not be used. integration-cli/cli/build/fakestorage/storage.go uses it. Other uses may not even need a random string; so either move it to the integration-cli, or perhaps it can be removed altogether.
GenerateRandomASCIIString() Looks like it's not used Probably can be removed
Ellipsis() Only used in the CLI (https://github.com/docker/cli) Move to that repository, and rewrite to indicate it uses displayWidth, not length.
Truncate() Looks like it's not used Move to the, and define its purpose, or perhaps rename it (TruncateDisplayWidth, rename the package to displayutils and name it displayutils.TrimWidth() - bad examples, but just to get you thinking), or remove it altogether
InSlice() used in a couple of tests, and also in the seccomp and oci_linux parts It's a small function, and given that it's used in different contexts, I think we should copy it to those parts that use it, possibly renaming it for each purpose.
ShellQuoteArguments() Looks like it's not used; also this isn't a "generic" string function - it's specifically for handling shell arguments. Given that it isn't used, we can probably remove it.

if maxlen <= 3 {
return string(r[:maxlen])
}
return string(r[:maxlen-3]) + "..."
Copy link
Member

Choose a reason for hiding this comment

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

If we're improving this function, perhaps this is the right moment to use the actual ellipsis ("…") character for this; not sure if that is one or two positions wide, but at least it allows showing more of the original string 👍

//In a broad sense, wide characters include East Asian Wide, East Asian Fullwidth, and East Asian Ambiguous,
//see http://unicode.org/reports/tr11/
kind := width.LookupRune(r).Kind()
if kind == width.EastAsianAmbiguous || kind == width.EastAsianWide || kind == width.EastAsianFullwidth {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this should be used to a utility function, something like:

// charWidth returns the number of horizontal positions a character occupies,
// and is used to account for wide characters when displaying strings.
//
// In a broad sense, wide characters include East Asian Wide, East Asian Full-width,
// and East Asian Ambiguous (see http://unicode.org/reports/tr11/).
func charWidth(r rune) int {
	switch width.LookupRune(r).Kind() {
	case width.EastAsianAmbiguous, width.EastAsianWide, width.EastAsianFullwidth:
		return 2
	default:
		return 1
	}
}

Then this loop could be something like;

	var (
		display      []int
		displayWidth int
	)
	rs := []rune(s)
	for _, r := range rs {
		cw := charWidth(r)
		displayWidth += cw
		display = append(display, cw)
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK , I will improve the code after we move the function to CLI repository.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Are you interested in working on that move?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will be a great pleasure :)

@ChiuWang
Copy link
Contributor Author

ChiuWang commented Oct 12, 2017

Thanks for your careful work @thaJeztah
So based on the above, I will
PR[*]: dumplicate the package function by function.
then
PR: remove the stringutils package in moby
then
Close this PR

@thaJeztah
Copy link
Member

Awesome! First step would be to open a PR in https://github.com/docker/cli to have these utilities there.

Once that is done, you can open a new PR in this repository to remove the things we no longer need (and make the other changes)

I suggest we close this PR for now; does that sound good to you?

@ChiuWang
Copy link
Contributor Author

OK.

@ChiuWang ChiuWang closed this Oct 12, 2017
@ChiuWang
Copy link
Contributor Author

ChiuWang commented Oct 12, 2017

But I'm a little confused, what doesopen a PR in https://github.com/docker/cli to have these utilities there exactly mean. @thaJeztah

@thaJeztah
Copy link
Member

That repository contains the Docker CLI; the parts of stringutils that are only used there (such as Ellipsis() should be added to that repository; if you can open a pull request for that.

Happy to give more information/help if you need

@ChiuWang
Copy link
Contributor Author

Yeah, I know this. What i'm comfused is the stringutils(moby) is in moby/moby/pkg/ ,but stringutils(cli) is in docker/cli/vendor/github.com/docker/docker/pkg/, this "vendor-relation" between moby and docker is somewhat equivocal.(sorry I'm a newbee ^-^)

@thaJeztah
Copy link
Member

Ah yes; what we use in these repositories is "vendoring"; basically, instead of downloading dependencies when compiling, we keep a copy of all dependencies in the repository.

Doing so, we know exactly which version of the dependency is used (the vendor.conf file contains the exact commit that was used), and also makes sure we can always build (even if the server hosting the dependency is down)

In this case, the Docker CLI uses some packages from this (https://github.com/moby/moby) repository - but still under the old name (https://github.com/docker/docker - that will change soon).

Here's some information about vendoring that I found https://blog.gopheracademy.com/advent-2015/vendor-folder/

@ChiuWang
Copy link
Contributor Author

If so, IMHO why not just change the code in this repository and then use vendor tools to update the package for CLI. But you recommend First step would be to open a PR in CLI ... Once that is done....

@thaJeztah
Copy link
Member

Well; the reason for moving it, is that usually, packages in a repository should be in that repository because the repository itself uses it (but they could be useful for other projects)

I this case, the code is not used at all in this repository, so it's better to move it to the repository (Docker CLI) where it's actually used

@ChiuWang
Copy link
Contributor Author

Thx! sorry for bothering u the beginner level question.

@thaJeztah
Copy link
Member

Not at all; they're good questions!

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.

5 participants