refactor to new tree functions

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-02-05 12:26:05 -06:00
parent 2b3296fd56
commit 3f54db0421
5 changed files with 104 additions and 66 deletions

View File

@ -7,6 +7,15 @@ plugin:
check-git-clean:
@git diff-index --quiet HEAD -- || (echo "Git repository is dirty, please commit your changes first"; exit 1)
# Test the README.md & doc.go file
# this runs pkgsite, the binary that does dev.go.dev
# go install golang.org/x/pkgsite/cmd/pkgsite@latest
pkgsite:
pkgsite
goimports:
goimports -w *.go
redomod:
rm -f go.*
GO111MODULE= go mod init

145
action.go
View File

@ -8,84 +8,99 @@ package main
import (
"go.wit.com/log"
"go.wit.com/toolkits/tree"
"go.wit.com/widget"
)
func doAction(a widget.Action) {
log.Log(INFO, "doAction() START a.ActionType =", a.ActionType)
log.Log(INFO, "doAction() START a.ProgName =", a.ProgName)
if a.ActionType == widget.ToolkitInit {
return
}
log.Log(INFO, "doAction() START a.WidgetId =", a.WidgetId, "a.ParentId =", a.ParentId)
switch a.WidgetType {
case widget.Root:
me.treeRoot = me.myTree.AddNode(&a)
log.Log(INFO, "doAction() found treeRoot")
return
}
switch a.ActionType {
case widget.Add:
me.myTree.AddNode(&a)
return
}
n := me.treeRoot.FindWidgetId(a.WidgetId)
func Add(n *tree.Node) {
log.Log(INFO, "Add() END =", n.WidgetType, n.String())
if n == nil {
log.Warn("FindId() n == nil", a.WidgetId, a.ActionType)
log.Warn("FindId() n == nil", a.WidgetId, a.ActionType)
log.Warn("FindId() n == nil", a.WidgetId, a.ActionType)
log.Warn("Aaaaa!, return")
log.Warn("Tree Error: Add() sent n == nil")
return
}
switch n.WidgetType {
case widget.Root:
me.treeRoot = n
log.Log(INFO, "Add() found treeRoot")
return
}
switch a.ActionType {
log.Log(INFO, "Add() END =", n.WidgetType, n.String())
}
func newaction(n *tree.Node, atype widget.ActionType) {
log.Log(INFO, "newaction() START", atype)
if n == nil {
log.Warn("Tree Error: Add() sent n == nil")
return
}
if n.TK == nil {
log.Warn("Tree sent an action on a widget we didn't seem to have.")
// do this init here again? Probably something
// went wrong and we should reset the our while gocui.View tree
// n.TK = initWidget(n)
}
// w := n.TK.(*guiWidget)
switch atype {
case widget.Show:
n.State.Hidden = true
log.Log(NOW, "Show() HERE. a.Hidden() was =", n.Hidden())
// w.Show()
case widget.Hide:
n.State.Hidden = false
case widget.Enable:
n.State.Enable = true
case widget.Disable:
n.State.Enable = false
case widget.Get:
log.Warn("value =", n.State.Value)
case widget.GetText:
log.Warn("value =", n.String())
case widget.Set:
n.State.Value = a.State.Value
case widget.SetText:
log.Warn("GOT TO SetText()", a.WidgetId)
log.Warn("GOT TO SetText()", a.WidgetId)
log.Warn("GOT TO SetText()", a.WidgetId)
log.Warn("GOT TO SetText()", a.WidgetId)
if n == nil {
log.Warn("HOT DIGGITY. n == nil")
}
n.State.Value = a.State.Value
case widget.AddText:
n.State.Strings = append(a.State.Strings, widget.GetString(a.State.Value))
case widget.Margin:
n.State.Pad = true
case widget.Unmargin:
n.State.Pad = false
case widget.Pad:
n.State.Pad = true
case widget.Unpad:
n.State.Pad = false
case widget.Delete:
log.Warn("doAction() TODO: Delete()")
n.DeleteNode()
log.Log(NOW, "Hide() HERE. a.State.Hidden was =", n.Hidden())
// w.Hide()
case widget.Move:
log.Warn("doAction() TODO: Move()")
log.Log(NOW, "attempt to move() =", atype, n.WidgetType, n.ProgName())
case widget.Enable:
// w.enable = true
// w.enableColor()
case widget.Disable:
// w.enable = false
// w.disableColor()
case widget.Delete:
log.Info("newaction() DeleteNode()")
n.DeleteNode()
case widget.ToolkitClose:
log.Warn("doAction() toolkit closed. are the channels cleand up?")
log.Info("newaction() toolkit closed. are the channels cleand up?")
return
default:
log.Log(ERROR, "doAction() Unknown =", a.ActionType, a.WidgetType)
log.Log(ERROR, "newaction() UNHANDLED Action Type =", atype, "WidgetType =", n.WidgetType, "Name =", n.ProgName())
}
log.Log(INFO, "doAction() END =", a.ActionType, a.WidgetType)
log.Log(INFO, "newaction() END", atype, n.String())
}
func SetTitle(n *tree.Node, s string) {
SetText(n, s)
}
func SetLabel(n *tree.Node, s string) {
SetText(n, s)
}
func SetText(n *tree.Node, s string) {
if n == nil {
log.Warn("Tree Error: Add() sent n == nil")
return
}
if n.TK == nil {
log.Warn("Tree sent an action on a widget we didn't seem to have.")
return
}
// w := n.TK.(*guiWidget)
// w.SetText(s)
log.Info("SetText()", n.WidgetType, n.String())
}
func AddText(n *tree.Node, s string) {
if n == nil {
log.Warn("Tree Error: Add() sent n == nil")
return
}
if n.TK == nil {
log.Warn("Tree sent an action on a widget we didn't seem to have.")
return
}
log.Info("AddText()", n.WidgetType, n.String())
// w := n.TK.(*guiWidget)
// w.AddText(s)
}

11
main.go
View File

@ -17,7 +17,16 @@ func init() {
me.myTree = tree.New()
me.myTree.PluginName = "nocui"
me.myTree.ActionFromChannel = doAction
// me.myTree.ActionFromChannel = doAction
me.myTree.NodeAction = newaction
me.myTree.Add = Add
me.myTree.SetTitle = SetTitle
me.myTree.SetLabel = SetLabel
me.myTree.SetText = SetText
me.myTree.AddText = AddText
me.exit = false
log.Log(INFO, "Init() END")

View File

@ -42,6 +42,9 @@ func simpleStdin() {
}()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if me.exit {
return
}
s := scanner.Text()
s = strings.TrimSuffix(s, "\n")
switch s {

View File

@ -19,4 +19,6 @@ var me config
type config struct {
treeRoot *tree.Node // the base of the binary tree. it should have id == 0
myTree *tree.TreeInfo
exit bool
}