mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-10-06 02:37:23 +03:00
feb42a9be4
* feat: support any type for list Root func Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * feat: move tree to root Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * feat: more cleanup Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * docs(examples): bring back tree examples * docs(examples): add tree toggle example * docs(examples): fix toggle tree hierarchy; as bashbunni metadata * fix(tree): stylize whitespace when vertically joining tree elements * docs: get started with trees * feat: add RootStyle with example * test: add test for RootStyle func * docs(godoc): replace list mentions in tree package * refactor(examples): use Root shorthand instead of tree.New where applicable --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> Co-authored-by: bashbunni <bunni@bashbunni.dev> Co-authored-by: Christian Rocha <christian@rocha.is>
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package tree
|
|
|
|
// Enumerator enumerates a tree. Typically, this is used to draw the branches
|
|
// for the tree nodes and is different for the last child.
|
|
//
|
|
// For example, the default enumerator would be:
|
|
//
|
|
// func TreeEnumerator(children Children, index int) string {
|
|
// if children.Length()-1 == index {
|
|
// return "└──"
|
|
// }
|
|
//
|
|
// return "├──"
|
|
// }
|
|
type Enumerator func(children Children, index int) string
|
|
|
|
// DefaultEnumerator enumerates a tree.
|
|
//
|
|
// ├── Foo
|
|
// ├── Bar
|
|
// ├── Baz
|
|
// └── Qux.
|
|
func DefaultEnumerator(children Children, index int) string {
|
|
if children.Length()-1 == index {
|
|
return "└──"
|
|
}
|
|
return "├──"
|
|
}
|
|
|
|
// RoundedEnumerator enumerates a tree with rounded edges.
|
|
//
|
|
// ├── Foo
|
|
// ├── Bar
|
|
// ├── Baz
|
|
// ╰── Qux.
|
|
func RoundedEnumerator(children Children, index int) string {
|
|
if children.Length()-1 == index {
|
|
return "╰──"
|
|
}
|
|
return "├──"
|
|
}
|
|
|
|
// Indenter indents the children of a tree.
|
|
//
|
|
// Indenters allow for displaying nested tree items with connecting borders
|
|
// to sibling nodes.
|
|
//
|
|
// For example, the default indenter would be:
|
|
//
|
|
// func TreeIndenter(children Children, index int) string {
|
|
// if children.Length()-1 == index {
|
|
// return "│ "
|
|
// }
|
|
//
|
|
// return " "
|
|
// }
|
|
type Indenter func(children Children, index int) string
|
|
|
|
// DefaultIndenter indents a tree for nested trees and multiline content.
|
|
//
|
|
// ├── Foo
|
|
// ├── Bar
|
|
// │ ├── Qux
|
|
// │ ├── Quux
|
|
// │ │ ├── Foo
|
|
// │ │ └── Bar
|
|
// │ └── Quuux
|
|
// └── Baz.
|
|
func DefaultIndenter(children Children, index int) string {
|
|
if children.Length()-1 == index {
|
|
return " "
|
|
}
|
|
return "│ "
|
|
}
|