gocui: better output handling

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-04-27 00:10:23 -05:00
parent 17b59b7e7e
commit b789fbbe56
7 changed files with 60 additions and 32 deletions

View File

@ -139,7 +139,7 @@ Creates a window helpful for debugging this package
`func ShowDebugValues()` `func ShowDebugValues()`
### func [StandardExit](/main.go#L191) ### func [StandardExit](/main.go#L197)
`func StandardExit()` `func StandardExit()`
@ -180,7 +180,7 @@ var Config GuiConfig
The Node is a binary tree. This is how all GUI elements are stored The Node is a binary tree. This is how all GUI elements are stored
simply the name and the size of whatever GUI element exists simply the name and the size of whatever GUI element exists
#### func [New](/main.go#L162) #### func [New](/main.go#L168)
`func New() *Node` `func New() *Node`

View File

@ -19,7 +19,7 @@ var myButton *Node
Creates a window helpful for debugging this package Creates a window helpful for debugging this package
*/ */
func DebugWindow() { func DebugWindow() {
bugWin = Config.rootNode.NewWindow("go.wit.org/gui debug window").DebugTab("Debug Tab") bugWin = Config.rootNode.NewWindow("go.wit.com/gui debug window").DebugTab("Debug Tab")
bugWin.Custom = bugWin.StandardClose bugWin.Custom = bugWin.StandardClose
// bugWin.DebugTab("Debug Tab") // bugWin.DebugTab("Debug Tab")
} }

12
main.go
View File

@ -53,13 +53,19 @@ func watchCallback() {
log(logNow, "watchCallback() restarted select for toolkit user events") log(logNow, "watchCallback() restarted select for toolkit user events")
select { select {
case a := <-Config.guiChan: case a := <-Config.guiChan:
n := Config.rootNode.FindId(a.WidgetId)
if (a.ActionType == toolkit.UserQuit) { if (a.ActionType == toolkit.UserQuit) {
log(logNow, "doUserEvent() node =", n.id, n.Name, "User sent Quit()") log(logNow, "doUserEvent() User sent Quit()")
n.doCustom() Config.rootNode.doCustom()
exit("wit/gui toolkit.UserQuit") exit("wit/gui toolkit.UserQuit")
break break
} }
if (a.ActionType == toolkit.EnableDebug) {
log(logNow, "doUserEvent() Enable Debugging Window")
DebugWindow()
break
}
n := Config.rootNode.FindId(a.WidgetId)
if (n == nil) { if (n == nil) {
log(logError, "watchCallback() UNKNOWN widget id =", a.WidgetId, a.Name) log(logError, "watchCallback() UNKNOWN widget id =", a.WidgetId, a.Name)
} else { } else {

View File

@ -7,6 +7,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"strings"
"github.com/awesome-gocui/gocui" "github.com/awesome-gocui/gocui"
) )
@ -15,33 +16,45 @@ func addHelp() {
me.baseGui.SetManagerFunc(helplayout) me.baseGui.SetManagerFunc(helplayout)
} }
var helpText []string = []string{"KEYBINDINGS",
"",
"d: show/hide debugging",
"h: hide widgets",
"s: show all widgets",
"q: quit()",
"p: panic()",
"o: show Stdout",
"l: log to /tmp/witgui.log",
"Ctrl-D: Enable Debugging",
"Ctrl-C: Exit",
"",
}
func helplayout(g *gocui.Gui) error { func helplayout(g *gocui.Gui) error {
var err error var err error
maxX, _ := g.Size() maxX, _ := g.Size()
help, err := g.SetView("help", maxX-32, 0, maxX-1, 13, 0) var newW int = 8
for _, s := range(helpText) {
if newW < len(s) {
newW = len(s)
}
}
help, err := g.SetView("help", maxX-(newW + me.FramePadW), 0, maxX-1, len(helpText) + me.FramePadH, 0)
if err != nil { if err != nil {
if !errors.Is(err, gocui.ErrUnknownView) { if !errors.Is(err, gocui.ErrUnknownView) {
return err return err
} }
help.SelBgColor = gocui.ColorGreen help.SelBgColor = gocui.ColorGreen
help.SelFgColor = gocui.ColorBlack help.SelFgColor = gocui.ColorBlack
fmt.Fprintln(help, "KEYBINDINGS")
// fmt.Fprintln(help, "Enter: Click Button") // fmt.Fprintln(help, "Enter: Click Button")
// fmt.Fprintln(help, "Tab/Space: Switch Buttons") // fmt.Fprintln(help, "Tab/Space: Switch Buttons")
fmt.Fprintln(help, "")
// fmt.Fprintln(help, "h: Help")
// fmt.Fprintln(help, "Backspace: Delete Button") // fmt.Fprintln(help, "Backspace: Delete Button")
// fmt.Fprintln(help, "Arrow keys: Move Button") // fmt.Fprintln(help, "Arrow keys: Move Button")
// fmt.Fprintln(help, "t: Move Button to the top")
// fmt.Fprintln(help, "b: Move Button to the button") fmt.Fprintln(help, strings.Join(helpText, "\n"))
fmt.Fprintln(help, "d: show/hide debugging")
fmt.Fprintln(help, "h: hide widgets")
fmt.Fprintln(help, "s: show all widgets")
fmt.Fprintln(help, "q: quit()")
fmt.Fprintln(help, "p: panic()")
fmt.Fprintln(help, "STDOUT: /tmp/witgui.log")
// fmt.Fprintln(help, "Ctrl-C: Exit") // TODO: fix ctrl-c handling
if _, err := g.SetCurrentView("help"); err != nil { if _, err := g.SetCurrentView("help"); err != nil {
return err return err
} }

View File

@ -6,6 +6,7 @@ package main
import ( import (
"github.com/awesome-gocui/gocui" "github.com/awesome-gocui/gocui"
"git.wit.org/wit/gui/toolkit"
) )
func defaultKeybindings(g *gocui.Gui) error { func defaultKeybindings(g *gocui.Gui) error {
@ -83,6 +84,13 @@ func addDebugKeys(g *gocui.Gui) {
standardExit() standardExit()
return nil return nil
}) })
g.SetKeybinding("", gocui.KeyCtrlD, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
var a toolkit.Action
a.ActionType = toolkit.EnableDebug
me.callback <- a
return nil
})
// panic // panic
g.SetKeybinding("", 'p', gocui.ModNone, g.SetKeybinding("", 'p', gocui.ModNone,

View File

@ -204,20 +204,19 @@ func (w *cuiWidget) Write(p []byte) (n int, err error) {
me.writeMutex.Lock() me.writeMutex.Lock()
defer me.writeMutex.Unlock() defer me.writeMutex.Unlock()
if (me.logStdout.v == nil) { if (me.logStdout.v == nil) {
// optionally write the output to /tmp
fmt.Fprintln(outf, string(p)) fmt.Fprintln(outf, string(p))
v, _ := me.baseGui.View("msg") v, _ := me.baseGui.View("msg")
if (v != nil) { if (v != nil) {
fmt.Fprintln(outf, "found msg") fmt.Fprintln(outf, "found msg")
me.logStdout.v = v me.logStdout.v = v
} }
return } else {
} // display the output in the gocui window
me.logStdout.v.Clear() me.logStdout.v.Clear()
// fmt.Fprintln(w.v, p + "jcarr")
// log(logNow, "widget.Write()", p)
s := fmt.Sprint(string(p)) s := fmt.Sprint(string(p))
s = "jwc " + strconv.Itoa(len(outputS)) + " " + strings.TrimSuffix(s, "\n") s = strings.TrimSuffix(s, "\n")
tmp := strings.Split(s, "\n") tmp := strings.Split(s, "\n")
outputS = append(outputS, tmp...) outputS = append(outputS, tmp...)
if (len(outputS) > outputH) { if (len(outputS) > outputH) {
@ -225,6 +224,7 @@ func (w *cuiWidget) Write(p []byte) (n int, err error) {
outputS = outputS[l:] outputS = outputS[l:]
} }
fmt.Fprintln(me.logStdout.v, strings.Join(outputS, "\n")) fmt.Fprintln(me.logStdout.v, strings.Join(outputS, "\n"))
}
return len(p), nil return len(p), nil
} }

View File

@ -95,6 +95,7 @@ const (
InitToolkit // initializes the toolkit InitToolkit // initializes the toolkit
CloseToolkit // closes the toolkit CloseToolkit // closes the toolkit
UserQuit // the user closed the GUI UserQuit // the user closed the GUI
EnableDebug // open the debugging window
) )
func (s WidgetType) String() string { func (s WidgetType) String() string {