Skip to content

Commit

Permalink
list: add unindent all function (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
mneira10 committed Aug 3, 2022
1 parent eb654fb commit 6bf8d65
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions list/list.go
Expand Up @@ -120,6 +120,10 @@ func (l *List) UnIndent() {
}
}

func (l *List) UnIndentAll() {
l.level = 0
}

func (l *List) initForRender() {
// pick a default style
l.Style()
Expand Down
11 changes: 11 additions & 0 deletions list/list_test.go
Expand Up @@ -149,3 +149,14 @@ func TestList_UnIndent(t *testing.T) {
list.UnIndent()
assert.Equal(t, 0, list.level)
}

func TestList_UnIndentAll(t *testing.T) {
list := List{level: 3}

list.UnIndentAll()
assert.Equal(t, 0, list.level)

// Ensure level is still 0 after 2 consecutive calls
list.UnIndentAll()
assert.Equal(t, 0, list.level)
}
44 changes: 44 additions & 0 deletions list/render_test.go
Expand Up @@ -289,3 +289,47 @@ func TestList_Render_Styles(t *testing.T) {
fmt.Println(mismatch)
}
}

func TestList_Render_UnindentAll(t *testing.T) {
lw := NewWriter()
lw.AppendItem(testItem1)
lw.Indent()
lw.AppendItems(testItems2)
lw.Indent()
lw.AppendItems(testItems3)
lw.UnIndentAll()
lw.AppendItem(testItem4)
lw.Indent()
lw.AppendItem(testItem5)
lw.UnIndentAll()
lw.AppendItem("The Mandalorian")
lw.Indent()
lw.AppendItem("This")
lw.Indent()
lw.AppendItem("Is")
lw.Indent()
lw.AppendItem("The")
lw.Indent()
lw.AppendItem("Way")
lw.Indent()
lw.UnIndentAll()
lw.AppendItem("Right?")

expectedOut := `* Game Of Thrones
* Winter
* Is
* Coming
* This
* Is
* Known
* The Dark Tower
* The Gunslinger
* The Mandalorian
* This
* Is
* The
* Way
* Right?`
assert.Equal(t, expectedOut, lw.Render())

}
1 change: 1 addition & 0 deletions list/writer.go
Expand Up @@ -17,6 +17,7 @@ type Writer interface {
SetStyle(style Style)
Style() *Style
UnIndent()
UnIndentAll()
}

// NewWriter initializes and returns a Writer.
Expand Down

0 comments on commit 6bf8d65

Please sign in to comment.