Skip to content

Commit

Permalink
list: refactor style and fix connected lists (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed May 30, 2018
1 parent b28165e commit 0f07093
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 491 deletions.
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: all dep profile test

default: build
default: test

all: dep test bench

Expand All @@ -12,12 +12,18 @@ tools:
bench:
go test -bench=. -benchmem

build:
go run cmd/demo/demo.go

cyclo:
gocyclo -over 13 ./*/*.go

demo-list:
go run cmd/demo-list/demo.go

demo-progress:
go run cmd/demo-progress/demo.go

demo-table:
go run cmd/demo-table/demo.go

dep:
dep ensure

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ The unit-tests for each of the above show how these are to be used.

Partial output of `make bench`:
```
BenchmarkList_Render-8 1000000 1850 ns/op 808 B/op 22 allocs/op
BenchmarkList_Render-8 500000 2182 ns/op 760 B/op 40 allocs/op
BenchmarkProgress_Render-8 2 800863000 ns/op 7200 B/op 209 allocs/op
BenchmarkTable_Render-8 100000 20839 ns/op 5538 B/op 188 allocs/op
BenchmarkTable_RenderCSV-8 300000 4479 ns/op 2464 B/op 45 allocs/op
Expand Down
34 changes: 17 additions & 17 deletions cmd/demo-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ A List using the Style 'StyleBulletCircle':
A List using the Style 'StyleConnectedRounded':
-----------------------------------------------
╭─ Game Of Thrones
├─┬─ Winter
│ ├─ Is
─ Coming
╰─┬─ This
│ ├─ Is
│ ╰─ Known
─ The Dark Tower
╰─── The Gunslinger
│ ├─ Winter
├─ Is
─ Coming
─ This
├─ Is
╰─ Known
─ The Dark Tower
─ The Gunslinger
A List using the Style 'funkyStyle':
------------------------------------
^> GAME OF THRONES
~f> WINTER
i> IS
i> COMING
c~f> THIS
i> IS
v> KNOWN
i> THE DARK TOWER
c~> THE GUNSLINGER
t GAME OF THRONES
|f WINTER
|m IS
|b COMING
| f THIS
| m IS
| b KNOWN
b THE DARK TOWER
b THE GUNSLINGER
A List in HTML format:
----------------------
Expand Down
38 changes: 19 additions & 19 deletions cmd/demo-list/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,30 @@ func main() {
// I want my own Style!
//==========================================================================
funkyStyle := list.Style{
Name: "funkyStyle",
CharConnectBottom: "c",
CharHorizontal: "~",
CharItem: "i",
CharItemBottom: "v",
CharItemFirst: "f",
CharItemTop: "^",
CharPaddingLeft: "<",
CharPaddingRight: ">",
Format: text.FormatUpper,
CharItemSingle: "s",
CharItemTop: "t",
CharItemFirst: "f",
CharItemMiddle: "m",
CharItemVertical: "|",
CharItemBottom: "b",
CharNewline: "\n",
Format: text.FormatUpper,
LinePrefix: "",
Name: "styleTest",
}
l.SetStyle(funkyStyle)
demoPrint("A List using the Style 'funkyStyle'", l.Render(), "")
//A List using the Style 'funkyStyle':
//------------------------------------
//^> GAME OF THRONES
//~f> WINTER
// i> IS
// i> COMING
// c~f> THIS
// i> IS
// v> KNOWN
//i> THE DARK TOWER
//c~> THE GUNSLINGER
//t GAME OF THRONES
//|f WINTER
//|m IS
//|b COMING
//| f THIS
//| m IS
//| b KNOWN
//b THE DARK TOWER
// b THE GUNSLINGER
//==========================================================================

//==========================================================================
Expand Down
3 changes: 0 additions & 3 deletions cmd/demo/README.md

This file was deleted.

109 changes: 0 additions & 109 deletions cmd/demo/demo.go

This file was deleted.

Binary file removed cmd/demo/demo.png
Binary file not shown.
35 changes: 24 additions & 11 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,10 @@ func (l *List) Style() *Style {
}

func (l *List) analyzeAndStringify(item interface{}) *listItem {
listEntry := &listItem{
return &listItem{
Level: l.level,
Text: fmt.Sprint(item),
}

// account for the following when incrementing approxSize: 1. length of
// text, 2. left-padding, 3. list-prefix, 4. right-padding, 5. newline
l.approxSize += utf8.RuneCountInString(listEntry.Text) + (l.level * 2) + 2 + 1 + 1
// 6. connector in case of level change
if len(l.items) > 0 && listEntry.Level > l.items[len(l.items)-1].Level {
l.approxSize++
}

return listEntry
}

// UnIndent un-indents the following items to appear left-shifted.
Expand All @@ -127,6 +117,20 @@ func (l *List) initForRender() {
// pick a default style
l.Style()

// calculate the approximate size needed by looking at all entries
l.approxSize = 0
for _, item := range l.items {
// account for the following when incrementing approxSize:
// 1. prefix, 2. padding, 3. bullet, 4. text, 5. newline
l.approxSize += utf8.RuneCountInString(l.style.LinePrefix)
if item.Level > 0 {
l.approxSize += utf8.RuneCountInString(l.style.CharItemVertical) * item.Level
}
l.approxSize += utf8.RuneCountInString(l.style.CharItemVertical)
l.approxSize += utf8.RuneCountInString(item.Text)
l.approxSize += utf8.RuneCountInString(l.style.CharNewline)
}

// default to a HTML CSS Class if none-defined
if l.htmlCSSClass == "" {
l.htmlCSSClass = DefaultHTMLCSSClass
Expand All @@ -152,3 +156,12 @@ func (l *List) render(out *strings.Builder) string {
}
return outStr
}

// renderHint has hints for the Render*() logic
type renderHint struct {
isTopItem bool
isFirstItem bool
isOnlyItem bool
isLastItem bool
isBottomItem bool
}
8 changes: 4 additions & 4 deletions list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func TestList_SetStyle(t *testing.T) {
list.SetStyle(StyleConnectedLight)
assert.NotNil(t, list.Style())
assert.Equal(t, &StyleConnectedLight, list.Style())
expectedOut = `─ Game Of Thrones
└─┬─ Winter
├─ Is
└─ Coming`
expectedOut = `─ Game Of Thrones
─ Winter
├─ Is
└─ Coming`
assert.Equal(t, expectedOut, list.Render())
}

Expand Down

0 comments on commit 0f07093

Please sign in to comment.