From 9ef16c1bf2bfc1e00163b461779383c809efff9f Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 3 Mar 2025 03:45:36 -0600 Subject: [PATCH] better tree init() --- eventBindings.go | 3 +- init.go | 112 +++++++++++++++++++++++++++++------------------ stdoutShow.go | 10 +++-- structs.go | 3 +- treeInit.go | 16 +++++++ 5 files changed, 96 insertions(+), 48 deletions(-) diff --git a/eventBindings.go b/eventBindings.go index 508daa4..47b6737 100644 --- a/eventBindings.go +++ b/eventBindings.go @@ -4,7 +4,6 @@ package main import ( - "fmt" "syscall" "github.com/awesome-gocui/gocui" @@ -18,7 +17,7 @@ import ( func registerHandlers(g *gocui.Gui) { defer func() { if r := recover(); r != nil { - fmt.Fprintln(outf, "EVENT BINDINGS recovered in r", r) + log.Info("EVENT BINDINGS recovered in r", r) return } }() diff --git a/init.go b/init.go index 63bf765..425be56 100644 --- a/init.go +++ b/init.go @@ -96,22 +96,55 @@ func toolkitClose() { me.baseGui.Close() } +// a GO GUI plugin should initTree in init() +// this should be done before the application +// starts trying to open up a channel +func init() { + me.myTree = initTree() + +} + // sets defaults and establishes communication // to this toolkit from the wit/gui golang package func initPlugin() { defer func() { if r := recover(); r != nil { - fmt.Fprintf(outf, "PANIC: initPlugin() recovered %v\n", r) + fmt.Fprintf(me.outf, "PANIC: initPlugin() recovered %v\n", r) return } }() var err error + + // read in defaults from config protobuf + if val, err := me.myTree.ConfigFind("stdout"); err == nil { + if val == "true" { + me.stdout.startOnscreen = true + // me.stdout.Write([]byte("starting with stdout onscreen\n")) + } + if val == "disable" { + log.Info("COMPLETELY DISABLE LOG CRAP") + me.stdout.disable = true + } + } + if val, err := me.myTree.ConfigFind("dark"); err == nil { + if val == "true" { + me.dark = true + } + } else { + // macos iterm2 really only works with dark mode right now + if runtime.GOOS == "macos" { + me.dark = true + } + } // todo: make this a tmp file that goes away - outf, err = os.OpenFile("/tmp/captureMode.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - log.Info("error opening file:", err) - os.Exit(0) + if !me.stdout.disable { + log.Info("USING STDOUT") + me.outf, err = os.OpenFile("/tmp/captureMode.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + if err != nil { + log.Info("error opening file:", err) + os.Exit(0) + } } me.starttime = time.Now() log.Log(INFO, "Init() of awesome-gocui") @@ -150,41 +183,22 @@ func initPlugin() { me.mouse.clicktime = time.Millisecond * 200 me.mouse.doubletime = time.Millisecond * 400 - me.myTree = initTree() - me.newWindowTrigger = make(chan *guiWidget, 1) go newWindowTrigger() go refreshGocui() - // read in defaults from config protobuf - if val, err := me.myTree.ConfigFind("stdout"); err == nil { - if val == "true" { - me.stdout.startOnscreen = true - // me.stdout.Write([]byte("starting with stdout onscreen\n")) - } else { - // me.stdout.Write([]byte("starting with stdout offscreen\n")) - } - } - if val, err := me.myTree.ConfigFind("dark"); err == nil { - if val == "true" { - me.dark = true - } - } else { - // macos iterm2 really only works with dark mode right now - if runtime.GOOS == "macos" { - me.dark = true - } - } - log.Log(NOW, "Init() start pluginChan") - - os.Stdout = outf - - log.CaptureMode(outf) + if me.stdout.disable { + log.Info("USING STDOUT") + } else { + os.Stdout = me.outf + log.CaptureMode(me.outf) + } // init gocui g, err := gocui.NewGui(gocui.OutputNormal, true) if err != nil { + os.Exit(-1) return } me.baseGui = g @@ -203,7 +217,9 @@ func initPlugin() { time.Sleep(100 * time.Millisecond) - fmt.Fprintln(outf, "hello world", time.Since(me.starttime)) + if me.outf != nil { + fmt.Fprintln(me.outf, "hello world", time.Since(me.starttime)) + } // coreStdout() // createStdout(g) @@ -219,12 +235,18 @@ func gocuiMain() { defer func() { if r := recover(); r != nil { log.Warn("PANIC ecovered in gocuiMain()", r) - fmt.Fprintf(outf, "PANIC recovered in r = %v", r) - os.Stderr = outf - os.Stdout = outf - debug.PrintStack() - pprof.Lookup("goroutine").WriteTo(outf, 1) - panic(outf) + if me.outf == nil { + debug.PrintStack() + pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) + panic(os.Stdout) + } else { + fmt.Fprintf(me.outf, "PANIC recovered in r = %v", r) + os.Stderr = me.outf + os.Stdout = me.outf + debug.PrintStack() + pprof.Lookup("goroutine").WriteTo(me.outf, 1) + panic(me.outf) + } } }() @@ -239,8 +261,10 @@ func gocuiMain() { func standardExit() { log.Log(NOW, "standardExit() doing baseGui.Close()") me.baseGui.Close() - log.Log(NOW, "standardExit() doing outf.Close()") - outf.Close() + if me.outf != nil { + log.Log(NOW, "standardExit() doing outf.Close()") + me.outf.Close() + } // log(true, "standardExit() setOutput(os.Stdout)") // setOutput(os.Stdout) log.Log(NOW, "standardExit() send back Quit()") @@ -255,7 +279,7 @@ func standardClose() { log.Log(NOW, "standardExit() doing baseGui.Close()") me.baseGui.Close() log.Log(NOW, "standardExit() doing outf.Close()") - outf.Close() + me.outf.Close() // os.Stdin = os.Stdin // os.Stdout = os.Stdout // os.Stderr = os.Stderr @@ -297,7 +321,11 @@ func testRefresh(*gocui.Gui) error { func refreshGocui() { defer func() { if r := recover(); r != nil { - fmt.Fprintln(outf, "INIT PLUGIN recovered in r", r) + if me.outf == nil { + log.Info("INIT PLUGIN recovered in r", r) + } else { + fmt.Fprintln(me.outf, "INIT PLUGIN recovered in r", r) + } return } }() diff --git a/stdoutShow.go b/stdoutShow.go index 68ec55b..4bbf59a 100644 --- a/stdoutShow.go +++ b/stdoutShow.go @@ -161,14 +161,19 @@ func (w stdout) Write(p []byte) (n int, err error) { lines := strings.Split(strings.TrimSpace(string(p)), "\n") me.stdout.outputS = append(me.stdout.outputS, lines...) - fmt.Fprint(outf, string(p)) + if me.outf != nil { + fmt.Fprint(me.outf, string(p)) + } return len(p), nil } func (w *guiWidget) Write(p []byte) (n int, err error) { lines := strings.Split(strings.TrimSpace(string(p)), "\n") - fmt.Fprint(outf, string(p)) + if me.outf != nil { + fmt.Fprint(me.outf, string(p)) + } + if w == nil { me.stdout.outputS = append(me.stdout.outputS, lines...) return len(p), nil @@ -187,7 +192,6 @@ func (w *guiWidget) Write(p []byte) (n int, err error) { // redo this old code v, _ := me.baseGui.View("msg") if v != nil { - // fmt.Fprintln(outf, "found msg") tk.v = v } return len(p), nil diff --git a/structs.go b/structs.go index c85e859..8b9541c 100644 --- a/structs.go +++ b/structs.go @@ -25,7 +25,6 @@ import ( ) var initOnce sync.Once // run initPlugin() only once -var outf *os.File // hacks for capturing stdout // It's probably a terrible idea to call this 'me' // 2025 note: doesn't seem terrible to call this 'me' anymore. notsure. @@ -87,6 +86,7 @@ type config struct { starttime time.Time // checks how long it takes on startup winchW int // used to detect SIGWINCH winchH int // used to detect SIGWINCH + outf *os.File // hacks for capturing stdout } // stuff controlling how the mouse works @@ -112,6 +112,7 @@ type stdout struct { outputOnTop bool // is the STDOUT window on top? outputOffscreen bool // is the STDOUT window offscreen? startOnscreen bool // start the output window onscreen? + disable bool // disable the stdout window. do not change os.Stdout & os.Stderr lastW int // the last 'w' location (used to move from offscreen to onscreen) lastH int // the last 'h' location (used to move from offscreen to onscreen) // mouseOffsetW int // the current 'w' offset diff --git a/treeInit.go b/treeInit.go index 7fc1c73..4443264 100644 --- a/treeInit.go +++ b/treeInit.go @@ -32,6 +32,9 @@ package main */ import ( + "time" + + log "go.wit.com/log" "go.wit.com/toolkits/tree" "go.wit.com/widget" ) @@ -48,7 +51,20 @@ func Callback(guiCallback chan widget.Action) { } func PluginChannel() chan widget.Action { + log.Info("PluginChannel() INIT(l)") + log.Info("PluginChannel() INIT(l)") + log.Info("PluginChannel() INIT(l)") initOnce.Do(initPlugin) + for { + if me.myTree != nil { + break + } + log.Info("me.myTree == nil") + time.Sleep(300 * time.Millisecond) + } + log.Info("PluginChannel() DONE()") + log.Info("PluginChannel() DONE()") + log.Info("PluginChannel() DONE()") return me.myTree.PluginChannel() }