From db0986fa06245a1173aa3a3b654e7df25117acdc Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 12 Feb 2025 15:43:58 -0600 Subject: updates for a standard plugin code file --- config.go | 116 -------------------------------------------------- init.go | 1 + plugin.go | 3 -- structs.go | 35 ++++++--------- toolkitConfig.save.go | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ widget.proto | 31 ++++++++------ 6 files changed, 148 insertions(+), 154 deletions(-) delete mode 100644 config.go create mode 100644 toolkitConfig.save.go diff --git a/config.go b/config.go deleted file mode 100644 index a68b562..0000000 --- a/config.go +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright 2025 WIT.COM Inc Licensed GPL 3.0 - -package tree - -// functions to import and export the protobuf -// data to and from config files - -import ( - "errors" - "fmt" - "os" - "path/filepath" - - "go.wit.com/log" -) - -// load the ~/.config/forge/ files -func configLoad() *ToolkitConfigs { - if os.Getenv("FORGE_CONFIG") == "" { - homeDir, _ := os.UserHomeDir() - fullpath := filepath.Join(homeDir, ".config/forge") - os.Setenv("FORGE_CONFIG", fullpath) - } - - c, err := loadText() - if err != nil { - log.Info("gui toolkit configLoad() error", err) - } - if c != nil { - return c - } - - // first time user. make a template config file - c = sampleConfig() - return c -} - -// makes a sample config (and saves it) -func sampleConfig() *ToolkitConfigs { - all := NewToolkitConfigs() - new1 := new(ToolkitConfig) - new1.Plugin = "tree" - new1.Name = "test" - new1.Value = "apple" - all.Append(new1) - - all.configSave() - - fmt.Println("first time user. adding an example config file with", len(all.ToolkitConfigs), "repos") - return all -} - -// write to ~/.config/forge/ unless ENV{FORGE_CONFIG} is set -func (c *ToolkitConfigs) configSave() error { - s := c.FormatTEXT() - configWrite("toolkit.text", []byte(s)) - return nil -} - -func loadText() (*ToolkitConfigs, error) { - // this lets the user hand edit the config - data, err := loadFile("toolkit.text") - if err != nil { - return nil, err - } - if data == nil { - return nil, fmt.Errorf("toolkit.text data was nil") - } - if len(data) == 0 { - return nil, fmt.Errorf("toolkit.text was empty") - } - - c := new(ToolkitConfigs) - - // attempt to marshal toolkit.text - if err := c.UnmarshalTEXT(data); err != nil { - return nil, err - } - log.Log(TREE, "ConfigLoad()", len(c.ToolkitConfigs), "entries in ~/.config/forge") - return c, nil -} - -func loadFile(filename string) ([]byte, error) { - fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename) - data, err := os.ReadFile(fullname) - if errors.Is(err, os.ErrNotExist) { - // if file does not exist, just return nil. this - // will cause ConfigLoad() to try the next config file like "toolkit.text" - // because the user might want to edit the .config by hand - return nil, nil - } - if err != nil { - // log.Info("open config file :", err) - return nil, err - } - return data, nil -} - -func configWrite(filename string, data []byte) error { - fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename) - - cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - defer cfgfile.Close() - if err != nil { - log.Warn("open config file :", err) - return err - } - if filename == "toolkit.text" { - // add header - cfgfile.Write([]byte("\n")) - cfgfile.Write([]byte("# you can customize your GUI toolkit user settings here\n")) - cfgfile.Write([]byte("\n")) - } - cfgfile.Write(data) - return nil -} diff --git a/init.go b/init.go index 0aaba86..0a94b3e 100644 --- a/init.go +++ b/init.go @@ -36,6 +36,7 @@ func (me *TreeInfo) newAction(a widget.Action) { // log.Log(TREEWARN, "tree.FindWidgetId() n == nil. A bug in your application?") return } + switch a.ActionType { case widget.SetText: log.Log(TREE, "tree.SetText() a.State.CurrentS =", a.State.CurrentS) diff --git a/plugin.go b/plugin.go index 1498880..61ae47f 100644 --- a/plugin.go +++ b/plugin.go @@ -6,9 +6,6 @@ package tree There are some helper functions that are probably going to be the same everywhere. Mostly due to handling the binary tree structure and the channel communication - - For now, it's just a symlink to the 'master' version in - ./toolkit/nocui/common.go */ import ( diff --git a/structs.go b/structs.go index eb76f39..c4792b7 100644 --- a/structs.go +++ b/structs.go @@ -17,27 +17,20 @@ import ( var treeRoot *Node type TreeInfo struct { - PluginName string - config *ToolkitConfigs - - // this is the channel we send user events like - // mouse clicks or keyboard events back to the program - callback chan widget.Action - - // this is the channel we get requests to make widgets - pluginChan chan widget.Action - - // NodeI interface{} - - // ActionFromChannel func(widget.Action) - NodeAction func(*Node, widget.ActionType) - Add func(*Node) - AddText func(*Node, string) - SetText func(*Node, string) - SetTitle func(*Node, string) - SetLabel func(*Node, string) - SetChecked func(*Node, bool) - ToolkitClose func() + PluginName string // used to identify the plugin + config *ToolkitConfigs // protobuf of plugin settings + callback chan widget.Action // mouse clicks or keyboard events back to the program + pluginChan chan widget.Action // this is the channel we get requests to make widgets + NodeAction func(*Node, widget.ActionType) // deprecate + Add func(*Node) // add a new widget + AddText func(*Node, string) // add a string to a dropdown widget + SetText func(*Node, string) // set the text of a widget + SetTitle func(*Node, string) // update the title of a window or tab + SetLabel func(*Node, string) // update the "label" (aka "Name") for a widget + SetChecked func(*Node, bool) // set the state of a checkbox + ToolkitClose func() // shutdown and unload the plugin + ShowTable func(*Node) // attempt at sending a whole table + // NodeI interface{} // is an interface useful here? } type Node struct { diff --git a/toolkitConfig.save.go b/toolkitConfig.save.go new file mode 100644 index 0000000..a68b562 --- /dev/null +++ b/toolkitConfig.save.go @@ -0,0 +1,116 @@ +// Copyright 2025 WIT.COM Inc Licensed GPL 3.0 + +package tree + +// functions to import and export the protobuf +// data to and from config files + +import ( + "errors" + "fmt" + "os" + "path/filepath" + + "go.wit.com/log" +) + +// load the ~/.config/forge/ files +func configLoad() *ToolkitConfigs { + if os.Getenv("FORGE_CONFIG") == "" { + homeDir, _ := os.UserHomeDir() + fullpath := filepath.Join(homeDir, ".config/forge") + os.Setenv("FORGE_CONFIG", fullpath) + } + + c, err := loadText() + if err != nil { + log.Info("gui toolkit configLoad() error", err) + } + if c != nil { + return c + } + + // first time user. make a template config file + c = sampleConfig() + return c +} + +// makes a sample config (and saves it) +func sampleConfig() *ToolkitConfigs { + all := NewToolkitConfigs() + new1 := new(ToolkitConfig) + new1.Plugin = "tree" + new1.Name = "test" + new1.Value = "apple" + all.Append(new1) + + all.configSave() + + fmt.Println("first time user. adding an example config file with", len(all.ToolkitConfigs), "repos") + return all +} + +// write to ~/.config/forge/ unless ENV{FORGE_CONFIG} is set +func (c *ToolkitConfigs) configSave() error { + s := c.FormatTEXT() + configWrite("toolkit.text", []byte(s)) + return nil +} + +func loadText() (*ToolkitConfigs, error) { + // this lets the user hand edit the config + data, err := loadFile("toolkit.text") + if err != nil { + return nil, err + } + if data == nil { + return nil, fmt.Errorf("toolkit.text data was nil") + } + if len(data) == 0 { + return nil, fmt.Errorf("toolkit.text was empty") + } + + c := new(ToolkitConfigs) + + // attempt to marshal toolkit.text + if err := c.UnmarshalTEXT(data); err != nil { + return nil, err + } + log.Log(TREE, "ConfigLoad()", len(c.ToolkitConfigs), "entries in ~/.config/forge") + return c, nil +} + +func loadFile(filename string) ([]byte, error) { + fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename) + data, err := os.ReadFile(fullname) + if errors.Is(err, os.ErrNotExist) { + // if file does not exist, just return nil. this + // will cause ConfigLoad() to try the next config file like "toolkit.text" + // because the user might want to edit the .config by hand + return nil, nil + } + if err != nil { + // log.Info("open config file :", err) + return nil, err + } + return data, nil +} + +func configWrite(filename string, data []byte) error { + fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename) + + cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + defer cfgfile.Close() + if err != nil { + log.Warn("open config file :", err) + return err + } + if filename == "toolkit.text" { + // add header + cfgfile.Write([]byte("\n")) + cfgfile.Write([]byte("# you can customize your GUI toolkit user settings here\n")) + cfgfile.Write([]byte("\n")) + } + cfgfile.Write(data) + return nil +} diff --git a/widget.proto b/widget.proto index 371c288..25265a7 100644 --- a/widget.proto +++ b/widget.proto @@ -5,32 +5,35 @@ syntax = "proto3"; package main; import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp +import "google/protobuf/any.proto"; // Import 'Any' + message Size { - int64 width = 1; - int64 height = 2; + int64 width = 1; + int64 height = 2; } message Location { - int64 x = 1; - int64 y = 2; + int64 x = 1; + int64 y = 2; } message Tree { - Widget parent = 1; - repeated Widget children = 2; + Widget parent = 1; + repeated Widget children = 2; } message Widget { - int64 id = 1; - string name = 2; - Size size = 3; - Location location = 4; - string color = 5; + int64 id = 1; + string name = 2; + Size size = 3; + Location location = 4; + string color = 5; + google.protobuf.Any TK = 6; } message Widgets { - string uuid = 1; // `autogenpb:uuid:0331fcd7-3c8c-43e4-be1b-77db6a6bc58c` - string version = 2; // `autogenpb:version:v1` - repeated Widget Widgets = 3; + string uuid = 1; // `autogenpb:uuid:0331fcd7-3c8c-43e4-be1b-77db6a6bc58c` + string version = 2; // `autogenpb:version:v1` + repeated Widget Widgets = 3; } -- cgit v1.2.3