diff options
| author | Jeff Carr <[email protected]> | 2023-04-26 20:56:25 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2023-04-26 20:56:25 -0500 |
| commit | e3cf42f1da9ec0ed07a7f346a0ce9c9ff10295c1 (patch) | |
| tree | 0fe6fe28f11ccb3d1d6a989349071928f4a7ccb9 | |
| parent | 5b217fa23af4e5040d5f09d9840ddc426a315960 (diff) | |
gocui: log() output works
Signed-off-by: Jeff Carr <[email protected]>
| -rw-r--r-- | README-goreadme.md | 4 | ||||
| -rw-r--r-- | log/log.go | 5 | ||||
| -rw-r--r-- | main.go | 9 | ||||
| -rw-r--r-- | toolkit/gocui/click.go | 8 | ||||
| -rw-r--r-- | toolkit/gocui/keybindings.go | 4 | ||||
| -rw-r--r-- | toolkit/gocui/log.go | 17 | ||||
| -rw-r--r-- | toolkit/gocui/main.go | 26 | ||||
| -rw-r--r-- | toolkit/gocui/mouse.go | 3 | ||||
| -rw-r--r-- | toolkit/gocui/showStdout.go | 57 | ||||
| -rw-r--r-- | toolkit/gocui/structs.go | 34 | ||||
| -rw-r--r-- | toolkit/widget.go | 4 |
11 files changed, 136 insertions, 35 deletions
diff --git a/README-goreadme.md b/README-goreadme.md index ea16866..2aa5bf2 100644 --- a/README-goreadme.md +++ b/README-goreadme.md @@ -139,7 +139,7 @@ Creates a window helpful for debugging this package `func ShowDebugValues()` -### func [StandardExit](/main.go#L182) +### func [StandardExit](/main.go#L191) `func StandardExit()` @@ -180,7 +180,7 @@ var Config GuiConfig 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 -#### func [New](/main.go#L153) +#### func [New](/main.go#L162) `func New() *Node` @@ -19,6 +19,7 @@ package witlog // import ( + "io" "os" "runtime" "runtime/pprof" @@ -152,8 +153,8 @@ func logindent(depth int, format string, a ...interface{}) { Log(debugToolkit, newFormat, a) } -func SetOutput(f *os.File) { - golog.SetOutput(f) +func SetOutput(w io.Writer) { + golog.SetOutput(w) } func SetToolkitOutput(newLog func(...any)) { @@ -40,6 +40,9 @@ func init() { Config.flag = Config.rootNode.newNode("flag", 0, nil) Config.flag.WidgetType = toolkit.Flag + Config.flag = Config.rootNode.newNode("stdout", 0, nil) + Config.flag.WidgetType = toolkit.Stdout + Config.guiChan = make(chan toolkit.Action, 1) go watchCallback() } @@ -51,6 +54,12 @@ func watchCallback() { select { case a := <-Config.guiChan: n := Config.rootNode.FindId(a.WidgetId) + if (a.ActionType == toolkit.UserQuit) { + log(logNow, "doUserEvent() node =", n.id, n.Name, "User sent Quit()") + n.doCustom() + exit("wit/gui toolkit.UserQuit") + break + } if (n == nil) { log(logError, "watchCallback() UNKNOWN widget id =", a.WidgetId, a.Name) } else { diff --git a/toolkit/gocui/click.go b/toolkit/gocui/click.go index 1da32e9..a4246a6 100644 --- a/toolkit/gocui/click.go +++ b/toolkit/gocui/click.go @@ -1,8 +1,8 @@ package main import ( - "fmt" - "errors" + // "fmt" + // "errors" "strconv" "strings" @@ -178,7 +178,7 @@ func (w *cuiWidget) drawTree(draw bool) { } func click(g *gocui.Gui, v *gocui.View) error { - var l string + // var l string var err error log(logNow, "click() START", v.Name()) @@ -201,6 +201,7 @@ func click(g *gocui.Gui, v *gocui.View) error { return err } + /* _, cy := v.Cursor() if l, err = v.Line(cy); err != nil { l = "" @@ -213,6 +214,7 @@ func click(g *gocui.Gui, v *gocui.View) error { v.SelFgColor = gocui.ColorBlack fmt.Fprintln(v, l) } + */ // this seems to delete the button(?) // g.SetViewOnBottom(v.Name()) diff --git a/toolkit/gocui/keybindings.go b/toolkit/gocui/keybindings.go index dd39e2d..66c098b 100644 --- a/toolkit/gocui/keybindings.go +++ b/toolkit/gocui/keybindings.go @@ -76,13 +76,13 @@ func addDebugKeys(g *gocui.Gui) { g.SetKeybinding("", 'q', gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error { me.baseGui.Close() - exit("forced exit() from within gocui") + sendBackQuit() return nil }) g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error { me.baseGui.Close() - exit("forced exit() from within gocui") + sendBackQuit() return nil }) diff --git a/toolkit/gocui/log.go b/toolkit/gocui/log.go index e818814..baf57d9 100644 --- a/toolkit/gocui/log.go +++ b/toolkit/gocui/log.go @@ -1,9 +1,9 @@ package main import ( - "fmt" - "os" - "strings" + "io" +// "fmt" +// "strings" witlog "git.wit.org/wit/gui/log" ) @@ -29,6 +29,7 @@ func exit(a ...any) { witlog.Exit(a...) } +/* func newLog(a ...any) { s := fmt.Sprint(a...) tmp := strings.Split(s, "\n") @@ -44,8 +45,12 @@ func newLog(a ...any) { } } } +*/ -func setOutput(f *os.File) { - witlog.SetOutput(f) - witlog.SetToolkitOutput(newLog) +func setOutput(w io.Writer) { + if (w == nil) { + return + } + witlog.SetOutput(w) + // witlog.SetToolkitOutput(newLog) } diff --git a/toolkit/gocui/main.go b/toolkit/gocui/main.go index 492d935..4b66153 100644 --- a/toolkit/gocui/main.go +++ b/toolkit/gocui/main.go @@ -35,6 +35,12 @@ func PluginChannel() chan toolkit.Action { return me.pluginChan } +/* +recieves requests from the program to do things like: +* add new widgets +* change the text of a label +* etc.. +*/ func catchActionChannel() { log(logInfo, "catchActionChannel() START") for { @@ -55,21 +61,35 @@ func catchActionChannel() { func Exit() { // TODO: what should actually happen here? me.baseGui.Close() + sendBackQuit() } +func sendBackQuit() { + // send 'Quit' back to the program (?) + var a toolkit.Action + a.ActionType = toolkit.UserQuit + me.callback <- a +} + +var outf *os.File + func main() { + var err error log(logInfo, "main() start Init()") - outf, err := os.OpenFile("/tmp/witgui.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) + outf, err = os.OpenFile("/tmp/witgui.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) if err != nil { exit("error opening file: %v", err) } defer outf.Close() - setOutput(outf) - log("This is a test log entry") + // setOutput(outf) + // log("This is a test log entry") MouseMain() + log(true, "MouseMain() closed") me.baseGui.Close() + + sendBackQuit() } diff --git a/toolkit/gocui/mouse.go b/toolkit/gocui/mouse.go index 591691d..2cba5c6 100644 --- a/toolkit/gocui/mouse.go +++ b/toolkit/gocui/mouse.go @@ -52,6 +52,9 @@ func layout(g *gocui.Gui) error { if widgetView, _ := g.View("msg"); widgetView == nil { log(logInfo, "create output widget now", maxX, maxY, mx, my) makeOutputWidget(g, "this is a create before a mouse click") + if (me.logStdout != nil) { + setOutput(me.logStdout) + } } else { log(logInfo, "output widget already exists", maxX, maxY, mx, my) } diff --git a/toolkit/gocui/showStdout.go b/toolkit/gocui/showStdout.go index 4e20dda..f028587 100644 --- a/toolkit/gocui/showStdout.go +++ b/toolkit/gocui/showStdout.go @@ -5,10 +5,11 @@ import ( "fmt" "github.com/awesome-gocui/gocui" + "git.wit.org/wit/gui/toolkit" ) -var outputW int = 80 -var outputH int = 24 +var outputW int = 200 +var outputH int = 36 func moveMsg(g *gocui.Gui) { mx, my := g.MousePosition() @@ -36,24 +37,62 @@ func showMsg(g *gocui.Gui, v *gocui.View) error { return nil } -func makeOutputWidget(g *gocui.Gui, stringFromMouseClick string) { +func makeOutputWidget(g *gocui.Gui, stringFromMouseClick string) *gocui.View { maxX, maxY := g.Size() - v, err := g.SetView("msg", maxX-32, maxY/2, maxX/2+outputW, maxY/2+outputH, 0) + + if (me.rootNode == nil) { + // keep skipping this until the binary tree is initialized + return nil + } + + if (me.logStdout == nil) { + a := new(toolkit.Action) + a.Name = "stdout" + a.Text = "stdout" + a.WidgetType = toolkit.Stdout + a.WidgetId = -3 + a.ParentId = 0 + me.logStdout = makeWidget(a) + me.logStdout.gocuiSize.w0 = maxX - 32 + me.logStdout.gocuiSize.h0 = maxY/2 + me.logStdout.gocuiSize.w1 = me.logStdout.gocuiSize.w0 + outputW + me.logStdout.gocuiSize.h1 = me.logStdout.gocuiSize.h0 + outputH + + me.logStdout.realWidth = me.logStdout.gocuiSize.Width() + me.logStdout.realHeight = me.logStdout.gocuiSize.Height() + } + v, err := g.View("msg") + if (v == nil) { + log("this is supposed to happen. v == nil", err) + } else { + log("msg != nil. WTF now? err =", err) + } + // help, err := g.SetView("help", maxX-32, 0, maxX-1, 13, 0) + // v, err = g.SetView("msg", 3, 3, 30, 30, 0) + + v, err = g.SetView("msg", maxX-32, maxY/2, maxX/2+outputW, maxY/2+outputH, 0) if errors.Is(err, gocui.ErrUnknownView) { log("this is supposed to happen?", err) } if (err != nil) { log("create output window failed", err) - return + // return nil + } + + if (v == nil) { + log("msg == nil. WTF now? err =", err) + return nil + } else { + me.logStdout.v = v } - v.Clear() - v.SelBgColor = gocui.ColorCyan - v.SelFgColor = gocui.ColorBlack + me.logStdout.v.Clear() + me.logStdout.v.SelBgColor = gocui.ColorCyan + me.logStdout.v.SelFgColor = gocui.ColorBlack fmt.Fprintln(v, "figure out how to capture STDOUT to here\n" + stringFromMouseClick) g.SetViewOnBottom("msg") // g.SetViewOnBottom(v.Name()) - return + return v } diff --git a/toolkit/gocui/structs.go b/toolkit/gocui/structs.go index a934946..c6cd248 100644 --- a/toolkit/gocui/structs.go +++ b/toolkit/gocui/structs.go @@ -13,6 +13,7 @@ import ( "reflect" "strconv" "sync" + "strings" "github.com/awesome-gocui/gocui" "git.wit.org/wit/gui/toolkit" ) @@ -25,6 +26,8 @@ type config struct { rootNode *cuiWidget // the base of the binary tree. it should have id == 0 ctrlDown *cuiWidget // shown if you click the mouse when the ctrl key is pressed current *cuiWidget // this is the current tab or window to show + logStdout *cuiWidget // where to show STDOUT + logStdoutV *gocui.View // where to show STDOUT // this is the channel we send user events like // mouse clicks or keyboard events back to the program @@ -200,12 +203,27 @@ func (w *cuiWidget) Write(p []byte) (n int, err error) { w.tainted = true w.writeMutex.Lock() defer w.writeMutex.Unlock() - if (w.v == nil) { + if (me.logStdout.v == nil) { + fmt.Fprintln(outf, string(p)) + v, _ := me.baseGui.View("msg") + if (v != nil) { + fmt.Fprintln(outf, "found msg") + me.logStdout.v = v + } return } - w.v.Clear() - fmt.Fprintln(w.v, p) - log(logNow, "widget.Write()", p) + me.logStdout.v.Clear() + // fmt.Fprintln(w.v, p + "jcarr") + // log(logNow, "widget.Write()", p) + + s := fmt.Sprint(string(p)) + s = strings.TrimSuffix(s, "\n") + tmp := strings.Split(s, "\n") + outputS = append(outputS, tmp...) + if (len(outputS) > 50) { + outputS = outputS[10:] + } + fmt.Fprintln(me.logStdout.v, strings.Join(outputS, "\n")) return len(p), nil } @@ -221,20 +239,20 @@ func Set(ptr interface{}, tag string) error { for i := 0; i < t.NumField(); i++ { defaultVal := t.Field(i).Tag.Get(tag) - // name := t.Field(i).Name + name := t.Field(i).Name // log("Set() try name =", name, "defaultVal =", defaultVal) - setField(v.Field(i), defaultVal) + setField(v.Field(i), defaultVal, name) } return nil } -func setField(field reflect.Value, defaultVal string) error { +func setField(field reflect.Value, defaultVal string, name string) error { if !field.CanSet() { // log("setField() Can't set value", field, defaultVal) return fmt.Errorf("Can't set value\n") } else { - log("setField() Can set value", field, defaultVal) + log("setField() Can set value", name, defaultVal) } switch field.Kind() { diff --git a/toolkit/widget.go b/toolkit/widget.go index d4542bc..f3d4773 100644 --- a/toolkit/widget.go +++ b/toolkit/widget.go @@ -69,6 +69,7 @@ const ( Font // TODO Color // TODO Dialog // TODO + Stdout // can be used to capture and display log output ) const ( @@ -93,6 +94,7 @@ const ( User // the user did something (mouse, keyboard, etc) InitToolkit // initializes the toolkit CloseToolkit // closes the toolkit + UserQuit // the user closed the GUI ) func (s WidgetType) String() string { @@ -141,6 +143,8 @@ func (s WidgetType) String() string { return "Color" case Dialog: return "Dialog" + case Stdout: + return "Stdout" case Unknown: return "Unknown" } |
