From b090f05f1b2ebfbb43860d93383484a1d0aa88f8 Mon Sep 17 00:00:00 2001 From: Graham Clark Date: Sun, 19 Jul 2020 18:11:23 -0400 Subject: [PATCH] A simple widget to display the termshark log file This uses gowid's terminal widget. Termshark's main.pager setting is used to open the log file, if configured; if not, the environment variable PAGER is tried. This is intended to help diagnose errors that are not displayed with enough information in the UI. --- widgets/logviewer/logviewer.go | 84 ++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 widgets/logviewer/logviewer.go diff --git a/widgets/logviewer/logviewer.go b/widgets/logviewer/logviewer.go new file mode 100644 index 0000000..b46e2d1 --- /dev/null +++ b/widgets/logviewer/logviewer.go @@ -0,0 +1,84 @@ +// Copyright 2019-2020 Graham Clark. All rights reserved. Use of this source +// code is governed by the MIT license that can be found in the LICENSE +// file. + +// +build !windows + +// Package logviewer provides a widget to view termshark's log file in a terminal +// via a pager program. +package logviewer + +import ( + "fmt" + "os" + + "github.com/gcla/gowid" + "github.com/gcla/gowid/widgets/hpadding" + "github.com/gcla/gowid/widgets/null" + "github.com/gcla/gowid/widgets/pile" + "github.com/gcla/gowid/widgets/terminal" + "github.com/gcla/gowid/widgets/text" + "github.com/gcla/termshark/v2" +) + +//====================================================================== + +type Widget struct { + gowid.IWidget +} + +// New - a bit clumsy, UI will always be legit, but error represents terminal failure +func New(cb gowid.IWidgetChangedCallback) (*Widget, error) { + logfile := termshark.CacheFile("termshark.log") + + var args []string + pager := termshark.ConfString("main.pager", "") + if pager == "" { + pager = os.Getenv("PAGER") + } + if pager == "" { + args = []string{"less", "+G", logfile} + } else { + args = []string{"sh", "-c", fmt.Sprintf("%s %s", pager, logfile)} + } + + var term gowid.IWidget + var termC *terminal.Widget + var errTerm error + termC, errTerm = terminal.New(args) + if errTerm != nil { + term = null.New() + } else { + termC.OnProcessExited(cb) + term = termC + } + + header := hpadding.New( + text.New(fmt.Sprintf("Logs - %s", logfile)), + gowid.HAlignMiddle{}, + gowid.RenderFixed{}, + ) + + main := pile.New([]gowid.IContainerWidget{ + &gowid.ContainerWidget{ + IWidget: header, + D: gowid.RenderWithUnits{U: 2}, + }, + &gowid.ContainerWidget{ + IWidget: term, + D: gowid.RenderWithWeight{W: 1.0}, + }, + }) + + res := &Widget{ + IWidget: main, + } + + return res, errTerm +} + +//====================================================================== +// Local Variables: +// mode: Go +// fill-column: 110 +// End: