Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code smell fixes; cleanup demo-progress README #200

Merged
merged 1 commit into from Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 1 addition & 22 deletions 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)
<img src="../../progress/images/demo.gif" width="640px"/>
130 changes: 78 additions & 52 deletions text/transformer.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
4 changes: 4 additions & 0 deletions text/transformer_test.go
Expand Up @@ -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))
}
23 changes: 14 additions & 9 deletions text/wrap.go
Expand Up @@ -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
Expand All @@ -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 {
Expand Down