Skip to content

Commit

Permalink
chore: make DefaultKeyMap a func
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr committed Oct 25, 2024
1 parent 20b545b commit c997845
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 69 deletions.
13 changes: 7 additions & 6 deletions tree/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ type Node struct {
// value is the root value of the node
value any

// TODO: expose a getter for this in lipgloss?
rootStyle lipgloss.Style

opts *itemOptions

// TODO: move to lipgloss.Tree?
Expand Down Expand Up @@ -163,6 +160,8 @@ func (t *Node) ItemStyle(s lipgloss.Style) *Node {
// }
// return lipgloss.NewStyle().Foreground(dimColor)
// })
//
// TODO: currently unused as this is set in the Styles struct.
func (t *Node) ItemStyleFunc(f StyleFunc) *Node {
t.tree.ItemStyleFunc(func(children ltree.Children, i int) lipgloss.Style {
c := make(Nodes, children.Length())
Expand All @@ -177,7 +176,6 @@ func (t *Node) ItemStyleFunc(f StyleFunc) *Node {

// TODO: support IndentStyleFunc in lipgloss so we can have a full background for the item

// TODO: should we re-export RoundedEnumerator from lipgloss?
// Enumerator sets the enumerator implementation. This can be used to change the
// way the branches indicators look. Lipgloss includes predefined enumerators
// for a classic or rounded tree. For example, you can have a rounded tree:
Expand Down Expand Up @@ -218,6 +216,7 @@ func (t *Node) Indenter(indenter ltree.Indenter) *Node {
// EnumeratorStyle sets a static style for all enumerators.
//
// Use EnumeratorStyleFunc to conditionally set styles based on the tree node.
// TODO: currently unused as this is set in the Styles struct.
func (t *Node) EnumeratorStyle(style lipgloss.Style) *Node {
t.tree.EnumeratorStyle(style)
return t
Expand All @@ -233,6 +232,8 @@ func (t *Node) EnumeratorStyle(style lipgloss.Style) *Node {
// }
// return lipgloss.NewStyle().Foreground(dimColor)
// })
//
// TODO: currently unused as this is set in the Styles struct.
func (t *Node) EnumeratorStyleFunc(f func(children ltree.Children, i int) lipgloss.Style) *Node {
t.tree.EnumeratorStyleFunc(f)
return t
Expand Down Expand Up @@ -262,7 +263,7 @@ func (t *Node) Child(children ...any) *Node {
t.size = t.size + child.size
t.tree.Child(child)

// Close the node again as the number of children as changed
// Close the node again as the number of children has changed
if t.initialClosed {
t.Close()
}
Expand All @@ -275,7 +276,7 @@ func (t *Node) Child(children ...any) *Node {
t.size = t.size + item.size
t.tree.Child(item)

// Close the node again as the number of children as changed
// Close the node again as the number of children has changed
if t.initialClosed {
t.Close()
}
Expand Down
124 changes: 61 additions & 63 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,69 +106,70 @@ type KeyMap struct {
Quit key.Binding
}

// DefaultKeyMap is the default set of key bindings for navigating and acting
// DefaultKeyMap returns the default set of key bindings for navigating and acting
// upon the tree.
var DefaultKeyMap = KeyMap{
Down: key.NewBinding(
func DefaultKeyMap() KeyMap {
return KeyMap{Down: key.NewBinding(
key.WithKeys("down", "j", "ctrl+n"),
key.WithHelp("↓/j", "down"),
),
Up: key.NewBinding(
key.WithKeys("up", "k", "ctrl+p"),
key.WithHelp("↑/k", "up"),
),
PageDown: key.NewBinding(
key.WithKeys("pgdown", spacebar, "f"),
key.WithHelp("f/pgdn", "page down"),
),
PageUp: key.NewBinding(
key.WithKeys("pgup", "b"),
key.WithHelp("b/pgup", "page up"),
),
HalfPageDown: key.NewBinding(
key.WithKeys("d", "ctrl+d"),
key.WithHelp("d", "½ page down"),
),
HalfPageUp: key.NewBinding(
key.WithKeys("u", "ctrl+u"),
key.WithHelp("u", "½ page up"),
),
GoToTop: key.NewBinding(
key.WithKeys("g", "home"),
key.WithHelp("g", "top"),
),
GoToBottom: key.NewBinding(
key.WithKeys("G", "end"),
key.WithHelp("G", "bottom"),
),
Up: key.NewBinding(
key.WithKeys("up", "k", "ctrl+p"),
key.WithHelp("↑/k", "up"),
),
PageDown: key.NewBinding(
key.WithKeys("pgdown", spacebar, "f"),
key.WithHelp("f/pgdn", "page down"),
),
PageUp: key.NewBinding(
key.WithKeys("pgup", "b"),
key.WithHelp("b/pgup", "page up"),
),
HalfPageDown: key.NewBinding(
key.WithKeys("d", "ctrl+d"),
key.WithHelp("d", "½ page down"),
),
HalfPageUp: key.NewBinding(
key.WithKeys("u", "ctrl+u"),
key.WithHelp("u", "½ page up"),
),
GoToTop: key.NewBinding(
key.WithKeys("g", "home"),
key.WithHelp("g", "top"),
),
GoToBottom: key.NewBinding(
key.WithKeys("G", "end"),
key.WithHelp("G", "bottom"),
),

Toggle: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("⏎", "toggle"),
),
Open: key.NewBinding(
key.WithKeys("l", "right"),
key.WithHelp("→/l", "open"),
),
Close: key.NewBinding(
key.WithKeys("h", "left"),
key.WithHelp("←/h", "close"),
),
Toggle: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("⏎", "toggle"),
),
Open: key.NewBinding(
key.WithKeys("l", "right"),
key.WithHelp("→/l", "open"),
),
Close: key.NewBinding(
key.WithKeys("h", "left"),
key.WithHelp("←/h", "close"),
),

// Toggle help.
ShowFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "more"),
),
CloseFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "close help"),
),
// Toggle help.
ShowFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "more"),
),
CloseFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "close help"),
),

Quit: key.NewBinding(
key.WithKeys("q", "ctrl+c"),
key.WithHelp("q", "quit"),
),
Quit: key.NewBinding(
key.WithKeys("q", "ctrl+c"),
key.WithHelp("q", "quit"),
),
}
}

// Model is the Bubble Tea model for this tree element.
Expand Down Expand Up @@ -208,7 +209,7 @@ type Model struct {
// New creates a new model with default settings.
func New(t *Node, width, height int) Model {
m := Model{
KeyMap: DefaultKeyMap,
KeyMap: DefaultKeyMap(),
OpenCharacter: "▼",
ClosedCharacter: "▶",
CursorCharacter: "→",
Expand Down Expand Up @@ -375,14 +376,13 @@ func (m *Model) toggleNode(node *Node, open bool) {
func (m *Model) updateViewport(movement int) {
m.yOffset = max(min(m.root.size-1, m.yOffset+movement), 0)
m.updateStyles()
m.viewport.Style = lipgloss.NewStyle().Background(m.styles.TreeStyle.GetBackground())

cursor := m.cursorView()
m.viewport.SetContent(
lipgloss.JoinHorizontal(
lipgloss.Top,
m.styles.TreeStyle.UnsetPaddingRight().UnsetMarginRight().Render(cursor),
m.styles.TreeStyle.UnsetPaddingLeft().UnsetMarginLeft().Render(m.root.String()),
cursor,
m.styles.TreeStyle.Render(m.root.String()),
),
)

Expand Down Expand Up @@ -626,9 +626,7 @@ func (m *Model) NodeAtCurrentOffset() *Node {
// style function's closure again
func (m *Model) updateStyles() {
// TODO: add RootStyleFunc to the Node interface?
m.root.rootStyle = m.rootStyle()
m.root.RootStyle(m.root.rootStyle)
// m.root.ItemStyleFunc(m.selectedNodeStyle())
m.root.RootStyle(m.rootStyle())

items := m.FlatNodes()
opts := &itemOptions{
Expand Down

0 comments on commit c997845

Please sign in to comment.