panels

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

README

Go-Data-Panels

Quick tree data panels for your go project with windows

Why

Ever had some data produced by your go program that you wanted to display nicely? Fed up of going through console output trying to pick out what you need? This is the tool for you.

Organise related data in hierarchical trees so you don't have to do see everything at once, and organise these trees into different views so you only see what you want to see, with the rest a few keystrokes away.

How do I get started?

Create your menu items
items := []*panels.MenuItem{
    {Name: "Foo", Details: "Foo Bar Baz", Shortcut: 'f'},
    {Name: "Other Menu Item", Details: "Some other string", 'o'},
    // etc...
}

In each item, there is a Name, Details, and a Shortcut. When the Menu is in focus, pressing the shortcut key has the same effect as hitting enter when the option is highlighted.

Create your data trees

For each menu item you create, you must create a corresponding DataTree for it. It will be associated with the menu item visible in items.

func makeFooTree() *panels.DataTree {
    tree := panels.NewDataTree("The foo tree")

    for i := 0; i < 10; i++ {
        tree.AddChild(fmt.Sprintf("%d", i))
    }

    return &tree
}

func makeOtherTree() *panels.DataTree {
    tree := panels.NewDataTree("Another tree")

    level1 := tree.AddChild("1st Level")
    level2 := level1.AddChild("2nd Level")

    return &tree
    // you can go down to as many levels as you want - not that you should
}

// ....

fooTree := makeFooTree()
otherTree := makeOtherTree()

trees := []*panels.DataTree{fooTree, otherTree}
See your data
gui := panels.MakeGui({
    MenuItems: items,
    DataViews: trees,
})

gui.Run()

You should now have a menu consisting of 2 items. Clicking on either of them shows the respective data tree. The component in focus is denoted by a red outline. Initially, the menu is always in focus. To cycle focus, press Tab and the focus switch from the menu to the data tree. You can naviage the tree with arrow keys, and if a given node has children, enter will hide / unhide them.

That's good, what about seing multiple things

You can simply add another data tree to the current screen (up to a max of 4) by pressing Ctrl-N and selecting the name of the data view you want to see (yes you can add duplicates of the same widget, and they will share the same state).

To close a window, press Ctrl-C which will close the most recently opened one.

That's it, basic, functional data visualization with very little code needed.

Full Example
package main

import (
    "fmt"

    "github.com/lspaccatrosi16/go-data-panels"
)

func main() {
    items := []*panels.MenuItem{
        {Name: "Foo", Details: "Foo Bar Baz", Shortcut: 'f'},
        {Name: "Other Menu Item", Details: "Some other string", 'o'},
    }

    fooTree := makeFooTree()
    otherTree := makeOtherTree()

    trees := []*panels.DataTree{fooTree, otherTree}

    gui := panels.MakeGui({
        MenuItems: items,
        DataViews: trees,
    })

    gui.Run()
}

func makeFooTree() *panels.DataTree {
    tree := panels.NewDataTree("The foo tree")

    for i := 0; i < 10; i++ {
        tree.AddChild(fmt.Sprintf("%d", i))
    }

    return &tree
}

func makeOtherTree() *panels.DataTree {
    tree := panels.NewDataTree("Another tree")

    level1 := tree.AddChild("1st Level")
    level2 := level1.AddChild("2nd Level")

    return &tree
}

Credits

This is based off of tview, and uses it under the hood. For more functionally complex guis, it should be used instead. This project is essentialy a series of tview presets combined with basic window and state management in a nice wrapper. In the future, I may allow custom tview components to be used as well, but that would likely increase the complexity significantly.

Licence

This project is availible under the Apache-2.0 licence. See LICENCE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataTree

type DataTree struct {
	// contains filtered or unexported fields
}

The overall data tree. Has one (implicit) root child and many sub children

func NewDataTree

func NewDataTree(name string) *DataTree

Make a new data tree. The name is the name of the root node

func (*DataTree) AddChild

func (t *DataTree) AddChild(text string) *TreeNode

Add a child of the root node

func (*DataTree) InheritTree added in v0.0.4

func (t *DataTree) InheritTree(subTree *DataTree) *TreeNode

Add a sub-tree to the root node

type GuiContext

type GuiContext interface {
	// Runs the Gui when it has been createed
	Run() error
	// Stops the Gui (causes Run to return)
	Stop()
}

The Gui container and handler

func MakeGui

func MakeGui(data GuiData) GuiContext

Creates a gui based off of the data provided

type GuiData

type GuiData struct {
	// Text that can be displayed above the widgets at all times
	TopFrameText string

	// Text that can be displayed below the widgets at all times
	BottomFrameText string

	// A list of menu items
	MenuItems []*MenuItem

	// A list of data views
	DataViews []*DataTree
}

Data that the gui needs

type MenuItem struct {
	// The name of the item
	Name string

	// Details attached to the item
	Details string

	// A shortcut key that will be active when the menu is in focus
	Shortcut rune
}

A menu item

type TreeNode

type TreeNode struct {
	// contains filtered or unexported fields
}

A part of the data tree. Each tree can contain as many children as desired.

func (*TreeNode) AddChild

func (t *TreeNode) AddChild(text string) *TreeNode

Add a child of a node to the data tree

func (*TreeNode) InheritTree added in v0.0.3

func (t *TreeNode) InheritTree(subTree *DataTree) *TreeNode

Add a sub-tree to the node of the data tree

Jump to

Keyboard shortcuts

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