teatree

package
v0.0.0-...-881f335 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 8 Imported by: 0

README

TeaTree

This is a tree "bubble" designed to be compatible with charm.sh bubbletea packages.

Operations:

  • Add items to main tree
  • Add items to items
  • When adding an item, you can specify the icon.
    • Perhaps when doing a View() operation, the item can call an interface function to get its icon. This would allow clients to specify their own state icons. Should there be some animation support? Or is that crazy?
  • Items can be opened or closed if they have children
  • There should be help, though actually I guess what shows up in the help should be up to the client application. But some standard functions should exist:
    • Select (return) -- called when the user hits return on a field. Used for picking something from a hierarchy. Should the "Select" function be opt-in or opt-out? Should it do something by default, or should it do something only if a user has configured it to?
    • Open/Close
    • Cursor Up/Down - move the selection:
      • up (previous sibling, or if at the parent, the previous sibling of the parent)
      • down (next sibling, or if at the end of the tree, we need to return to the caller that the user has tried to select down from us)
    • Cursor Right/Left - actually I won't capture these, so they could be executed by the calling application

To Do

When rendering a tree item, it should be rendered and then if there are any children, they should be rendered. The item needs to pass the amount of lines still remaining in the view. Somehow I need to know how many lines the children used.

Is it possible to count the lines in a returned view string? If so, then I can just use that.

Documentation

Index

Constants

View Source
const ChevronDown = "\U000F0140"
View Source
const ChevronRight = "\U000F0142"
View Source
const NoChevron = " "

This is the material design icon in the nerdfont/material symbols set, as found in https://pictogrammers.com/library/mdi/

Variables

This section is empty.

Functions

func New

func New() tea.Model

Types

type ItemHolder

type ItemHolder interface {
	GetItems() []*TreeItem
	// GetPath - recursively search through the parent hierarchy and return the name of each item
	// They will be ordered from oldest ancestor to most recent descendant. The item itself will
	// be the last one in the list
	GetPath() []string
	AddChildren(...*TreeItem) ItemHolder
	GetParent() ItemHolder
	Refresh() // This tells the item holder to delete all of its children and re-read them.
}

type KeyMap

type KeyMap struct {
	Space    key.Binding
	GoToTop  key.Binding
	GoToLast key.Binding
	Down     key.Binding
	Up       key.Binding
	PageUp   key.Binding
	PageDown key.Binding
	Back     key.Binding
	Open     key.Binding
	Select   key.Binding
}

func DefaultKeyMap

func DefaultKeyMap() KeyMap

type Tree

type Tree struct {
	sync.Mutex
	Viewtop              int // for scrolling
	Width                int
	Height               int
	ClosedChildrenSymbol string
	OpenChildrenSymbol   string
	ActiveItem           *TreeItem
	ActiveLine           int         // Which line, (from 0..Height) is the cursor on?
	Items                []*TreeItem `json:"-"`

	Style  lipgloss.Style
	KeyMap KeyMap
	// contains filtered or unexported fields
}

func (*Tree) AddChildren

func (t *Tree) AddChildren(i ...*TreeItem) ItemHolder

func (*Tree) Blur

func (t *Tree) Blur() tea.Cmd

func (*Tree) CountVisibleItems

func (t *Tree) CountVisibleItems() int

func (*Tree) Error

func (t *Tree) Error() error

func (*Tree) Focus

func (t *Tree) Focus() tea.Cmd

func (*Tree) GetItems

func (t *Tree) GetItems() []*TreeItem

func (*Tree) GetKey

func (t *Tree) GetKey() string

GetKey returns the field's key.

func (*Tree) GetParent

func (t *Tree) GetParent() ItemHolder

Returning nil here means you can't go "up" outside of the tree widget, so if this widget is embedded with others, this will prevent getting out of the tree.

func (*Tree) GetPath

func (t *Tree) GetPath() []string

This GetPath() represents the path from the root of the tree to here. Since this IS the root of the tree, then it must return nothing.

func (*Tree) GetValue

func (t *Tree) GetValue() any

GetValue returns the field's value.

func (*Tree) Init

func (t *Tree) Init() tea.Cmd

func (*Tree) KeyBinds

func (t *Tree) KeyBinds() []key.Binding

func (*Tree) Refresh

func (t *Tree) Refresh()

func (*Tree) Run

func (t *Tree) Run() error

Run runs the field individually.

func (*Tree) ScrollDown

func (t *Tree) ScrollDown(n int)

ScrollDown moves the "display" area down the virtual list. This actually looks like scrolling up ((the items move up the screen) Not sure if this is counterintuitive or not

func (*Tree) ScrollUp

func (t *Tree) ScrollUp(n int)

func (*Tree) SelectFirst

func (t *Tree) SelectFirst()

func (*Tree) SelectLast

func (t *Tree) SelectLast()

func (*Tree) SelectNext

func (t *Tree) SelectNext()

SelectNext is like SelectPrevious, but the other way

func (*Tree) SelectPrevious

func (t *Tree) SelectPrevious()

SelectPrevious - selects the previous TreeItem. This involves first getting the parent and then telling the parent to select the previous item from the current selection. If we're already at the first child, then we go to the grandparent and select the previous parent item from us, and then we descend to the most open child and activate that.rune

func (*Tree) SetActive

func (t *Tree) SetActive(ti *TreeItem)

func (*Tree) Skip

func (t *Tree) Skip() bool

Skip returns whether this input should be skipped or not.

func (*Tree) ToggleChild

func (t *Tree) ToggleChild()

ToggleChild will toggle the open/closed state of the current selection. This only has meaning if there are actually children

func (*Tree) Update

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

func (*Tree) View

func (t *Tree) View() string

func (*Tree) WithAccessible

func (t *Tree) WithAccessible(accessible bool) huh.Field

WithAccessible sets the accessible mode of the input field.

func (*Tree) WithHeight

func (t *Tree) WithHeight(height int) huh.Field

WithHeight sets the height of the input field.

func (*Tree) WithKeyMap

func (t *Tree) WithKeyMap(k *huh.KeyMap) huh.Field

WithKeyMap sets the keymap on an input field.

func (*Tree) WithPosition

func (t *Tree) WithPosition(p huh.FieldPosition) huh.Field

WithPosition sets the position of the input field.

func (*Tree) WithTheme

func (t *Tree) WithTheme(theme *huh.Theme) huh.Field

WithTheme sets the theme on a field.

func (*Tree) WithWidth

func (t *Tree) WithWidth(width int) huh.Field

WithWidth sets the width of the input field.

type TreeItem

type TreeItem struct {
	sync.Mutex

	Name            string
	Children        []*TreeItem
	CanHaveChildren bool // CanHaveChildren: By setting this to True, you say that this item can have children. This allows for the implementation of a lazy loader, when you supply an Open() function. This affects how the item is rendered.
	Open            bool
	Data            interface{}
	OpenFunc        func(*TreeItem) `json:"-"`
	CloseFunc       func(*TreeItem) `json:"-"`
	// contains filtered or unexported fields
}

func NewItem

func NewItem(name string, canHaveChildren bool, children []*TreeItem, icon func(*TreeItem) string, labelStyle, iconStyle func(*TreeItem) lipgloss.Style, openFunc, closeFunc func(*TreeItem), data interface{}) *TreeItem

func (*TreeItem) AddChildren

func (ti *TreeItem) AddChildren(children ...*TreeItem) ItemHolder

AddChildren - adds a list of children to an item, and then returns the item AddChild - adds a child item to the item. Adding a child will result in the automatic inclusion of the collapse chevron

func (*TreeItem) CloseChildren

func (ti *TreeItem) CloseChildren()

func (*TreeItem) CountItemAndChildren

func (ti *TreeItem) CountItemAndChildren() int

CountItemAndChildren - returns the count of this item plus any visible children.

func (*TreeItem) GetItems

func (ti *TreeItem) GetItems() []*TreeItem

func (*TreeItem) GetParent

func (ti *TreeItem) GetParent() ItemHolder

func (*TreeItem) GetPath

func (ti *TreeItem) GetPath() []string

func (*TreeItem) Icon

func (ti *TreeItem) Icon() string

func (*TreeItem) IconStyle

func (ti *TreeItem) IconStyle() lipgloss.Style

func (*TreeItem) Init

func (ti *TreeItem) Init() tea.Cmd

func (*TreeItem) LabelStyle

func (ti *TreeItem) LabelStyle() lipgloss.Style

func (*TreeItem) OpenChildren

func (ti *TreeItem) OpenChildren()

func (*TreeItem) Refresh

func (ti *TreeItem) Refresh()

func (*TreeItem) SelectLast

func (ti *TreeItem) SelectLast()

SelectLast - starting from the current Item, descend in the last child of the last child and set that as the active item. Also need to set the topline ff

func (*TreeItem) SelectNext

func (ti *TreeItem) SelectNext()

We're being told to select the next item relative to our current position.

func (*TreeItem) SelectPrevious

func (ti *TreeItem) SelectPrevious()

SelectPrevious - this is being invoked on a TreeItem that is currently selected and the user wants to move up to the previous selection. This will involve recursively going up the tree until we find the one to select or we get to the top

func (*TreeItem) SetSelectFunc

func (ti *TreeItem) SetSelectFunc(sf func(*TreeItem))

func (*TreeItem) ToggleChildren

func (ti *TreeItem) ToggleChildren()

func (*TreeItem) Update

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

func (*TreeItem) View

func (ti *TreeItem) View() string

func (*TreeItem) ViewScrolled

func (ti *TreeItem) ViewScrolled(viewtop, curline, bottomline int) (int, string)

Jump to

Keyboard shortcuts

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