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

list: add unindent all function #217

Merged
merged 3 commits into from Aug 3, 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
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
mneira10 marked this conversation as resolved.
Show resolved Hide resolved
* 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