diff options
| author | Jeff Carr <[email protected]> | 2022-11-06 12:59:24 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2022-11-06 12:59:24 -0600 |
| commit | e55fb6675d692e3f44fa67b02b12661e476bd028 (patch) | |
| tree | c16084dea9779f5ef244adb3937d33adb17e7bad /cmds | |
| parent | 099efb6b24caf9eaad50d7386636a7ac23552bde (diff) | |
start trying to make the tookits pluginsv0.4.3
totally minimize helloworld demo
try to make a button plugin example
debug changes
final changes before attempting to use a golang plugin
actually running gocui as a plugin
add gocli-as-plugin example
try to convert the go-cui toolkit into a plugin
doc updates
make a minimal console gui
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'cmds')
| -rw-r--r-- | cmds/buttonAsPlugin/Makefile | 14 | ||||
| -rw-r--r-- | cmds/buttonAsPlugin/main.go | 33 | ||||
| -rw-r--r-- | cmds/console-hello-world/Makefile | 15 | ||||
| -rw-r--r-- | cmds/console-hello-world/keybindings.go | 130 | ||||
| -rw-r--r-- | cmds/console-hello-world/log.go | 35 | ||||
| -rw-r--r-- | cmds/console-hello-world/main.go | 91 | ||||
| -rw-r--r-- | cmds/console-hello-world/newJ.go | 46 | ||||
| -rw-r--r-- | cmds/console-hello-world/views.go | 114 | ||||
| -rw-r--r-- | cmds/gocli-as-plugin/Makefile | 14 | ||||
| -rw-r--r-- | cmds/gocli-as-plugin/log.go | 35 | ||||
| -rw-r--r-- | cmds/gocli-as-plugin/main.go | 59 | ||||
| -rw-r--r-- | cmds/helloworld/main.go | 36 |
12 files changed, 594 insertions, 28 deletions
diff --git a/cmds/buttonAsPlugin/Makefile b/cmds/buttonAsPlugin/Makefile new file mode 100644 index 0000000..431d956 --- /dev/null +++ b/cmds/buttonAsPlugin/Makefile @@ -0,0 +1,14 @@ +run: build + ./buttonAsPlugin + +build-release: + go get -v -u -x . + go build + ./buttonAsPlugin + +build: + GO111MODULE="off" go get -v -x . + GO111MODULE="off" go build + +update: + GO111MODULE="off" go get -v -u -x . diff --git a/cmds/buttonAsPlugin/main.go b/cmds/buttonAsPlugin/main.go new file mode 100644 index 0000000..1c2eeaf --- /dev/null +++ b/cmds/buttonAsPlugin/main.go @@ -0,0 +1,33 @@ +// This is a simple example +package main + +import ( + "log" + "git.wit.org/wit/gui" +) + +func main() { + gui.Main(buttonWindow) +} + +// This creates a window +func buttonWindow() { + var w, g *gui.Node + gui.Config.Title = "Demo Plugin Window" + gui.Config.Width = 640 + gui.Config.Height = 480 + +// gui.Config.Exit = gui.StandardClose +// gui.SetDebug(true) + + w = gui.NewWindow() + g = w.NewGroup("buttonGroup") + + g.NewButton("hello", func () { + log.Println("world") + }) + + g.NewButton("foo", func () { + log.Println("bar") + }) +} diff --git a/cmds/console-hello-world/Makefile b/cmds/console-hello-world/Makefile new file mode 100644 index 0000000..9c3540d --- /dev/null +++ b/cmds/console-hello-world/Makefile @@ -0,0 +1,15 @@ +run: build + ./console-hello-world + reset + ldd ./console-hello-world + +build-release: + go get -v -u -x . + go build + +build: + GO111MODULE="off" go get -v -x . + GO111MODULE="off" go build + +update: + GO111MODULE="off" go get -v -u -x . diff --git a/cmds/console-hello-world/keybindings.go b/cmds/console-hello-world/keybindings.go new file mode 100644 index 0000000..fdac1ff --- /dev/null +++ b/cmds/console-hello-world/keybindings.go @@ -0,0 +1,130 @@ +// Copyright 2014 The gocui Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( +// "errors" +// "fmt" + "log" +// "strings" + + "github.com/awesome-gocui/gocui" +) + +func initKeybindings(g *gocui.Gui) error { + log.Println("got to initKeybindings") + if err := g.SetKeybinding("", 'q', gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit + }); err != nil { + return err + } + if err := g.SetKeybinding("", 'Q', gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeySpace, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return newView(g) + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyBackspace, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return delView(g) + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyBackspace2, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return delView(g) + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + log.Println("tab", v.Name()) + return nextView(g, true) + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyArrowLeft, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return moveView(g, v, -delta, 0) + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyArrowRight, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return moveView(g, v, delta, 0) + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + log.Println("down", v.Name()) + return moveView(g, v, 0, delta) + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + log.Println("up", v.Name()) + return moveView(g, v, 0, -delta) + }); err != nil { + return err + } + if err := g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + log.Println("enter", v.Name()) + return nil + }); err != nil { + return err + } + if err := g.SetKeybinding("", 't', gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + _, err := g.SetViewOnTop(views[curView]) + return err + }); err != nil { + return err + } + if err := g.SetKeybinding("", 'b', gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + _, err := g.SetViewOnBottom(views[curView]) + return err + }); err != nil { + return err + } + if err := g.SetKeybinding("", 'j', gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + return newJ(g) + }); err != nil { + return err + } + if err := g.SetKeybinding("", 'h', gocui.ModNone, + func(g *gocui.Gui, v *gocui.View) error { + log.Println("help", v.Name()) + tmp, _ := g.SetViewOnTop("help") + log.Println("help 2", tmp.Name(), "blah") +// g.SetView("help", 2, 2, 30, 15, 0); + g.SetCurrentView("help") +// moveView(g, tmp, 0, -delta) + if err := g.DeleteView("help"); err != nil { + panic(err) + } + return nil + }); err != nil { + return err + } + return nil +} diff --git a/cmds/console-hello-world/log.go b/cmds/console-hello-world/log.go new file mode 100644 index 0000000..b05beaf --- /dev/null +++ b/cmds/console-hello-world/log.go @@ -0,0 +1,35 @@ +// This creates a simple hello world window +package main + +import ( + "log" + "fmt" + "os" + arg "github.com/alexflint/go-arg" +) + + +var args struct { + Foo string + Bar bool + User string `arg:"env:USER"` + Demo bool `help:"run a demo"` +} + +var f *os.File +var err error + +func init() { + arg.MustParse(&args) + fmt.Println(args.Foo, args.Bar, args.User) + + f, err = os.OpenFile("/tmp/guilogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) + if err != nil { + log.Fatalf("error opening file: %v", err) + } + // hmm. is there a trick here or must this be in main() + // defer f.Close() + + log.SetOutput(f) + log.Println("This is a test log entry") +} diff --git a/cmds/console-hello-world/main.go b/cmds/console-hello-world/main.go new file mode 100644 index 0000000..dd360d5 --- /dev/null +++ b/cmds/console-hello-world/main.go @@ -0,0 +1,91 @@ +// Copyright 2014 The gocui Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "errors" + "fmt" + "log" + + "github.com/awesome-gocui/gocui" +) + +const delta = 1 + +var ( + views = []string{} + curView = -1 + idxView = 0 + currentX = 5 + currentY = 2 + groupSize = 0 + baseGui *gocui.Gui +) + +var helpLabel *gocui.View + +func main() { + // setup log to write to a file +// logInit() + + g, err := gocui.NewGui(gocui.OutputNormal, true) + baseGui = g + if err != nil { + log.Panicln(err) + } + defer g.Close() + + g.Highlight = true + g.SelFgColor = gocui.ColorRed + g.SelFrameColor = gocui.ColorRed + + g.SetManagerFunc(layout) + + if err := initKeybindings(g); err != nil { + log.Panicln(err) + } + if err := newView(g); err != nil { + log.Panicln(err) + } + + addButton("hello") + addButton("world") + addButton("foo") + + addGroup("blank") + addButton("bar") + addButton("bar none") + addButton("bar going") + + addGroup("te") + addButton("world 2") + addButton("foo 2") + + if err := g.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) { + log.Panicln(err) + } +} + +func layout(g *gocui.Gui) error { + var err error + maxX, _ := g.Size() + helpLabel, err = g.SetView("help", maxX-32, 0, maxX-1, 11, 0) + if err != nil { + if !errors.Is(err, gocui.ErrUnknownView) { + return err + } + fmt.Fprintln(helpLabel, "KEYBINDINGS") + fmt.Fprintln(helpLabel, "Enter: Click Button") + fmt.Fprintln(helpLabel, "Tab/Space: Switch Buttons") + fmt.Fprintln(helpLabel, "") + fmt.Fprintln(helpLabel, "h: Help") + fmt.Fprintln(helpLabel, "Backspace: Delete Button") + fmt.Fprintln(helpLabel, "Arrow keys: Move Button") + fmt.Fprintln(helpLabel, "t: Move Button to the top") + fmt.Fprintln(helpLabel, "b: Move Button to the button") + fmt.Fprintln(helpLabel, "Ctrl-C or Q: Exit") + } + return nil +} diff --git a/cmds/console-hello-world/newJ.go b/cmds/console-hello-world/newJ.go new file mode 100644 index 0000000..47c7439 --- /dev/null +++ b/cmds/console-hello-world/newJ.go @@ -0,0 +1,46 @@ +// Copyright 2014 The gocui Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "errors" + "fmt" + "log" + "strings" + + "github.com/awesome-gocui/gocui" +) + +var topX int = 2 +var bottomX int = 20 +var topY int = 2 +var bottomY int = 7 + +func newJ(g *gocui.Gui) error { + // maxX, maxY := g.Size() + name := fmt.Sprintf("jcarr %v test ", idxView) + v, err := g.SetView(name, topX, topY, bottomX, bottomY, 0) + if err == nil { + return err + } + if !errors.Is(err, gocui.ErrUnknownView) { + return err + } + + v.Wrap = true + fmt.Fprintln(v, name) + fmt.Fprintln(v, strings.Repeat("foo\n", 2)) + // fmt.Fprintln(v, strings.Repeat(name+" ", 30)) + log.Println("newJ added a new view", v.Name()) + + if _, err := g.SetCurrentView(name); err != nil { + return err + } + + views = append(views, name) + curView = len(views) - 1 + idxView += 1 + return nil +} diff --git a/cmds/console-hello-world/views.go b/cmds/console-hello-world/views.go new file mode 100644 index 0000000..50287c2 --- /dev/null +++ b/cmds/console-hello-world/views.go @@ -0,0 +1,114 @@ +// Copyright 2014 The gocui Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "errors" + "fmt" + "log" + "strings" + + "github.com/awesome-gocui/gocui" +) + +func addGroup(name string) { + log.Println("addGroup()", name) + currentY = 2 + currentX += groupSize + 6 +} + +func addButton(name string) error { + t := len(name) + v, err := baseGui.SetView(name, currentX, currentY, currentX+t+3, currentY+2, 0) + if err == nil { + return err + } + if !errors.Is(err, gocui.ErrUnknownView) { + return err + } + + v.Wrap = true + fmt.Fprintln(v, " " + name) + fmt.Fprintln(v, strings.Repeat("foo\n", 2)) + + if _, err := baseGui.SetCurrentView(name); err != nil { + return err + } + + views = append(views, name) + curView = len(views) - 1 + idxView += 1 + currentY += 3 + if (groupSize < len(views)) { + groupSize = len(views) + } + return nil +} + +func newView(g *gocui.Gui) error { + maxX, maxY := g.Size() + name := fmt.Sprintf("v%v", idxView) + v, err := g.SetView(name, maxX/2-5, maxY/2-5, maxX/2+5, maxY/2+5, 0) + if err == nil { + return err + } + if !errors.Is(err, gocui.ErrUnknownView) { + return err + } + + v.Wrap = true + fmt.Fprintln(v, strings.Repeat(name+" ", 30)) + + if _, err := g.SetCurrentView(name); err != nil { + return err + } + + views = append(views, name) + curView = len(views) - 1 + idxView += 1 + return nil +} + +func delView(g *gocui.Gui) error { + if len(views) <= 1 { + return nil + } + + if err := g.DeleteView(views[curView]); err != nil { + return err + } + views = append(views[:curView], views[curView+1:]...) + + return nextView(g, false) +} + +func nextView(g *gocui.Gui, disableCurrent bool) error { + next := curView + 1 + if next > len(views)-1 { + next = 0 + } + + if _, err := g.SetCurrentView(views[next]); err != nil { + return err + } + + curView = next + return nil +} + +func moveView(g *gocui.Gui, v *gocui.View, dx, dy int) error { + name := v.Name() + x0, y0, x1, y1, err := g.ViewPosition(name) + if err != nil { + return err + } + log.Println(x0, y0, x1, y1) + if _, err := g.SetView(name, x0+dx, y0+dy, x1+dx, y1+dy, 0); err != nil { + return err + } + x0, y0, x1, y1, err = g.ViewPosition(name) + log.Println(x0, y0, x1, y1) + return nil +} diff --git a/cmds/gocli-as-plugin/Makefile b/cmds/gocli-as-plugin/Makefile new file mode 100644 index 0000000..277d737 --- /dev/null +++ b/cmds/gocli-as-plugin/Makefile @@ -0,0 +1,14 @@ +run: build + ./gocli-as-plugin + # ldd ./gocli-as-plugin + +build-release: + go get -v -u -x . + go build + +build: + GO111MODULE="off" go get -v -x . + GO111MODULE="off" go build + +update: + GO111MODULE="off" go get -v -u -x . diff --git a/cmds/gocli-as-plugin/log.go b/cmds/gocli-as-plugin/log.go new file mode 100644 index 0000000..b05beaf --- /dev/null +++ b/cmds/gocli-as-plugin/log.go @@ -0,0 +1,35 @@ +// This creates a simple hello world window +package main + +import ( + "log" + "fmt" + "os" + arg "github.com/alexflint/go-arg" +) + + +var args struct { + Foo string + Bar bool + User string `arg:"env:USER"` + Demo bool `help:"run a demo"` +} + +var f *os.File +var err error + +func init() { + arg.MustParse(&args) + fmt.Println(args.Foo, args.Bar, args.User) + + f, err = os.OpenFile("/tmp/guilogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) + if err != nil { + log.Fatalf("error opening file: %v", err) + } + // hmm. is there a trick here or must this be in main() + // defer f.Close() + + log.SetOutput(f) + log.Println("This is a test log entry") +} diff --git a/cmds/gocli-as-plugin/main.go b/cmds/gocli-as-plugin/main.go new file mode 100644 index 0000000..a9bd632 --- /dev/null +++ b/cmds/gocli-as-plugin/main.go @@ -0,0 +1,59 @@ +// Copyright 2014 The gocui Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "log" + "os" + + "plugin" +// "github.com/awesome-gocui/gocui" +) + +type Greeter interface { + Greet() +} + +var plugGocli *plugin.Plugin +var plugHello *plugin.Plugin + +func main() { + log.Println("attempt plugin") + + go loadPlugin(plugHello, "../../toolkit/hello.so") + loadPlugin(plugGocli, "../../toolkit/gocli.so") +} + +func loadPlugin(plug *plugin.Plugin, name string) { + // load module + // 1. open the so file to load the symbols + plug, err = plugin.Open(name) + if err != nil { + log.Println(err) + os.Exit(1) + } + + // 2. look up a symbol (an exported function or variable) + // in this case, variable Greeter + symGreeter, err := plug.Lookup("Greeter") + if err != nil { + log.Println(err) + os.Exit(1) + } + + log.Println("symGreater", symGreeter) + + // 3. Assert that loaded symbol is of a desired type + // in this case interface type Greeter (defined above) + // var greeter Greeter + greeter, ok := symGreeter.(Greeter) + if !ok { + log.Println("unexpected type from module symbol") + os.Exit(1) + } + + // 4. use the module + greeter.Greet() +} diff --git a/cmds/helloworld/main.go b/cmds/helloworld/main.go index 3ae3b07..552ecb5 100644 --- a/cmds/helloworld/main.go +++ b/cmds/helloworld/main.go @@ -1,44 +1,24 @@ -// This creates a simple hello world window +// This is a simple example package main import ( - "os" "log" "git.wit.org/wit/gui" ) func main() { - gui.Main(myGUI) + gui.Main(helloworld) } -// This initializes the first window -func myGUI() { +// This creates a window +func helloworld() { var w *gui.Node - gui.Config.Title = "Hello World golang wit/gui Window" + gui.Config.Title = "helloworld golang wit/gui window" gui.Config.Width = 640 gui.Config.Height = 480 - gui.Config.Exit = myExit w = gui.NewWindow() - addHelloWorld(w, "A Simple Tab") + w.NewButton("hello", func () { + log.Println("world") + }) } - -func addHelloWorld(window *gui.Node, title string) { - var newNode, g, tb *gui.Node - - newNode = window.NewTab(title) - - g = newNode.NewGroup("hello") - tb = g.NewTextbox("hello world box") // when debugging, this string will be used - tb.OnChanged = func(*gui.Node) { - s := tb.GetText() - log.Println("text box =", s) - } - tb.SetText("world") -} - -func myExit(n *gui.Node) { - log.Println("exit() here") - os.Exit(0) -} - |
