From 72f6da20abddf5b44e2e8d5798e31a6d97431e51 Mon Sep 17 00:00:00 2001 From: Funky Gao Date: Sat, 24 Dec 2016 14:13:03 +0800 Subject: [PATCH 1/5] solve issues when display with terminal color before this fix, the output was not aligned correctly --- columnize.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/columnize.go b/columnize.go index 915716a..e1929ab 100644 --- a/columnize.go +++ b/columnize.go @@ -100,9 +100,22 @@ func elementsFromLine(config *Config, line string) []interface{} { // runeLen calculates the number of visible "characters" in a string func runeLen(s string) int { l := 0 - for _ = range s { + insideConsoleColor := false + for _, c := range s { + if c == '\x1b' { // start of esc color sequence + insideConsoleColor = true + continue + } + if insideConsoleColor { + if c == 'm' { // end of esc color sequence + insideConsoleColor = false + } + continue + } + l++ } + return l } From 7e3d9b101c2eed7deedb518547a5e3a4185282b3 Mon Sep 17 00:00:00 2001 From: Funky Gao Date: Thu, 19 Jan 2017 14:15:38 +0800 Subject: [PATCH 2/5] add debug mode --- columnize.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/columnize.go b/columnize.go index e1929ab..81fd908 100644 --- a/columnize.go +++ b/columnize.go @@ -3,6 +3,7 @@ package columnize import ( "bytes" "fmt" + "os" "strings" ) @@ -136,6 +137,10 @@ func widthsFromLines(config *Config, lines []string) []int { } } } + + if os.Getenv("DEBUG_COL") == "1" { + fmt.Println(widths) + } return widths } From a1f20b6c1db0ed01c2de63ac288b2cf45f7bfba8 Mon Sep 17 00:00:00 2001 From: gaopeng71 Date: Sun, 11 Nov 2018 20:59:59 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=89=BE=E5=88=B0=E4=BA=86=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E5=AF=B9=E4=B8=8D=E9=BD=90=E7=9A=84=E5=8E=9F=E5=9B=A0?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=20=E6=98=AFfmt.Sprintf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- columnize.go | 6 ++++++ columnize_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/columnize.go b/columnize.go index 81fd908..3a7f253 100644 --- a/columnize.go +++ b/columnize.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strings" + "unicode" ) // Config can be used to tune certain parameters which affect the way @@ -115,6 +116,10 @@ func runeLen(s string) int { } l++ + if unicode.Is(unicode.Scripts["Han"], rune(c)) { + l++ + } + } return l @@ -176,6 +181,7 @@ func Format(lines []string, config *Config) string { fmtCache[numElems] = stringfmt } + // %-21s, 如果中文会有问题 fmt.Fprintf(buf, stringfmt, elems...) } diff --git a/columnize_test.go b/columnize_test.go index 89dabaa..58dc87e 100644 --- a/columnize_test.go +++ b/columnize_test.go @@ -7,6 +7,29 @@ import ( crand "crypto/rand" ) +func TestSimpleFormatWithChinese(t *testing.T) { + lines := []string{} + lines = append(lines, fmt.Sprintf("%s|%s", "阿尔法项目", "10.187.131.227")) + lines = append(lines, fmt.Sprintf("%s|%s", "ide.cc.polaris.jd.com", "10.191.92.130")) + + println(Format(lines, &Config{Glue: " ", Empty: " "})) + println() + fmt.Printf("%-21s %s\n", "阿尔法项目", "abc") + fmt.Printf("%-21s %s\n", "ide.cc.polaris.jd.com", "cm") +} + +func TestRuneLen(t *testing.T) { + if runeLen("ab") != 2 { + t.Fatalf("ab should 2") + } + if runeLen("我") != 2 { + t.Fatalf("我 should 2") + } + if runeLen("我b") != 3 { + t.Fatalf("我b should 3") + } +} + func TestListOfStringsInput(t *testing.T) { input := []string{ "Column A | Column B | Column C", @@ -210,7 +233,7 @@ func TestAlternateSpacingString(t *testing.T) { } } -func TestSimpleFormat(t *testing.T) { +func TestSimpleFormatBasic(t *testing.T) { input := []string{ "Column A | Column B | Column C", "x | y | z", From 5368ae5ec8e6439fa76dd9226c2026f3740f8cef Mon Sep 17 00:00:00 2001 From: gaopeng71 Date: Sun, 11 Nov 2018 21:46:26 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E5=AF=B9=E9=BD=90=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- columnize.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/columnize.go b/columnize.go index 3a7f253..e30f90b 100644 --- a/columnize.go +++ b/columnize.go @@ -181,8 +181,31 @@ func Format(lines []string, config *Config) string { fmtCache[numElems] = stringfmt } - // %-21s, 如果中文会有问题 - fmt.Fprintf(buf, stringfmt, elems...) + if false { + // 原作者的逻辑 + // fmt.Fprintf("%-21s %s", "我们", "12.21.212.1"), 如果中文会有问题 + fmt.Fprintf(buf, stringfmt, elems...) + } else { + // 手动解决 + fmt.Fprintf(buf, conf.Prefix) + for col, elem := range elems { + s := elem.(string) + for _, c := range s { + fmt.Fprintf(buf, string(c)) + } + + if col == len(elems)-1 { + // 最后一列 + fmt.Fprintf(buf, "\n") + } else { + // space padding + fmt.Fprintf(buf, strings.Repeat(" ", widths[col]-runeLen(s))) + // glue padding + fmt.Fprintf(buf, conf.Glue) + } + + } + } } // Get the string result From d92c1d6104e02f017eb883243855af2b55105583 Mon Sep 17 00:00:00 2001 From: gaopeng71 Date: Sun, 11 Nov 2018 22:20:14 +0800 Subject: [PATCH 5/5] FIX: format placeholder should be handled --- columnize.go | 2 +- columnize_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/columnize.go b/columnize.go index e30f90b..c2c2db6 100644 --- a/columnize.go +++ b/columnize.go @@ -191,7 +191,7 @@ func Format(lines []string, config *Config) string { for col, elem := range elems { s := elem.(string) for _, c := range s { - fmt.Fprintf(buf, string(c)) + fmt.Fprintf(buf, strings.Replace(string(c), "%", "%%", -1)) } if col == len(elems)-1 { diff --git a/columnize_test.go b/columnize_test.go index 58dc87e..e56023f 100644 --- a/columnize_test.go +++ b/columnize_test.go @@ -8,7 +8,7 @@ import ( ) func TestSimpleFormatWithChinese(t *testing.T) { - lines := []string{} + lines := []string{"CPU%|IP"} lines = append(lines, fmt.Sprintf("%s|%s", "阿尔法项目", "10.187.131.227")) lines = append(lines, fmt.Sprintf("%s|%s", "ide.cc.polaris.jd.com", "10.191.92.130"))