restore STDOUT and STDERR on panic()

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-02-01 20:12:26 -06:00
parent f2f43a2b0b
commit 13b0daed7c
5 changed files with 258 additions and 198 deletions

102
click.go
View File

@ -1,8 +1,6 @@
package main package main
import ( import (
"fmt"
"github.com/awesome-gocui/gocui" "github.com/awesome-gocui/gocui"
"go.wit.com/log" "go.wit.com/log"
"go.wit.com/toolkits/tree" "go.wit.com/toolkits/tree"
@ -181,6 +179,18 @@ func (w *guiWidget) doWidgetClick() {
case widget.Button: case widget.Button:
// doUserEvent(n) // doUserEvent(n)
me.myTree.SendUserEvent(w.node) me.myTree.SendUserEvent(w.node)
case widget.Combobox:
log.Log(NOW, "do the combobox here")
var ddItems string
for i, s := range w.vals {
log.Log(NOW, "ddItem:", w.String(), i, s)
ddItems += s + "\n"
}
if me.ddview == nil {
me.ddview = makeDropdownView(ddItems)
}
showDropdownItems(ddItems)
me.ddNode = w.node
case widget.Dropdown: case widget.Dropdown:
log.Log(NOW, "do the dropdown here") log.Log(NOW, "do the dropdown here")
var ddItems string var ddItems string
@ -189,47 +199,30 @@ func (w *guiWidget) doWidgetClick() {
ddItems += s + "\n" ddItems += s + "\n"
} }
if me.ddview == nil { if me.ddview == nil {
me.ddview = addDropdown() me.ddview = makeDropdownView(ddItems)
tk := me.ddview.TK.(*guiWidget) }
tk.labelN = ddItems showDropdownItems(ddItems)
tk.SetText(ddItems) me.ddNode = w.node
tk.gocuiSize.w0 = 20 /*
tk.gocuiSize.w1 = 40 var tk *guiWidget
tk.gocuiSize.h0 = 10 tk = me.ddview.TK.(*guiWidget)
tk.gocuiSize.h1 = 25 log.Log(NOW, "doWidgetClick() visible =", tk.Visible())
tk.v, _ = me.baseGui.SetView("ddview", if tk.Visible() {
tk.gocuiSize.w0, tk.SetVisible(false)
tk.gocuiSize.h0, me.baseGui.DeleteView("ddview")
tk.gocuiSize.w1, tk.v = nil
tk.gocuiSize.h1, 0) } else {
if tk.v == nil { me.ddNode = w.node
return log.Log(NOW, "new dns list should be set to:", ddItems)
tk.labelN = ddItems
tk.SetText(ddItems)
tk.SetVisible(true)
fmt.Fprint(tk.v, ddItems)
} }
tk.v.Wrap = true for i, s := range tk.vals {
tk.v.Frame = true log.Log(NOW, "AddText()", tk.String(), i, s)
tk.v.Clear() }
fmt.Fprint(tk.v, ddItems) */
tk.SetVisible(true)
return
}
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
log.Log(NOW, "doWidgetClick() visible =", tk.Visible())
if tk.Visible() {
tk.SetVisible(false)
me.baseGui.DeleteView("ddview")
tk.v = nil
} else {
me.ddNode = w.node
log.Log(NOW, "new dns list should be set to:", ddItems)
tk.labelN = ddItems
tk.SetText(ddItems)
tk.SetVisible(true)
fmt.Fprint(tk.v, ddItems)
}
for i, s := range tk.vals {
log.Log(NOW, "AddText()", tk.String(), i, s)
}
default: default:
} }
} }
@ -300,14 +293,19 @@ func findUnderMouse() *tree.Node {
var f func(n *tree.Node) var f func(n *tree.Node)
w, h := me.baseGui.MousePosition() w, h := me.baseGui.MousePosition()
ddTK := me.ddview.TK.(*guiWidget)
// if the dropdown view is visable, close it
if ddTK.Visible() {
ddTK.dropdownClicked(w, h)
return me.ddview
}
// find buttons that are below where the mouse button click // find buttons that are below where the mouse button click
f = func(n *tree.Node) { f = func(n *tree.Node) {
var widget *guiWidget var widget *guiWidget
widget = n.TK.(*guiWidget) widget = n.TK.(*guiWidget)
// ignore widgets that are not visible // ignore widgets that are not visible
if widget.node.WidgetId == 2222 {
widget.SetVisible(false)
}
if widget.Visible() { if widget.Visible() {
if (widget.gocuiSize.w0 <= w) && (w <= widget.gocuiSize.w1) && if (widget.gocuiSize.w0 <= w) && (w <= widget.gocuiSize.w1) &&
(widget.gocuiSize.h0 <= h) && (h <= widget.gocuiSize.h1) { (widget.gocuiSize.h0 <= h) && (h <= widget.gocuiSize.h1) {
@ -315,20 +313,6 @@ func findUnderMouse() *tree.Node {
found = n found = n
} }
} }
/*
if n == me.ddview {
tk := me.ddview.TK.(*guiWidget)
log.Log(NOW, "findUnderMouse() found ddview")
if tk.Visible() {
log.Log(NOW, "findUnderMouse() and ddview is visable. hide it here. TODO: find highlighted row")
found = n
// find the actual value here and set the dropdown widget
me.baseGui.DeleteView("ddview")
} else {
log.Log(NOW, "findUnderMouse() I was lying, actually it's not found")
}
}
*/
for _, child := range widget.children { for _, child := range widget.children {
f(child.node) f(child.node)

204
dropdown.go Normal file
View File

@ -0,0 +1,204 @@
package main
import (
"fmt"
"strings"
"github.com/awesome-gocui/gocui"
log "go.wit.com/log"
"go.wit.com/toolkits/tree"
"go.wit.com/widget"
)
func makeDropdownView(ddItems string) *tree.Node {
newNode := addDropdown()
tk := newNode.TK.(*guiWidget)
tk.labelN = ddItems
tk.SetText(ddItems)
tk.gocuiSize.w0 = 20
tk.gocuiSize.w1 = 40
tk.gocuiSize.h0 = 10
tk.gocuiSize.h1 = 25
tk.v, _ = me.baseGui.SetView("ddview",
tk.gocuiSize.w0,
tk.gocuiSize.h0,
tk.gocuiSize.w1,
tk.gocuiSize.h1, 0)
if tk.v == nil {
return newNode
}
tk.v.Wrap = true
tk.v.Frame = true
tk.v.Clear()
fmt.Fprint(tk.v, ddItems)
tk.SetVisible(true)
return newNode
}
func makeDropdownViewOrig() {
if me.ddview != nil {
return
}
me.ddview = addDropdown()
// n.TK = initWidget(n)
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
tk.gocuiSize.w0 = 20
tk.gocuiSize.w1 = 40
tk.gocuiSize.h0 = 10
tk.gocuiSize.h1 = 25
tk.v, _ = me.baseGui.SetView("ddview",
tk.gocuiSize.w0,
tk.gocuiSize.h0,
tk.gocuiSize.w1,
tk.gocuiSize.h1, 0)
if tk.v == nil {
return
}
tk.v.Wrap = true
tk.v.Frame = true
tk.v.Clear()
fmt.Fprint(tk.v, tk.labelN)
}
func addDropdown() *tree.Node {
n := new(tree.Node)
n.WidgetType = widget.Flag
n.WidgetId = 2222
n.ParentId = 0
// store the internal toolkit information
tk := new(guiWidget)
tk.frame = true
tk.labelN = "DropBox text"
tk.node = n
// copy the data from the action message
tk.node.State.Label = "DropBox"
// set the name used by gocui to the id
tk.cuiName = "-1 dropbox"
tk.color = &colorFlag
// add this new widget on the binary tree
tk.parent = me.treeRoot.TK.(*guiWidget)
if tk.parent == nil {
panic("addDropdown() didn't get treeRoot guiWidget")
} else {
tk.parent.children = append(tk.parent.children, tk)
}
n.TK = tk
return n
}
func showDropdownItems(ddItems string) {
for i, s := range strings.Split(ddItems, "\n") {
log.Log(NOW, "showDropdownItems() found =", i, s)
}
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
log.Log(NOW, "doWidgetClick() visible =", tk.Visible())
if tk.Visible() {
tk.SetVisible(false)
me.baseGui.DeleteView("ddview")
tk.v = nil
} else {
log.Log(NOW, "new dns list should be set to:", ddItems)
tk.labelN = ddItems
tk.SetText(ddItems)
tk.SetVisible(true)
tk.v.Clear()
fmt.Fprint(tk.v, ddItems)
}
}
func dropdownClicked(mouseX, mouseH int) {
tk := me.ddview.TK.(*guiWidget)
if tk.Visible() {
log.Log(NOW, "hide DDview() Mouse really down at:", mouseX, mouseH)
hideDDview()
} else {
log.Log(NOW, "show DDview() Mouse really down at:", mouseX, mouseH)
showDDview()
}
}
func hideDDview() error {
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
w, h := me.baseGui.MousePosition()
log.Log(NOW, "hide dropdown menu() view msgMouseDown (w,h) =", w, h)
if me.ddview == nil {
return gocui.ErrUnknownView
}
if tk.v == nil {
return gocui.ErrUnknownView
}
tk.SetVisible(false)
return nil
}
func showDDview() error {
w, h := me.baseGui.MousePosition()
log.Log(NOW, "show dropdown menu() view msgMouseDown (w,h) =", w, h)
if me.ddview == nil {
return gocui.ErrUnknownView
}
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
if tk.v == nil {
return gocui.ErrUnknownView
}
tk.SetVisible(true)
return nil
}
/*
// if there is a drop down view active, treat it like a dialog box and close it
if (hideDDview() == nil) {
return nil
}
*/
func (w *guiWidget) dropdownClicked(mouseW, mouseH int) {
log.Log(NOW, "dropdownClicked() (w,h) =", mouseW, mouseH)
w.SetVisible(false)
}
func dropdownUnclicked(mouseX, mouseH int) {
tk := me.ddview.TK.(*guiWidget)
log.Log(NOW, "mouseUp() view msgMouseDown (check here for dropdown menu click) (w,h) =", mouseX, mouseH)
if me.ddClicked {
me.ddClicked = false
log.Log(NOW, "mouseUp() ddview is the thing that was clicked", mouseX, mouseH)
log.Log(NOW, "mouseUp() find out what the string is here", mouseX, mouseH, tk.gocuiSize.h1)
var newZone string = ""
if me.ddNode != nil {
var ddtk *guiWidget
ddtk = me.ddview.TK.(*guiWidget)
value := mouseH - tk.gocuiSize.h0 - 1
log.Log(NOW, "mouseUp() me.ddview.tk.gocuiSize.h1 =", tk.gocuiSize.h1)
log.Log(NOW, "mouseUp() me.ddNode.vals =", ddtk.vals)
valsLen := len(ddtk.vals)
log.Log(NOW, "mouseUp() value =", value, "valsLen =", valsLen)
log.Log(NOW, "mouseUp() me.ddNode.vals =", ddtk.vals)
if (value >= 0) && (value < valsLen) {
newZone = ddtk.vals[value]
log.Log(NOW, "mouseUp() value =", value, "newZone =", newZone)
}
}
hideDDview()
if newZone != "" {
if me.ddNode != nil {
var ddtk *guiWidget
ddtk = me.ddview.TK.(*guiWidget)
ddtk.SetText(newZone)
ddtk.value = newZone
// me.ddNode.doUserEvent()
me.myTree.SendUserEvent(me.ddNode)
}
}
}
}

View File

@ -66,13 +66,18 @@ var outf *os.File
func main() { func main() {
} }
var origStdout *os.File
var origStderr *os.File
func mainGogui() { func mainGogui() {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
log.Warn("YAHOOOO Recovered in guiMain application:", r) log.Warn("YAHOOOO Recovered in guiMain application:", r)
log.Warn("Recovered from panic:", r) log.Warn("Recovered from panic:", r)
me.baseGui.Close() me.baseGui.Close()
log.Sleep(1) log.Sleep(3)
os.Stdout = origStdout
os.Stderr = origStderr
me.myTree.SendToolkitPanic() me.myTree.SendToolkitPanic()
log.Warn("Stack trace:") log.Warn("Stack trace:")
@ -89,6 +94,7 @@ func mainGogui() {
log.Error(err, "error opening file: %v") log.Error(err, "error opening file: %v")
os.Exit(0) os.Exit(0)
} }
origStdout = os.Stdout
os.Stdout = outf os.Stdout = outf
defer outf.Close() defer outf.Close()
@ -96,6 +102,7 @@ func mainGogui() {
// log("This is a test log entry") // log("This is a test log entry")
ferr, _ := os.OpenFile("/tmp/witgui.err", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664) ferr, _ := os.OpenFile("/tmp/witgui.err", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
origStderr = os.Stderr
os.Stderr = ferr os.Stderr = ferr
gocuiMain() gocuiMain()
} }

109
mouse.go
View File

@ -36,110 +36,14 @@ func msgDown(g *gocui.Gui, v *gocui.View) error {
return nil return nil
} }
func hideDDview() error {
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
w, h := me.baseGui.MousePosition()
log.Log(NOW, "hide dropdown menu() view msgMouseDown (w,h) =", w, h)
if me.ddview == nil {
return gocui.ErrUnknownView
}
if tk.v == nil {
return gocui.ErrUnknownView
}
tk.SetVisible(false)
return nil
}
func showDDview() error {
w, h := me.baseGui.MousePosition()
log.Log(NOW, "show dropdown menu() view msgMouseDown (w,h) =", w, h)
if me.ddview == nil {
return gocui.ErrUnknownView
}
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
if tk.v == nil {
return gocui.ErrUnknownView
}
tk.SetVisible(true)
return nil
}
func makeDropdownView() {
if me.ddview != nil {
return
}
me.ddview = addDropdown()
// n.TK = initWidget(n)
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
tk.gocuiSize.w0 = 20
tk.gocuiSize.w1 = 40
tk.gocuiSize.h0 = 10
tk.gocuiSize.h1 = 25
tk.v, _ = me.baseGui.SetView("ddview",
tk.gocuiSize.w0,
tk.gocuiSize.h0,
tk.gocuiSize.w1,
tk.gocuiSize.h1, 0)
if tk.v == nil {
return
}
tk.v.Wrap = true
tk.v.Frame = true
tk.v.Clear()
fmt.Fprint(tk.v, tk.labelN)
}
func mouseUp(g *gocui.Gui, v *gocui.View) error { func mouseUp(g *gocui.Gui, v *gocui.View) error {
var tk *guiWidget
if me.ddview == nil { if me.ddview == nil {
makeDropdownView() me.ddview = makeDropdownView("mouseUp unclick")
} }
tk = me.ddview.TK.(*guiWidget)
w, h := g.MousePosition() w, h := g.MousePosition()
log.Log(NOW, "mouseUp() view msgMouseDown (check here for dropdown menu click) (w,h) =", w, h)
if me.ddClicked {
me.ddClicked = false
log.Log(NOW, "mouseUp() ddview is the thing that was clicked", w, h)
log.Log(NOW, "mouseUp() find out what the string is here", w, h, tk.gocuiSize.h1)
var newZone string = "" dropdownUnclicked(w, h)
if me.ddNode != nil {
var ddtk *guiWidget
ddtk = me.ddview.TK.(*guiWidget)
value := h - tk.gocuiSize.h0 - 1
log.Log(NOW, "mouseUp() me.ddview.tk.gocuiSize.h1 =", tk.gocuiSize.h1)
log.Log(NOW, "mouseUp() me.ddNode.vals =", ddtk.vals)
valsLen := len(ddtk.vals)
log.Log(NOW, "mouseUp() value =", value, "valsLen =", valsLen)
log.Log(NOW, "mouseUp() me.ddNode.vals =", ddtk.vals)
if (value >= 0) && (value < valsLen) {
newZone = ddtk.vals[value]
log.Log(NOW, "mouseUp() value =", value, "newZone =", newZone)
}
}
hideDDview()
if newZone != "" {
if me.ddNode != nil {
var ddtk *guiWidget
ddtk = me.ddview.TK.(*guiWidget)
ddtk.SetText(newZone)
ddtk.value = newZone
// me.ddNode.doUserEvent()
me.myTree.SendUserEvent(me.ddNode)
}
}
return nil
}
/*
// if there is a drop down view active, treat it like a dialog box and close it
if (hideDDview() == nil) {
return nil
}
*/
if msgMouseDown { if msgMouseDown {
msgMouseDown = false msgMouseDown = false
if movingMsg { if movingMsg {
@ -167,14 +71,7 @@ func mouseDown(g *gocui.Gui, v *gocui.View) error {
test := findUnderMouse() test := findUnderMouse()
msg := fmt.Sprintf("Mouse really down at: %d,%d", mx, my) + "foobar" msg := fmt.Sprintf("Mouse really down at: %d,%d", mx, my) + "foobar"
if test == me.ddview { if test == me.ddview {
tk := me.ddview.TK.(*guiWidget) dropdownClicked(mx, my)
if tk.Visible() {
log.Log(NOW, "hide DDview() Mouse really down at:", mx, my)
hideDDview()
} else {
log.Log(NOW, "show DDview() Mouse really down at:", mx, my)
showDDview()
}
return nil return nil
} }
x := mx - len(msg)/2 x := mx - len(msg)/2

View File

@ -112,35 +112,3 @@ func (w *guiWidget) SetVisible(b bool) {
} }
w.v.Visible = b w.v.Visible = b
} }
func addDropdown() *tree.Node {
n := new(tree.Node)
n.WidgetType = widget.Flag
n.WidgetId = 2222
n.ParentId = 0
// store the internal toolkit information
tk := new(guiWidget)
tk.frame = true
tk.labelN = "DropBox text"
tk.node = n
// copy the data from the action message
tk.node.State.Label = "DropBox"
// set the name used by gocui to the id
tk.cuiName = "-1 dropbox"
tk.color = &colorFlag
// add this new widget on the binary tree
tk.parent = me.treeRoot.TK.(*guiWidget)
if tk.parent == nil {
panic("addDropdown() didn't get treeRoot guiWidget")
} else {
tk.parent.children = append(tk.parent.children, tk)
}
n.TK = tk
return n
}