Skip to content

Commit

Permalink
A simple widget to display the termshark log file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gcla committed Jul 19, 2020
1 parent 1722d07 commit b090f05
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions widgets/logviewer/logviewer.go
Original file line number Diff line number Diff line change
@@ -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:

0 comments on commit b090f05

Please sign in to comment.