From 5f317628579a2a965d91754682a4c420ecba1884 Mon Sep 17 00:00:00 2001 From: Naveen Mahalingam Date: Fri, 15 Apr 2022 09:43:01 -0700 Subject: [PATCH] code smell fixes; cleanup demo-progress README (#200) --- cmd/demo-progress/README.md | 23 +------ text/transformer.go | 130 +++++++++++++++++++++--------------- text/transformer_test.go | 4 ++ text/wrap.go | 23 ++++--- 4 files changed, 97 insertions(+), 83 deletions(-) diff --git a/cmd/demo-progress/README.md b/cmd/demo-progress/README.md index 983790b..735db87 100644 --- a/cmd/demo-progress/README.md +++ b/cmd/demo-progress/README.md @@ -1,24 +1,3 @@ Output of `go run cmd/demo-list/demo.go`: -``` -Tracking Progress of 13 trackers ... - -Calculating Total # 1 ... done! [250 in 101ms] -Calculating Total # 2 ... done! [2.00K in 101ms] -Downloading File # 3 ... done! [6.75KB in 101ms] -Transferring Amount # 4 ... done! [$16.00K in 200ms] -Transferring Amount # 5 ... done! [£31.25K in 201ms] -Downloading File # 6 ... done! [54.00KB in 300ms] -Calculating Total # 7 ... done! [85.75K in 400ms] -Transferring Amount # 8 ... done! [$128.00K in 500ms] -Downloading File # 9 ... done! [182.25KB in 700ms] -Transferring Amount # 10 ... done! [£250.00K in 801ms] -Calculating Total # 11 ... done! [332.75K in 1s] -Transferring Amount # 12 ... done! [$432.00K in 1.2s] -Calculating Total # 13 ... done! [549.25K in 1.301s] - -All done! -``` - -Real-time playback of the demo @ asciinema.org: -[![asciicast](https://asciinema.org/a/KcPw8aoBSsYCBOj60wluhu5z3.png)](https://asciinema.org/a/KcPw8aoBSsYCBOj60wluhu5z3) + \ No newline at end of file diff --git a/text/transformer.go b/text/transformer.go index 6538668..872d663 100644 --- a/text/transformer.go +++ b/text/transformer.go @@ -42,71 +42,92 @@ type Transformer func(val interface{}) string // * colors positive values Green func NewNumberTransformer(format string) Transformer { return func(val interface{}) string { - if number, ok := val.(int); ok { - return transformInt(format, int64(number)) + if valStr := transformInt(format, val); valStr != "" { + return valStr } - if number, ok := val.(int8); ok { - return transformInt(format, int64(number)) + if valStr := transformUint(format, val); valStr != "" { + return valStr } - if number, ok := val.(int16); ok { - return transformInt(format, int64(number)) - } - if number, ok := val.(int32); ok { - return transformInt(format, int64(number)) - } - if number, ok := val.(int64); ok { - return transformInt(format, int64(number)) - } - if number, ok := val.(uint); ok { - return transformUint(format, uint64(number)) - } - if number, ok := val.(uint8); ok { - return transformUint(format, uint64(number)) - } - if number, ok := val.(uint16); ok { - return transformUint(format, uint64(number)) - } - if number, ok := val.(uint32); ok { - return transformUint(format, uint64(number)) - } - if number, ok := val.(uint64); ok { - return transformUint(format, uint64(number)) - } - if number, ok := val.(float32); ok { - return transformFloat(format, float64(number)) - } - if number, ok := val.(float64); ok { - return transformFloat(format, float64(number)) + if valStr := transformFloat(format, val); valStr != "" { + return valStr } return fmt.Sprint(val) } } -func transformInt(format string, val int64) string { - if val < 0 { - return colorsNumberNegative.Sprintf("-"+format, -val) +func transformInt(format string, val interface{}) string { + transform := func(val int64) string { + if val < 0 { + return colorsNumberNegative.Sprintf("-"+format, -val) + } + if val > 0 { + return colorsNumberPositive.Sprintf(format, val) + } + return colorsNumberZero.Sprintf(format, val) + } + + if number, ok := val.(int); ok { + return transform(int64(number)) + } + if number, ok := val.(int8); ok { + return transform(int64(number)) + } + if number, ok := val.(int16); ok { + return transform(int64(number)) } - if val > 0 { - return colorsNumberPositive.Sprintf(format, val) + if number, ok := val.(int32); ok { + return transform(int64(number)) } - return colorsNumberZero.Sprintf(format, val) + if number, ok := val.(int64); ok { + return transform(int64(number)) + } + return "" } -func transformUint(format string, val uint64) string { - if val > 0 { - return colorsNumberPositive.Sprintf(format, val) +func transformUint(format string, val interface{}) string { + transform := func(val uint64) string { + if val > 0 { + return colorsNumberPositive.Sprintf(format, val) + } + return colorsNumberZero.Sprintf(format, val) + } + + if number, ok := val.(uint); ok { + return transform(uint64(number)) + } + if number, ok := val.(uint8); ok { + return transform(uint64(number)) + } + if number, ok := val.(uint16); ok { + return transform(uint64(number)) + } + if number, ok := val.(uint32); ok { + return transform(uint64(number)) } - return colorsNumberZero.Sprintf(format, val) + if number, ok := val.(uint64); ok { + return transform(uint64(number)) + } + return "" } -func transformFloat(format string, val float64) string { - if val < 0 { - return colorsNumberNegative.Sprintf("-"+format, -val) +func transformFloat(format string, val interface{}) string { + transform := func(val float64) string { + if val < 0 { + return colorsNumberNegative.Sprintf("-"+format, -val) + } + if val > 0 { + return colorsNumberPositive.Sprintf(format, val) + } + return colorsNumberZero.Sprintf(format, val) } - if val > 0 { - return colorsNumberPositive.Sprintf(format, val) + + if number, ok := val.(float32); ok { + return transform(float64(number)) } - return colorsNumberZero.Sprintf(format, val) + if number, ok := val.(float64); ok { + return transform(float64(number)) + } + return "" } // NewJSONTransformer returns a Transformer that can format a JSON string or an @@ -172,10 +193,15 @@ func NewUnixTimeTransformer(layout string, location *time.Location) Transformer } // NewURLTransformer returns a Transformer that can format and pretty print a string -// that contains an URL (the text is underlined and colored Blue). -func NewURLTransformer() Transformer { +// that contains a URL (the text is underlined and colored Blue). +func NewURLTransformer(colors ...Color) Transformer { + colorsToUse := colorsURL + if len(colors) > 0 { + colorsToUse = colors + } + return func(val interface{}) string { - return colorsURL.Sprint(val) + return colorsToUse.Sprint(val) } } diff --git a/text/transformer_test.go b/text/transformer_test.go index 285b102..b42aea4 100644 --- a/text/transformer_test.go +++ b/text/transformer_test.go @@ -223,7 +223,11 @@ func TestNewUnixTimeTransformer(t *testing.T) { func TestNewURLTransformer(t *testing.T) { url := "https://winter.is.coming" + transformer := NewURLTransformer() + assert.Equal(t, colorsURL.Sprint(url), transformer(url)) + transformer2 := NewURLTransformer(FgRed, BgWhite, Bold) + assert.Equal(t, Colors{FgRed, BgWhite, Bold}.Sprint(url), transformer2(url)) assert.Equal(t, colorsURL.Sprint(url), transformer(url)) } diff --git a/text/wrap.go b/text/wrap.go index 7617bb1..ca15276 100644 --- a/text/wrap.go +++ b/text/wrap.go @@ -233,15 +233,7 @@ func wrapSoft(paragraph string, wrapLen int, out *strings.Builder) { out.WriteString(word) lineLen += spacingLen + wordLen } else { // word doesn't fit within the line - if lineLen > 0 { // something is already on the line; terminate it - terminateLine(wrapLen, &lineLen, lastSeenEscSeq, out) - } - if wordLen <= wrapLen { // word fits within a single line - out.WriteString(word) - lineLen = wordLen - } else { // word doesn't fit within a single line; hard-wrap - appendWord(word, &lineLen, lastSeenEscSeq, wrapLen, out) - } + lineLen = wrapSoftLastWordInLine(wrapLen, lineLen, lastSeenEscSeq, wordLen, word, out) } // end of line; but more words incoming @@ -252,6 +244,19 @@ func wrapSoft(paragraph string, wrapLen int, out *strings.Builder) { terminateOutput(lastSeenEscSeq, out) } +func wrapSoftLastWordInLine(wrapLen int, lineLen int, lastSeenEscSeq string, wordLen int, word string, out *strings.Builder) int { + if lineLen > 0 { // something is already on the line; terminate it + terminateLine(wrapLen, &lineLen, lastSeenEscSeq, out) + } + if wordLen <= wrapLen { // word fits within a single line + out.WriteString(word) + lineLen = wordLen + } else { // word doesn't fit within a single line; hard-wrap + appendWord(word, &lineLen, lastSeenEscSeq, wrapLen, out) + } + return lineLen +} + func wrapSoftSpacing(lineLen int) (string, int) { spacing, spacingLen := "", 0 if lineLen > 0 {