From 17b1e81ca13bc386be182fdc93d59d50e978fb85 Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Wed, 15 May 2024 16:29:33 -0700 Subject: [PATCH 1/5] fixed width and word count for double spacing --- cmd/cmd.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index cae35f51..1606e9f1 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -25,7 +25,7 @@ import ( "time" "github.com/containerd/console" - + "github.com/mattn/go-runewidth" "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" "golang.org/x/crypto/ssh" @@ -739,12 +739,13 @@ type displayResponseState struct { wordBuffer string } +// using runewidth instead of len (cus length is number of bytes, we wnat display length) func displayResponse(content string, wordWrap bool, state *displayResponseState) { termWidth, _, _ := term.GetSize(int(os.Stdout.Fd())) if wordWrap && termWidth >= 10 { for _, ch := range content { - if state.lineLength+1 > termWidth-5 { - if len(state.wordBuffer) > termWidth-10 { + if state.lineLength+1 > termWidth - 5 { + if runewidth.StringWidth(state.wordBuffer) > termWidth - 10 { fmt.Printf("%s%c", state.wordBuffer, ch) state.wordBuffer = "" state.lineLength = 0 @@ -752,12 +753,18 @@ func displayResponse(content string, wordWrap bool, state *displayResponseState) } // backtrack the length of the last word and clear to the end of the line - fmt.Printf("\x1b[%dD\x1b[K\n", len(state.wordBuffer)) + fmt.Printf("\x1b[%dD\x1b[K\n", runewidth.StringWidth(state.wordBuffer)) fmt.Printf("%s%c", state.wordBuffer, ch) - state.lineLength = len(state.wordBuffer) + 1 + chWidth := runewidth.RuneWidth(ch) + + state.lineLength = runewidth.StringWidth(state.wordBuffer) + chWidth } else { fmt.Print(string(ch)) - state.lineLength += 1 + state.lineLength += runewidth.RuneWidth(ch) + if runewidth.RuneWidth(ch) >= 2 { + state.wordBuffer = "" + continue + } switch ch { case ' ': From c9e584fb9009faff2b1f2e38b4c7ab86a7d150b1 Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Wed, 15 May 2024 16:45:24 -0700 Subject: [PATCH 2/5] updated double-width display --- cmd/cmd.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/cmd.go b/cmd/cmd.go index 1606e9f1..91a77c79 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -745,6 +745,7 @@ func displayResponse(content string, wordWrap bool, state *displayResponseState) if wordWrap && termWidth >= 10 { for _, ch := range content { if state.lineLength+1 > termWidth - 5 { + if runewidth.StringWidth(state.wordBuffer) > termWidth - 10 { fmt.Printf("%s%c", state.wordBuffer, ch) state.wordBuffer = "" From 799aa9883cd9d33b44e7ab64540093a9a4e3cbab Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Wed, 15 May 2024 17:24:17 -0700 Subject: [PATCH 3/5] go fmt'd cmd.go --- cmd/cmd.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 91a77c79..a0ae86d3 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -744,9 +744,9 @@ func displayResponse(content string, wordWrap bool, state *displayResponseState) termWidth, _, _ := term.GetSize(int(os.Stdout.Fd())) if wordWrap && termWidth >= 10 { for _, ch := range content { - if state.lineLength+1 > termWidth - 5 { + if state.lineLength+1 > termWidth-5 { - if runewidth.StringWidth(state.wordBuffer) > termWidth - 10 { + if runewidth.StringWidth(state.wordBuffer) > termWidth-10 { fmt.Printf("%s%c", state.wordBuffer, ch) state.wordBuffer = "" state.lineLength = 0 @@ -765,7 +765,7 @@ func displayResponse(content string, wordWrap bool, state *displayResponseState) if runewidth.RuneWidth(ch) >= 2 { state.wordBuffer = "" continue - } + } switch ch { case ' ': From 26bfc1c443ce178fa009747b2cf83ad9876d5a6a Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Wed, 15 May 2024 17:26:39 -0700 Subject: [PATCH 4/5] go fmt'd cmd.go --- cmd/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index a0ae86d3..8917022e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -739,7 +739,7 @@ type displayResponseState struct { wordBuffer string } -// using runewidth instead of len (cus length is number of bytes, we wnat display length) +// using runewidth instead of len (cus length is number of bytes, we want display length) func displayResponse(content string, wordWrap bool, state *displayResponseState) { termWidth, _, _ := term.GetSize(int(os.Stdout.Fd())) if wordWrap && termWidth >= 10 { From 3d90156e997dd5daec727152f0cc85f26cc91342 Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Thu, 16 May 2024 14:12:03 -0700 Subject: [PATCH 5/5] removed comment --- cmd/cmd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 8917022e..dff8d7c1 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -739,7 +739,6 @@ type displayResponseState struct { wordBuffer string } -// using runewidth instead of len (cus length is number of bytes, we want display length) func displayResponse(content string, wordWrap bool, state *displayResponseState) { termWidth, _, _ := term.GetSize(int(os.Stdout.Fd())) if wordWrap && termWidth >= 10 {