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

View File

@ -1,8 +1,6 @@
package main
import (
"fmt"
"github.com/awesome-gocui/gocui"
"go.wit.com/log"
"go.wit.com/toolkits/tree"
@ -181,6 +179,18 @@ func (w *guiWidget) doWidgetClick() {
case widget.Button:
// doUserEvent(n)
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:
log.Log(NOW, "do the dropdown here")
var ddItems string
@ -189,29 +199,11 @@ func (w *guiWidget) doWidgetClick() {
ddItems += s + "\n"
}
if me.ddview == nil {
me.ddview = addDropdown()
tk := me.ddview.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
}
tk.v.Wrap = true
tk.v.Frame = true
tk.v.Clear()
fmt.Fprint(tk.v, ddItems)
tk.SetVisible(true)
return
me.ddview = makeDropdownView(ddItems)
}
showDropdownItems(ddItems)
me.ddNode = w.node
/*
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
log.Log(NOW, "doWidgetClick() visible =", tk.Visible())
@ -230,6 +222,7 @@ func (w *guiWidget) doWidgetClick() {
for i, s := range tk.vals {
log.Log(NOW, "AddText()", tk.String(), i, s)
}
*/
default:
}
}
@ -300,14 +293,19 @@ func findUnderMouse() *tree.Node {
var f func(n *tree.Node)
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
f = func(n *tree.Node) {
var widget *guiWidget
widget = n.TK.(*guiWidget)
// ignore widgets that are not visible
if widget.node.WidgetId == 2222 {
widget.SetVisible(false)
}
if widget.Visible() {
if (widget.gocuiSize.w0 <= w) && (w <= widget.gocuiSize.w1) &&
(widget.gocuiSize.h0 <= h) && (h <= widget.gocuiSize.h1) {
@ -315,20 +313,6 @@ func findUnderMouse() *tree.Node {
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 {
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() {
}
var origStdout *os.File
var origStderr *os.File
func mainGogui() {
defer func() {
if r := recover(); r != nil {
log.Warn("YAHOOOO Recovered in guiMain application:", r)
log.Warn("Recovered from panic:", r)
me.baseGui.Close()
log.Sleep(1)
log.Sleep(3)
os.Stdout = origStdout
os.Stderr = origStderr
me.myTree.SendToolkitPanic()
log.Warn("Stack trace:")
@ -89,6 +94,7 @@ func mainGogui() {
log.Error(err, "error opening file: %v")
os.Exit(0)
}
origStdout = os.Stdout
os.Stdout = outf
defer outf.Close()
@ -96,6 +102,7 @@ func mainGogui() {
// log("This is a test log entry")
ferr, _ := os.OpenFile("/tmp/witgui.err", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
origStderr = os.Stderr
os.Stderr = ferr
gocuiMain()
}

109
mouse.go
View File

@ -36,110 +36,14 @@ func msgDown(g *gocui.Gui, v *gocui.View) error {
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 {
var tk *guiWidget
if me.ddview == nil {
makeDropdownView()
me.ddview = makeDropdownView("mouseUp unclick")
}
tk = me.ddview.TK.(*guiWidget)
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 = ""
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
}
*/
dropdownUnclicked(w, h)
if msgMouseDown {
msgMouseDown = false
if movingMsg {
@ -167,14 +71,7 @@ func mouseDown(g *gocui.Gui, v *gocui.View) error {
test := findUnderMouse()
msg := fmt.Sprintf("Mouse really down at: %d,%d", mx, my) + "foobar"
if test == me.ddview {
tk := me.ddview.TK.(*guiWidget)
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()
}
dropdownClicked(mx, my)
return nil
}
x := mx - len(msg)/2

View File

@ -112,35 +112,3 @@ func (w *guiWidget) SetVisible(b bool) {
}
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
}