gocui/keybindings.go

223 lines
5.3 KiB
Go

// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"syscall"
"github.com/awesome-gocui/gocui"
"go.wit.com/log"
)
// handle ctrl+z
func handle_ctrl_z(g *gocui.Gui, v *gocui.View) error {
gocui.Suspend()
log.Info("got ctrl+z")
syscall.Kill(syscall.Getpid(), syscall.SIGSTOP)
log.Info("got ctrl+z syscall() done")
gocui.Resume()
return nil
}
func defaultKeybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return err
}
// setup ctrl+z
keyForced, modForced := gocui.MustParse("ctrl+z")
if err := g.SetKeybinding("", keyForced, modForced, handle_ctrl_z); err != nil {
log.Error(err)
}
for _, n := range []string{"but1", "but2", "help", "but3"} {
if err := g.SetKeybinding(n, gocui.MouseLeft, gocui.ModNone, showMsg); err != nil {
return err
}
}
if err := g.SetKeybinding("", gocui.MouseRelease, gocui.ModNone, mouseUp); err != nil {
return err
}
// mouseDown() runs whenever you click on an unknown view (?)
if err := g.SetKeybinding("", gocui.MouseLeft, gocui.ModNone, mouseDown); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.MouseLeft, gocui.ModMouseCtrl, ctrlDown); err != nil {
return err
}
// if err := g.SetKeybinding(w.v.Name(), gocui.MouseLeft, gocui.ModNone, click); err != nil {
// return err
// }
/*
if err := g.SetKeybinding("", gocui.MouseLeft, gocui.ModNone, globalDown); err != nil {
return err
}
*/
if err := g.SetKeybinding("msg", gocui.MouseLeft, gocui.ModNone, msgDown); err != nil {
return err
}
addDebugKeys(g)
return nil
}
func addDebugKeys(g *gocui.Gui) {
// show debugging buttons
g.SetKeybinding("", 'd', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
fakeStartWidth = me.FakeW
fakeStartHeight = me.TabH + me.FramePadH
if showDebug {
showFake()
showDebug = false
} else {
hideFake()
showDebug = true
}
return nil
})
// display the help menu
g.SetKeybinding("", '?', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
if showHelp {
helplayout()
showHelp = false
if me.dropdownV == nil {
me.dropdownV = makeDropdownView("addWidget() ddview")
}
me.dropdownV.Show()
} else {
me.baseGui.DeleteView("help")
showHelp = true
me.dropdownV.Hide()
}
return nil
})
// redraw all the widgets
g.SetKeybinding("", 'r', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
var w *guiWidget
w = me.treeRoot.TK.(*guiWidget)
if redoWidgets {
wRoot := me.treeRoot.TK.(*guiWidget)
wRoot.redoWindows(0, 0)
redoWidgets = false
} else {
w.hideWidgets()
redoWidgets = true
}
return nil
})
// hide all widgets
g.SetKeybinding("", 'h', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
var w *guiWidget
w = me.treeRoot.TK.(*guiWidget)
w.hideWidgets()
return nil
})
// show all widgets
g.SetKeybinding("", 's', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
var w *guiWidget
w = me.treeRoot.TK.(*guiWidget)
w.showWidgets()
return nil
})
// list all widgets
g.SetKeybinding("", 'L', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
me.treeRoot.ListWidgets()
tk := me.logStdout.TK.(*guiWidget)
// msg := fmt.Sprintf("test out kb %d\n", ecount)
// tk.Write([]byte(msg))
if tk == nil {
log.Log(ERROR, "tk = nil")
}
if tk.v == nil {
log.Log(ERROR, "tk.v = nil")
} else {
log.Log(ERROR, "setting log.CaptureMode(tk.v)")
log.Log(ERROR, "setting log.CaptureMode(tk.v)")
log.CaptureMode(tk)
}
return nil
})
// list all widgets with positions
g.SetKeybinding("", 'M', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
w := me.treeRoot.TK.(*guiWidget)
w.dumpTree("M")
return nil
})
// redo windows
g.SetKeybinding("", 'w', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
wRoot := me.treeRoot.TK.(*guiWidget)
wRoot.redoWindows(0, 0)
return nil
})
// log to output window
g.SetKeybinding("", 'o', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
log.Log(ERROR, "TODO: trying re-implement this")
tk := me.logStdout.TK.(*guiWidget)
if tk == nil {
log.Log(ERROR, "tk = nil")
}
if tk.v == nil {
log.Log(ERROR, "tk.v = nil")
}
if tk.Visible() {
log.Log(ERROR, "setting visible to false")
tk.SetVisible(false)
// setOutput(os.Stdout)
} else {
log.Log(ERROR, "setting visible to true")
tk.SetVisible(true)
// setOutput(me.logStdout.tk)
}
return nil
})
// exit
g.SetKeybinding("", 'q', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
standardExit()
return nil
})
g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
standardExit()
return nil
})
g.SetKeybinding("", gocui.KeyCtrlD, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
if showDebug {
me.myTree.SendEnableDebugger()
}
return nil
})
g.SetKeybinding("", gocui.KeyCtrlV, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
return nil
})
// panic
g.SetKeybinding("", 'p', gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
standardClose()
panic("forced panic in gocui")
return nil
})
}