From ed382bec55be25039e4dcf020d1512139855c9bb Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 9 Nov 2022 08:38:50 -0600 Subject: cleanup of the example commands. more plugin work add a button in two plugins at the same time! added a button in andlabs/ui & gocui attempt a common widget struct between wit/gui & the plugins start handling missing plugins rename Makefile command examples remote examples used for testing filename improvements golang src code uses lowercase and _ in filenames fix crash when button click function == nil fix wrong name 'gocli' -> 'gocui' keep fighting with goreadme generated README.md Signed-off-by: Jeff Carr --- cmds/buttonAsPlugin/Makefile | 14 ---- cmds/buttonAsPlugin/log.go | 64 --------------- cmds/buttonAsPlugin/main.go | 54 ------------- cmds/buttonplugin/Makefile | 14 ++++ cmds/buttonplugin/log.go | 64 +++++++++++++++ cmds/buttonplugin/main.go | 49 +++++++++++ cmds/console-hello-world/Makefile | 15 ---- cmds/console-hello-world/keybindings.go | 130 ------------------------------ cmds/console-hello-world/log.go | 35 -------- cmds/console-hello-world/main.go | 91 --------------------- cmds/console-hello-world/newJ.go | 46 ----------- cmds/console-hello-world/views.go | 114 -------------------------- cmds/console-ui-helloworld/Makefile | 15 ++++ cmds/console-ui-helloworld/keybindings.go | 130 ++++++++++++++++++++++++++++++ cmds/console-ui-helloworld/log.go | 35 ++++++++ cmds/console-ui-helloworld/main.go | 83 +++++++++++++++++++ cmds/console-ui-helloworld/newJ.go | 46 +++++++++++ cmds/console-ui-helloworld/views.go | 114 ++++++++++++++++++++++++++ cmds/consolemouse/Makefile | 14 ---- cmds/consolemouse/gocui.go | 51 ------------ cmds/consolemouse/main.go | 52 ------------ cmds/debug/Makefile | 5 ++ cmds/debug/helloworld.go | 20 +++++ cmds/debug/main.go | 52 ++++++++++++ cmds/gocli-as-plugin/Makefile | 14 ---- cmds/gocli-as-plugin/log.go | 35 -------- cmds/gocli-as-plugin/main.go | 59 -------------- cmds/gui-demo/Makefile | 5 -- cmds/gui-demo/main.go | 56 ------------- 29 files changed, 627 insertions(+), 849 deletions(-) delete mode 100644 cmds/buttonAsPlugin/Makefile delete mode 100644 cmds/buttonAsPlugin/log.go delete mode 100644 cmds/buttonAsPlugin/main.go create mode 100644 cmds/buttonplugin/Makefile create mode 100644 cmds/buttonplugin/log.go create mode 100644 cmds/buttonplugin/main.go delete mode 100644 cmds/console-hello-world/Makefile delete mode 100644 cmds/console-hello-world/keybindings.go delete mode 100644 cmds/console-hello-world/log.go delete mode 100644 cmds/console-hello-world/main.go delete mode 100644 cmds/console-hello-world/newJ.go delete mode 100644 cmds/console-hello-world/views.go create mode 100644 cmds/console-ui-helloworld/Makefile create mode 100644 cmds/console-ui-helloworld/keybindings.go create mode 100644 cmds/console-ui-helloworld/log.go create mode 100644 cmds/console-ui-helloworld/main.go create mode 100644 cmds/console-ui-helloworld/newJ.go create mode 100644 cmds/console-ui-helloworld/views.go delete mode 100644 cmds/consolemouse/Makefile delete mode 100644 cmds/consolemouse/gocui.go delete mode 100644 cmds/consolemouse/main.go create mode 100644 cmds/debug/Makefile create mode 100644 cmds/debug/helloworld.go create mode 100644 cmds/debug/main.go delete mode 100644 cmds/gocli-as-plugin/Makefile delete mode 100644 cmds/gocli-as-plugin/log.go delete mode 100644 cmds/gocli-as-plugin/main.go delete mode 100644 cmds/gui-demo/Makefile delete mode 100644 cmds/gui-demo/main.go (limited to 'cmds') diff --git a/cmds/buttonAsPlugin/Makefile b/cmds/buttonAsPlugin/Makefile deleted file mode 100644 index d9a67bf..0000000 --- a/cmds/buttonAsPlugin/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -run: build - ./buttonAsPlugin >/tmp/buttonAsPlugin.log 2>&1 - -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/log.go b/cmds/buttonAsPlugin/log.go deleted file mode 100644 index 642ff7b..0000000 --- a/cmds/buttonAsPlugin/log.go +++ /dev/null @@ -1,64 +0,0 @@ -// This creates a simple hello world window -package main - -import ( - "log" - "fmt" - "os" - "io" - "time" - "bufio" - 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 f1 *os.File -var f2 *os.File -var err error - -func init() { - arg.MustParse(&args) - fmt.Println(args.Foo, args.Bar, args.User) - - f1, 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(f1) - log.Println("This is a test log entry") -} - -func captureSTDOUT() { - f2, _ = os.OpenFile("/tmp/my.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664) - multiWriter := io.MultiWriter(os.Stderr, f2) - rd, wr, err := os.Pipe() - if err != nil { - os.Exit(1) - } - - // overwrite os.Stdout - os.Stderr = wr - - go func() { - scanner := bufio.NewScanner(rd) - for scanner.Scan() { - stdoutLine := scanner.Text() - multiWriter.Write([]byte(stdoutLine + "\n")) - } - }() - - fmt.Println("foobar") - - // hacky sleep to ensure the go routine can write before program exits - time.Sleep(time.Second) -} diff --git a/cmds/buttonAsPlugin/main.go b/cmds/buttonAsPlugin/main.go deleted file mode 100644 index e26c75e..0000000 --- a/cmds/buttonAsPlugin/main.go +++ /dev/null @@ -1,54 +0,0 @@ -// This is a simple example -package main - -import ( - "log" - "git.wit.org/wit/gui" -) - -func main() { - // go loadPlugin(plugHello, "../../toolkit/hello.so") - - // this doesn't seem to work - captureSTDOUT() - - // go loadPlugin("../../toolkit/gocli.so") - 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 - - w = gui.NewWindow() - g = w.NewGroup("buttonGroup") - - g.NewButton("hello", func () { - log.Println("world") - }) - - /* - g.NewButton("LoadPlugin()", func () { - log.Println("world") - gui.LoadPlugin("../../toolkit/gocli.so") - }) - */ - - g.NewButton("RunGreet()", func () { - log.Println("world") - go gui.RunGreet() - }) - - g.NewButton("gui.LookupJcarrButton()", func () { - log.Println("gui.LookupJcarrButton()") - gui.LookupJcarrButton() - }) - - g.NewButton("gui.GocuiAddButton()", func () { - log.Println("gui.GocuiAddButton()") - gui.GocuiAddButton("new foobar") - }) -} diff --git a/cmds/buttonplugin/Makefile b/cmds/buttonplugin/Makefile new file mode 100644 index 0000000..cf60369 --- /dev/null +++ b/cmds/buttonplugin/Makefile @@ -0,0 +1,14 @@ +run: build + ./buttonplugin >/tmp/buttonplugin.log 2>&1 + +build-release: + go get -v -u -x . + go build + ./buttonplugin + +build: + GO111MODULE="off" go get -v -x . + GO111MODULE="off" go build + +update: + GO111MODULE="off" go get -v -u -x . diff --git a/cmds/buttonplugin/log.go b/cmds/buttonplugin/log.go new file mode 100644 index 0000000..642ff7b --- /dev/null +++ b/cmds/buttonplugin/log.go @@ -0,0 +1,64 @@ +// This creates a simple hello world window +package main + +import ( + "log" + "fmt" + "os" + "io" + "time" + "bufio" + 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 f1 *os.File +var f2 *os.File +var err error + +func init() { + arg.MustParse(&args) + fmt.Println(args.Foo, args.Bar, args.User) + + f1, 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(f1) + log.Println("This is a test log entry") +} + +func captureSTDOUT() { + f2, _ = os.OpenFile("/tmp/my.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664) + multiWriter := io.MultiWriter(os.Stderr, f2) + rd, wr, err := os.Pipe() + if err != nil { + os.Exit(1) + } + + // overwrite os.Stdout + os.Stderr = wr + + go func() { + scanner := bufio.NewScanner(rd) + for scanner.Scan() { + stdoutLine := scanner.Text() + multiWriter.Write([]byte(stdoutLine + "\n")) + } + }() + + fmt.Println("foobar") + + // hacky sleep to ensure the go routine can write before program exits + time.Sleep(time.Second) +} diff --git a/cmds/buttonplugin/main.go b/cmds/buttonplugin/main.go new file mode 100644 index 0000000..cd4d770 --- /dev/null +++ b/cmds/buttonplugin/main.go @@ -0,0 +1,49 @@ +// This is a simple example +package main + +import ( + "log" + "strconv" + "git.wit.org/wit/gui" +) + +func main() { + // this doesn't seem to work + captureSTDOUT() + + gui.Main(buttonWindow) +} + +var counter int = 10 + +// 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 + + w = gui.NewWindow() + g = w.NewGroup("buttonGroup") + + g.NewButton("hello", func () { + log.Println("world") + }) + + g.NewButton("RunGreet()", func () { + log.Println("world") + go gui.RunGreet() + }) + + g.NewButton("gui.LookupJcarrButton()", func () { + log.Println("gui.LookupJcarrButton()") + gui.LookupJcarrButton() + }) + + g.NewButton("new foobar 2", func () { + log.Println("new foobar 2. Adding button 'foobar 3'") + name := "foobar " + strconv.Itoa(counter) + counter += 1 + g.NewButton(name, nil) + }) +} diff --git a/cmds/console-hello-world/Makefile b/cmds/console-hello-world/Makefile deleted file mode 100644 index 9c3540d..0000000 --- a/cmds/console-hello-world/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index fdac1ff..0000000 --- a/cmds/console-hello-world/keybindings.go +++ /dev/null @@ -1,130 +0,0 @@ -// 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 deleted file mode 100644 index b05beaf..0000000 --- a/cmds/console-hello-world/log.go +++ /dev/null @@ -1,35 +0,0 @@ -// 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 deleted file mode 100644 index dd360d5..0000000 --- a/cmds/console-hello-world/main.go +++ /dev/null @@ -1,91 +0,0 @@ -// 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 deleted file mode 100644 index 47c7439..0000000 --- a/cmds/console-hello-world/newJ.go +++ /dev/null @@ -1,46 +0,0 @@ -// 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 deleted file mode 100644 index 50287c2..0000000 --- a/cmds/console-hello-world/views.go +++ /dev/null @@ -1,114 +0,0 @@ -// 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/console-ui-helloworld/Makefile b/cmds/console-ui-helloworld/Makefile new file mode 100644 index 0000000..f63c8a9 --- /dev/null +++ b/cmds/console-ui-helloworld/Makefile @@ -0,0 +1,15 @@ +run: build + ./console-ui-helloworld + reset + ldd ./console-ui-helloworld + +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-ui-helloworld/keybindings.go b/cmds/console-ui-helloworld/keybindings.go new file mode 100644 index 0000000..fdac1ff --- /dev/null +++ b/cmds/console-ui-helloworld/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-ui-helloworld/log.go b/cmds/console-ui-helloworld/log.go new file mode 100644 index 0000000..b05beaf --- /dev/null +++ b/cmds/console-ui-helloworld/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-ui-helloworld/main.go b/cmds/console-ui-helloworld/main.go new file mode 100644 index 0000000..d16b805 --- /dev/null +++ b/cmds/console-ui-helloworld/main.go @@ -0,0 +1,83 @@ +// 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") + + addGroup("blank") + addButton("world") + + 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-ui-helloworld/newJ.go b/cmds/console-ui-helloworld/newJ.go new file mode 100644 index 0000000..47c7439 --- /dev/null +++ b/cmds/console-ui-helloworld/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-ui-helloworld/views.go b/cmds/console-ui-helloworld/views.go new file mode 100644 index 0000000..50287c2 --- /dev/null +++ b/cmds/console-ui-helloworld/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/consolemouse/Makefile b/cmds/consolemouse/Makefile deleted file mode 100644 index 7061e7e..0000000 --- a/cmds/consolemouse/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -run: build - ./consolemouse - reset - -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/consolemouse/gocui.go b/cmds/consolemouse/gocui.go deleted file mode 100644 index 7da888e..0000000 --- a/cmds/consolemouse/gocui.go +++ /dev/null @@ -1,51 +0,0 @@ -// This creates a simple hello world window -package main - -import ( - "log" - "time" - "git.wit.org/wit/gui" -) - -import toolkit "git.wit.org/wit/gui/toolkit/gocui" - -func configureGogui() { - toolkit.Init() - toolkit.OnExit(mycallback) -} - -func startGogui() { - toolkit.StartConsoleMouse() -} - -func mycallback(name string) { - log.Println("run andlabs here? name =", name) - if (name == "andlabs") { - go gui.Main(initGUI) - } - if (name == "something") { - log.Println("add something to do here") - } - if (name == "DemoToolkitWindow") { - gui.Queue( func () { - gui.DemoToolkitWindow() - }) - } - if (name == "addDemoTab") { - gui.Queue( func () { - addDemoTab(w, "A Tab from gocui") - }) - } - if (name == "DebugWindow") { - log.Println("Opening a Debug Window via the gui.Queue()") - gui.Config.Width = 800 - gui.Config.Height = 300 - gui.Config.Exit = myExit - gui.Queue(gui.DebugWindow) - time.Sleep(1 * time.Second) - gui.Queue(gui.DebugTab) - } - if (name == "exit") { - myExit(nil) - } -} diff --git a/cmds/consolemouse/main.go b/cmds/consolemouse/main.go deleted file mode 100644 index 3f4ce58..0000000 --- a/cmds/consolemouse/main.go +++ /dev/null @@ -1,52 +0,0 @@ -// This creates a simple hello world window -package main - -import ( - "os" - "log" -// "time" - "git.wit.org/wit/gui" -) - -import toolkit "git.wit.org/wit/gui/toolkit/gocui" - -var w *gui.Node - -func main() { - go gui.Main(initGUI) - - configureGogui() - startGogui() -} - -// This initializes the first window -func initGUI() { - gui.Config.Title = "Hello World golang wit/gui Window" - gui.Config.Width = 640 - gui.Config.Height = 480 - gui.Config.Exit = myExit - - w = gui.NewWindow() - w.Dump() - addDemoTab(w, "A Simple Tab Demo") - addDemoTab(w, "A Second Tab") -} - -func addDemoTab(w *gui.Node, title string) { - var newNode, g *gui.Node - - newNode = w.NewTab(title) - - g = newNode.NewGroup("group 1") - - dd := g.NewDropdown("demoCombo2") - dd.AddDropdown("more 1") - dd.AddDropdown("less 2") - dd.AddDropdown("foo 3") -} - -func myExit(n *gui.Node) { - log.Println("You can Do exit() things here") - toolkit.Exit() - os.Exit(0) -} diff --git a/cmds/debug/Makefile b/cmds/debug/Makefile new file mode 100644 index 0000000..25c994d --- /dev/null +++ b/cmds/debug/Makefile @@ -0,0 +1,5 @@ +run: build + ./debug + +build: + go build diff --git a/cmds/debug/helloworld.go b/cmds/debug/helloworld.go new file mode 100644 index 0000000..d0998bf --- /dev/null +++ b/cmds/debug/helloworld.go @@ -0,0 +1,20 @@ +// A simple helloworld window +package main + +import ( + "log" + "git.wit.org/wit/gui" +) + +// This creates a window +func helloworld() { + var w *gui.Node + gui.Config.Title = "helloworld golang wit/gui window" + gui.Config.Width = 400 + gui.Config.Height = 100 + + w = gui.NewWindow() + w.NewButton("hello", func () { + log.Println("world") + }) +} diff --git a/cmds/debug/main.go b/cmds/debug/main.go new file mode 100644 index 0000000..5e3a350 --- /dev/null +++ b/cmds/debug/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "log" + "os" + "time" + + "git.wit.org/wit/gui" +) + +// This initializes the first window +// +// BUG: THIS PROGRAM DOESN'T EXIT PROPERLY (NOT REALLY A BUG) +// +// Then starts a goroutine to demonstrate how to +// inject things into the GUI +func main() { + log.Println("Starting my Control Panel") + + go gui.Main(helloworld) +// go gui.DemoToolkitWindow() + + watchGUI() +} + +// This demonstrates how to properly interact with the GUI +// You can not involke the GUI from external goroutines in most cases. +func watchGUI() { + var i = 1 + for { + log.Println("Waiting", i, "seconds") + i += 1 + time.Sleep(1 * time.Second) + if i == 2 { + log.Println("Opening a Debug Window via the gui.Queue()") + gui.Config.Width = 800 + gui.Config.Height = 300 + gui.Config.Exit = myExit + gui.Queue(gui.DebugWindow) + time.Sleep(1 * time.Second) + gui.Queue(gui.DebugTab) + } + } +} + +// TODO: myExit isn't getting used anymore +func myExit(n *gui.Node) { + log.Println() + log.Println("Entered myExit() on node.Name =", n.Name) + log.Println() + os.Exit(0) +} diff --git a/cmds/gocli-as-plugin/Makefile b/cmds/gocli-as-plugin/Makefile deleted file mode 100644 index 277d737..0000000 --- a/cmds/gocli-as-plugin/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -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 deleted file mode 100644 index b05beaf..0000000 --- a/cmds/gocli-as-plugin/log.go +++ /dev/null @@ -1,35 +0,0 @@ -// 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 deleted file mode 100644 index a9bd632..0000000 --- a/cmds/gocli-as-plugin/main.go +++ /dev/null @@ -1,59 +0,0 @@ -// 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/gui-demo/Makefile b/cmds/gui-demo/Makefile deleted file mode 100644 index 41fdd10..0000000 --- a/cmds/gui-demo/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -run: build - ./gui-demo - -build: - go build diff --git a/cmds/gui-demo/main.go b/cmds/gui-demo/main.go deleted file mode 100644 index 54ad449..0000000 --- a/cmds/gui-demo/main.go +++ /dev/null @@ -1,56 +0,0 @@ -package main - -import ( - "log" - "os" - "time" - - "git.wit.org/wit/gui" -) - -// This initializes the first window -// -// BUG: THIS PROGRAM DOESN'T EXIT PROPERLY (NOT REALLY A BUG) -// -// Then starts a goroutine to demonstrate how to -// inject things into the GUI -func main() { - log.Println("Starting my Control Panel") - - go gui.Main(initGUI) - - watchGUI() -} - -// This initializes the first window -func initGUI() { - gui.DemoToolkitWindow() -} - -// This demonstrates how to properly interact with the GUI -// You can not involke the GUI from external goroutines in most cases. -func watchGUI() { - var i = 1 - for { - log.Println("Waiting", i, "seconds") - i += 1 - time.Sleep(1 * time.Second) - if i == 2 { - log.Println("Opening a Debug Window via the gui.Queue()") - gui.Config.Width = 800 - gui.Config.Height = 300 - gui.Config.Exit = myExit - gui.Queue(gui.DebugWindow) - time.Sleep(1 * time.Second) - gui.Queue(gui.DebugTab) - } - } -} - -// TODO: myExit isn't getting used anymore -func myExit(n *gui.Node) { - log.Println() - log.Println("Entered myExit() on node.Name =", n.Name) - log.Println() - os.Exit(0) -} -- cgit v1.2.3