From dd89fa621d6824edf96c61e47099f1c6ad0bfd09 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 1 Apr 2018 10:05:29 +0300 Subject: [PATCH] Improve debug output options --- gomuks.go | 12 ++++++++---- ui/debug/debug.go | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/gomuks.go b/gomuks.go index 0840bfe..f252fc4 100644 --- a/gomuks.go +++ b/gomuks.go @@ -43,7 +43,7 @@ type Gomuks struct { // NewGomuks creates a new Gomuks instance with everything initialized, // but does not start it. -func NewGomuks(enableDebug bool) *Gomuks { +func NewGomuks(enableDebug, forceExternalDebug bool) *Gomuks { configDir := filepath.Join(os.Getenv("HOME"), ".config/gomuks") gmx := &Gomuks{ app: tview.NewApplication(), @@ -70,7 +70,11 @@ func NewGomuks(enableDebug bool) *Gomuks { main := gmx.ui.InitViews() if enableDebug { debug.EnableExternal() - main = gmx.debug.Wrap(main) + if forceExternalDebug { + debug.RedirectAllExt = true + } else { + main = gmx.debug.Wrap(main, debug.Right) + } gmx.debugMode = true } gmx.app.SetRoot(main, true) @@ -166,8 +170,8 @@ func (gmx *Gomuks) UI() ifc.GomuksUI { } func main() { - enableDebug := len(os.Getenv("DEBUG")) > 0 - NewGomuks(enableDebug).Start() + debugVar := os.Getenv("DEBUG") + NewGomuks(len(debugVar) > 0, debugVar == "ext").Start() // We use os.Exit() everywhere, so exiting by returning from Start() shouldn't happen. time.Sleep(5 * time.Second) diff --git a/ui/debug/debug.go b/ui/debug/debug.go index a87d64c..3f47980 100644 --- a/ui/debug/debug.go +++ b/ui/debug/debug.go @@ -35,10 +35,12 @@ type Printer interface { type Pane struct { *tview.TextView Height int + Width int num int } var Default Printer +var RedirectAllExt bool func NewPane() *Pane { pane := tview.NewTextView() @@ -52,6 +54,7 @@ func NewPane() *Pane { return &Pane{ TextView: pane, Height: 35, + Width: 80, num: 0, } } @@ -69,20 +72,49 @@ func (db *Pane) WriteString(text string) { fmt.Fprintf(db, "[%d] %s", db.num, text) } -func (db *Pane) Wrap(main tview.Primitive) tview.Primitive { - return tview.NewGrid().SetRows(0, db.Height).SetColumns(0). - AddItem(main, 0, 0, 1, 1, 1, 1, true). - AddItem(db, 1, 0, 1, 1, 1, 1, false) +type PaneSide int + +const ( + Top PaneSide = iota + Bottom + Left + Right +) + +func (db *Pane) Wrap(main tview.Primitive, side PaneSide) tview.Primitive { + rows, columns := []int{0}, []int{0} + mainRow, mainColumn, paneRow, paneColumn := 0, 0, 0, 0 + switch side { + case Top: + rows = []int{db.Height, 0} + mainRow = 1 + case Bottom: + rows = []int{0, db.Height} + paneRow = 1 + case Left: + columns = []int{db.Width, 0} + mainColumn = 1 + case Right: + columns = []int{0, db.Width} + paneColumn = 1 + } + return tview.NewGrid().SetRows(rows...).SetColumns(columns...). + AddItem(main, mainRow, mainColumn, 1, 1, 1, 1, true). + AddItem(db, paneRow, paneColumn, 1, 1, 1, 1, false) } func Printf(text string, args ...interface{}) { - if Default != nil { + if RedirectAllExt { + ExtPrintf(text, args...) + } else if Default != nil { Default.Printf(text, args...) } } func Print(text ...interface{}) { - if Default != nil { + if RedirectAllExt { + ExtPrint(text...) + } else if Default != nil { Default.Print(text...) } }