tree

package module
v0.0.0-...-b0aaa01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2024 License: MIT Imports: 8 Imported by: 1

README

Tree Model for BubbleTea TUI framework

This is a package to be used in applications based on Bubble Tea TUI framework.

It can be used to output an interactive tree model.

The nodes of the tree, need to conform to a simple interface.


type Node interface {
	tea.Model
	Parent() Node
	Children() Nodes
	State() NodeState
}

Instalation

go get github.com/mariusor/bubbles-tree

Examples

To see two of the possible usages for this package check the examples folder where we have a minimal filesystem tree utility and a threaded conversation.

Tree
$ go run main.go -depth 3 ../../
 └─ ⊟ /some/path/bubbles-tree
    ├─ ⊟ examples
    │  └─ ⊟ tree
    │     ├─   go.mod
    │     ├─   go.sum
    │     ├─   go.work
    │     └─   main.go
    ├─   go.mod
    ├─   go.sum
    ├─   tree.go
    └─   tree_test.go
Conversation
 $ go run . -depth 3
╻
┃ Root node
┃ Sphinx of black quartz, judge my vow!
┃ The quick brown fox jumps over the lazy dog.
╹
┃ ╻
┃ ┃ Child node
┃ ┃ Sphinx of black quartz, judge my vow!
┃ ┃ The quick brown fox jumps over the lazy dog.
┃ ╹
┃ ┃ ╻
┃ ┃ ┃ Child node
┃ ┃ ┃ Sphinx of black quartz, judge my vow!
┃ ┃ ┃ The quick brown fox jumps over the lazy dog.
┃ ┃ ╹

Documentation

Index

Constants

View Source
const Ellipsis = "…"

Variables

This section is empty.

Functions

func Padding

func Padding(style DepthStyler, s Symbols, depth int) string

Padding is expected to output a whitespace, or equivalent, used when two nodes at the same level are not children to the same parent.

func RenderConnector

func RenderConnector(style DepthStyler, s Symbols, depth int) string

RenderConnector is expected to output a continuator marker used to connect two nodes which are children on the same parent.

func RenderStarter

func RenderStarter(style DepthStyler, s Symbols, depth int) string

RenderStarter is expected to output the marker used for every node in the tree.

func RenderTerminator

func RenderTerminator(style DepthStyler, s Symbols, depth int) string

RenderTerminator is expected to output a terminator marker used for the last node in a list of nodes.

Types

type DepthStyler

type DepthStyler interface {
	Width(int) DepthStyler
	Render(depth int, strs ...string) string
}

type ErrorMsg

type ErrorMsg error

type KeyMap

type KeyMap struct {
	LineUp       key.Binding
	LineDown     key.Binding
	PageUp       key.Binding
	PageDown     key.Binding
	HalfPageUp   key.Binding
	HalfPageDown key.Binding
	GotoTop      key.Binding
	GotoBottom   key.Binding

	Expand key.Binding
}

KeyMap defines keybindings. It satisfies the github.com/charm/bubbles/help.KeyMap interface.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns a default set of keybindings.

type Model

type Model struct {
	*viewport.Model

	KeyMap  KeyMap
	Styles  Styles
	Symbols Symbols
	// contains filtered or unexported fields
}

Model is the Bubble Tea model for this user interface.

func New

func New(t Nodes) Model

New initializes a new Model It sets the default keymap, styles and symbols.

func (*Model) Blur

func (m *Model) Blur()

Blur blurs the tree, preventing selection or movement.

func (*Model) Children

func (m *Model) Children() Nodes

func (*Model) Cursor

func (m *Model) Cursor() int

Cursor returns the index of the selected row.

func (*Model) Focus

func (m *Model) Focus()

Focus focuses the tree, allowing the user to move around the tree nodes. interact.

func (*Model) Focused

func (m *Model) Focused() bool

Focused returns the focus state of the tree.

func (*Model) GotoBottom

func (m *Model) GotoBottom() tea.Cmd

GotoBottom moves the selection to the last row.

func (*Model) GotoTop

func (m *Model) GotoTop() tea.Cmd

GotoTop moves the selection to the first row.

func (*Model) Height

func (m *Model) Height() int

Height returns the viewport height of the tree.

func (*Model) Init

func (m *Model) Init() tea.Cmd

func (*Model) MoveDown

func (m *Model) MoveDown(n int) tea.Cmd

MoveDown moves the selection down by any number of row. It can not go below the last row.

func (*Model) MoveUp

func (m *Model) MoveUp(n int) tea.Cmd

MoveUp moves the selection up by any number of row. It can not go above the first row.

func (*Model) PastBottom

func (m *Model) PastBottom() bool

PastBottom returns whether the viewport is scrolled beyond the last line. This can happen when adjusting the viewport height.

func (*Model) ScrollPercent

func (m *Model) ScrollPercent() float64

ScrollPercent returns the amount scrolled as a float between 0 and 1.

func (*Model) SetCursor

func (m *Model) SetCursor(pos int) tea.Cmd

SetCursor returns the index of the selected row.

func (*Model) SetHeight

func (m *Model) SetHeight(h int)

SetHeight sets the height of the viewport of the tree.

func (*Model) SetStyles

func (m *Model) SetStyles(s Styles)

SetStyles sets the tree Styles.

func (*Model) SetWidth

func (m *Model) SetWidth(w int)

SetWidth sets the width of the viewport of the tree.

func (*Model) SetYOffset

func (m *Model) SetYOffset(n int)

SetYOffset sets Y offset of the tree's viewport.

func (*Model) ToggleExpand

func (m *Model) ToggleExpand()

ToggleExpand toggles the expanded state of the node pointed at by m.cursor

func (*Model) Update

func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update is the Tea update function which binds keystrokes to pagination.

func (*Model) View

func (m *Model) View() string

View renders the pagination to a string.

func (*Model) Width

func (m *Model) Width() int

Width returns the viewport width of the tree.

func (*Model) YOffset

func (m *Model) YOffset() int

YOffset returns the viewport vertical scroll position of the tree.

type Msg

type Msg string

type Node

type Node interface {
	tea.Model
	// Parent should return the parent of the current node, or nil if a root node.
	Parent() Node
	// Children should return a list of Nodes which represent the children of the current node.
	Children() Nodes
	// State should return the annotation for the current node, which are used for computing various display states.
	State() NodeState
}

Node represents the base model for the elements of the Treeish implementation

type NodeState

type NodeState uint16

NodeState is used for passing information from a Treeish element to the view itself

const (
	NodeNone NodeState = 0

	// NodeCollapsed hints that the current node is collapsed
	NodeCollapsed NodeState = 1 << iota
	// NodeSelected hints that the current node should be rendered as selected
	NodeSelected
	// NodeCollapsible hints that the current node can be collapsed
	NodeCollapsible
	// NodeHidden hints that the current node is not going to be displayed
	NodeHidden
	// NodeLastChild shows the node to be the last in the children list
	NodeLastChild

	// NodeIsMultiLine shows if the node should not be truncated to the viewport's max width
	NodeIsMultiLine

	// NodeMaxState serves no other purpose than as a sentinel value for outside Node interface
	// implementations to append their own states.
	NodeMaxState
)

func (NodeState) Is

func (s NodeState) Is(st NodeState) bool

type Nodes

type Nodes []Node

Nodes is a slice of Node elements, usually representing the children of a Node.

func (Nodes) Update

func (n Nodes) Update(msg tea.Msg) tea.Cmd

type Style

type Style lipgloss.Style

func (Style) Render

func (s Style) Render(_ int, strs ...string) string

func (Style) Width

func (s Style) Width(w int) DepthStyler

type Styles

type Styles struct {
	Line     lipgloss.Style
	Selected lipgloss.Style
	Symbol   DepthStyler
}

Styles contains style definitions for this list component. By default, these values are generated by DefaultStyles.

func DefaultStyles

func DefaultStyles() Styles

DefaultStyles returns a set of default style definitions for this tree.

type Symbols

type Symbols struct {
	Connector  string
	Starter    string
	Terminator string
	Horizontal string
}

func DefaultSymbols

func DefaultSymbols() Symbols

DefaultSymbols returns a set of default Symbols for drawing the tree.

func DoubleSymbols

func DoubleSymbols() Symbols

DoubleSymbols returns a symbols comprised of two thin strokes.

func NormalEdgeSymbols

func NormalEdgeSymbols() Symbols

func NormalSymbols

func NormalSymbols() Symbols

NormalSymbols returns a standard-type symbols with a normal weight and 90 degree corners.

func RoundedSymbols

func RoundedSymbols() Symbols

RoundedSymbols returns a symbols with rounded corners.

func ThickEdgeSymbols

func ThickEdgeSymbols() Symbols

func ThickSymbols

func ThickSymbols() Symbols

ThickSymbols returns a symbols that's thicker than the one returned by NormalSymbols.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL