Docker Community Forums

Share and learn in the Docker community.

Non-ascii character in docker client output (UTF-8 ellipsis)


(Georgalis) #1

Hi, I’ve been struggling with non-ascii output from docker client. I have a workaround on command line, by using tmux which converts to ascii in my terminal, but that doesn’t apply to scripts where I really need it, eg docker history 79f4bda91989 > non-ascii is there any way to configure the client not to use UTF-8 characters?


(Georgalis) #2

FYI - I think I got to the bottom of it. 1) The docker cli doesn’t respect locale for display. 2) The (Go) vendor unicode/utf8 libraries are used throughout the code base for string functions (fine). 3) There is at least one function in the codebase to throw an error for utf8 character use (parseKeyValueFile). 4) There is exactly one use of utf8 characters in the code base, the function “Ellipsis” which truncates strings and inserts an “…” character at the end.
The irony is that silly character in the output of the cli makes it very difficult to parse the tool data for programmed consumption, it may as well be generating jpg files of command outputs.
Solution: fork and patch “Ellipsis” function to check for DONT_POLLUTE_OUTPUT_WITH_UTF8 set in the environment, if set use “_” instead of “…” when truncating.

diff --git a/components/cli/cli/command/formatter/displayutils.go b/components/cli/cli/command/formatter/displayutils.go
index 0c3b6ebbb0..21129c5a08 100644
--- a/components/cli/cli/command/formatter/displayutils.go
+++ b/components/cli/cli/command/formatter/displayutils.go
@@ -22,8 +22,9 @@ func charWidth(r rune) int {

 // Ellipsis truncates a string to fit within maxDisplayWidth, and appends ellipsis (<E2><80><A6>).
 // For maxDisplayWidth of 1 and lower, no ellipsis is appended.
 // For maxDisplayWidth of 1, first char of string will return even if its width > 1.
+// XXX
 func Ellipsis(s string, maxDisplayWidth int) string {
        if maxDisplayWidth <= 0 {
                return ""
        }
diff --git a/components/cli/opts/file.go b/components/cli/opts/file.go
index 1721a46ef9..ec305487fb 100644
--- a/components/cli/opts/file.go
+++ b/components/cli/opts/file.go
@@ -31,8 +31,11 @@ func parseKeyValueFile(filename string, emptyFn func(string) (string, bool)) ([]
        lines := []string{}
        scanner := bufio.NewScanner(fh)
        currentLine := 0
        utf8bom := []byte{0xEF, 0xBB, 0xBF}
+       // and yet we have an aperent solitary use of utf8 characters in the cli, the function Ellipsis
+       // "Ellipsis truncates a string to fit within maxDisplayWidth, and appends ellipsis (~@)."
+       // docker-ce/components/cli/cli/command/formatter/displayutils.go
        for scanner.Scan() {
                scannedBytes := scanner.Bytes()
                if !utf8.Valid(scannedBytes) {
                        return []string{}, fmt.Errorf("env file %s contains invalid utf8 bytes at line %d: %v", filename, currentLine+1, scannedBytes)