Skip to content

Commit

Permalink
Merge pull request #380 from keithknott26/treeview
Browse files Browse the repository at this point in the history
Adding Treeview Widget
  • Loading branch information
mum4k authored Sep 24, 2024
2 parents db8e463 + 25bcdea commit 9b96347
Show file tree
Hide file tree
Showing 6 changed files with 1,841 additions and 2 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ go run widgets/segmentdisplay/segmentdisplaydemo/segmentdisplaydemo.go

[<img src="./doc/images/segmentdisplaydemo.gif" alt="segmentdisplaydemo" type="image/gif">](widgets/segmentdisplay/segmentdisplaydemo/segmentdisplaydemo.go)

## The TreeView

Displays a tree view which provides a hierarchical and collapsible view for displaying and interacting with nested data structures.
[treeviewdemo](widgets/treeview/treeviewdemo/treeviewdemo.go).

```go
go run widgets/treeview/treeviewdemo/treeviewdemo.go
```

[<img src="./doc/images/treeviewdemo.gif" alt="treeviewdemo" type="image/gif">](widgets/treeview/treeviewdemo/treeviewdemo.go)

# Contributing

If you are willing to contribute, improve the infrastructure or develop a
Expand Down Expand Up @@ -219,8 +230,8 @@ Termdash uses [this branching model](https://nvie.com/posts/a-successful-git-bra
- [perfstat](https://github.com/flaviostutz/perfstat): Analyze and show tips about possible bottlenecks in Linux systems.
- [gex](https://github.com/Tosch110/gex): Cosmos SDK explorer in-terminal.
- [ali](https://github.com/nakabonne/ali): ALI HTTP load testing tool with realtime analysis.
- [suimon](https://github.com/bartosian/suimon): SUI blockchain explorer and monitor.

- [suimon](https://github.com/bartosian/suimon): SUI blockchain explorer and monitor.
# Disclaimer

This is not an official Google product.

Binary file added doc/images/treeviewdemo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 128 additions & 0 deletions widgets/treeview/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package treeview

import "github.com/mum4k/termdash/cell"

// Option represents a configuration option for the TreeView.
type Option func(*options)

// options holds the configuration for the TreeView.
type options struct {
// nodes are the root nodes of the TreeView.
nodes []*TreeNode
// labelColor is the color of the node labels.
labelColor cell.Color
// expandedIcon is the icon used for expanded nodes.
expandedIcon string
// collapsedIcon is the icon used for collapsed nodes.
collapsedIcon string
// leafIcon is the icon used for leaf nodes.
leafIcon string
// indentation is the number of spaces per indentation level.
indentation int
// waitingIcons are the icons used for the spinner.
waitingIcons []string
// truncate indicates whether to truncate long labels.
truncate bool
// enableLogging enables or disables logging for debugging.
enableLogging bool
}

// newOptions initializes default options.
func newOptions() *options {
return &options{
nodes: []*TreeNode{},
labelColor: cell.ColorWhite,
expandedIcon: "▼",
collapsedIcon: "▶",
leafIcon: "→",
waitingIcons: []string{"◐", "◓", "◑", "◒"},
truncate: false,
indentation: 2, // Default indentation
}
}

// Nodes sets the root nodes of the TreeView.
func Nodes(nodes ...*TreeNode) Option {
return func(o *options) {
o.nodes = nodes
}
}

// Indentation sets the number of spaces for each indentation level.
func Indentation(spaces int) Option {
return func(o *options) {
o.indentation = spaces
}
}

// Icons sets custom icons for expanded, collapsed, and leaf nodes.
func Icons(expanded, collapsed, leaf string) Option {
return func(o *options) {
o.expandedIcon = expanded
o.collapsedIcon = collapsed
o.leafIcon = leaf
}
}

// LabelColor sets the color of the node labels.
func LabelColor(color cell.Color) Option {
return func(o *options) {
o.labelColor = color
}
}

// WaitingIcons sets the icons for the spinner.
func WaitingIcons(icons []string) Option {
return func(o *options) {
o.waitingIcons = icons
}
}

// Truncate enables or disables label truncation.
func Truncate(truncate bool) Option {
return func(o *options) {
o.truncate = truncate
}
}

// EnableLogging enables or disables logging for debugging.
func EnableLogging(enable bool) Option {
return func(o *options) {
o.enableLogging = enable
}
}

// Label sets the widget's label.
// Note: If the widget's label is managed by the container, this can be a no-op.
func Label(label string) Option {
return func(o *options) {
// No action needed; label is set in container's BorderTitle.
}
}

// CollapsedIcon sets the icon for collapsed nodes.
func CollapsedIcon(icon string) Option {
return func(o *options) {
o.collapsedIcon = icon
}
}

// ExpandedIcon sets the icon for expanded nodes.
func ExpandedIcon(icon string) Option {
return func(o *options) {
o.expandedIcon = icon
}
}

// LeafIcon sets the icon for leaf nodes.
func LeafIcon(icon string) Option {
return func(o *options) {
o.leafIcon = icon
}
}

// IndentationPerLevel sets the indentation per level.
// Alias to Indentation for compatibility with demo code.
func IndentationPerLevel(spaces int) Option {
return Indentation(spaces)
}
Loading

0 comments on commit 9b96347

Please sign in to comment.