Skip to content

Commit

Permalink
resolve pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor German authored and cjbassi committed Jul 15, 2019
1 parent d76da5c commit 691e2f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion _examples/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
l := widgets.NewTree()
l.TextStyle = ui.NewStyle(ui.ColorYellow)
l.WrapText = false
l.Nodes = nodes
l.SetNodes(nodes)

x, y := ui.TerminalDimensions()

Expand Down
12 changes: 6 additions & 6 deletions v3/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ type ListTheme struct {
}

type TreeTheme struct {
Text Style
Collapse rune
Expand rune
Text Style
Collapsed rune
Expanded rune
}

type ParagraphTheme struct {
Expand Down Expand Up @@ -130,9 +130,9 @@ var Theme = RootTheme{
},

Tree: TreeTheme{
Text: NewStyle(ColorWhite),
Collapse: COLLAPSED,
Expand: EXPANDED,
Text: NewStyle(ColorWhite),
Collapsed: COLLAPSED,
Expanded: EXPANDED,
},

StackedBarChart: StackedBarChartTheme{
Expand Down
32 changes: 20 additions & 12 deletions widgets/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type TreeNode struct {
Expanded bool
Nodes []*TreeNode

//level stores the node level in the tree.
// level stores the node level in the tree.
level int
}

Expand All @@ -32,9 +32,9 @@ func (self *TreeNode) parseStyles(style Style) []Cell {
} else {
sb.WriteString(strings.Repeat(treeIndent, self.level))
if self.Expanded {
sb.WriteRune(Theme.Tree.Expand)
sb.WriteRune(Theme.Tree.Expanded)
} else {
sb.WriteRune(Theme.Tree.Collapse)
sb.WriteRune(Theme.Tree.Collapsed)
}
sb.WriteByte(' ')
}
Expand All @@ -45,15 +45,15 @@ func (self *TreeNode) parseStyles(style Style) []Cell {
// Tree is a tree widget.
type Tree struct {
Block
Nodes []*TreeNode
TextStyle Style
SelectedRowStyle Style
WrapText bool
SelectedRow int
topRow int

//rows is flatten nodes for rendering.
rows []*TreeNode
nodes []*TreeNode
// rows is flatten nodes for rendering.
rows []*TreeNode
topRow int
}

// NewTree creates a new Tree widget.
Expand All @@ -66,9 +66,14 @@ func NewTree() *Tree {
}
}

func (self *Tree) SetNodes(nodes []*TreeNode) {
self.nodes = nodes
self.prepareNodes()
}

func (self *Tree) prepareNodes() {
self.rows = self.rows[:0]
for _, node := range self.Nodes {
self.rows = make([]*TreeNode, 0)
for _, node := range self.nodes {
self.prepareNode(node, 0)
}
}
Expand All @@ -85,7 +90,7 @@ func (self *Tree) prepareNode(node *TreeNode, level int) {
}

func (self *Tree) Walk(fn TreeWalkFn) {
for _, n := range self.Nodes {
for _, n := range self.nodes {
if !self.walk(n, fn) {
break
}
Expand All @@ -108,8 +113,6 @@ func (self *Tree) walk(n *TreeNode, fn TreeWalkFn) bool {

func (self *Tree) Draw(buf *Buffer) {
self.Block.Draw(buf)
self.prepareNodes()

point := self.Inner.Min

// adjusts view into widget
Expand Down Expand Up @@ -209,20 +212,23 @@ func (self *Tree) ScrollBottom() {

func (self *Tree) Collapse() {
self.rows[self.SelectedRow].Expanded = false
self.prepareNodes()
}

func (self *Tree) Expand() {
node := self.rows[self.SelectedRow]
if len(node.Nodes) > 0 {
self.rows[self.SelectedRow].Expanded = true
}
self.prepareNodes()
}

func (self *Tree) ToggleExpand() {
node := self.rows[self.SelectedRow]
if len(node.Nodes) > 0 {
node.Expanded = !node.Expanded
}
self.prepareNodes()
}

func (self *Tree) ExpandAll() {
Expand All @@ -232,11 +238,13 @@ func (self *Tree) ExpandAll() {
}
return true
})
self.prepareNodes()
}

func (self *Tree) CollapseAll() {
self.Walk(func(n *TreeNode) bool {
n.Expanded = false
return true
})
self.prepareNodes()
}

0 comments on commit 691e2f0

Please sign in to comment.