1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/logger/widget.go
Chris Cummer fd794707cd
☢️ WTF-1031 Support multiple simultaneous configurations (#1032)
* WTF-1031 Rename WtfApp.app to WtfApp.tviewApp

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 Add scaffolding for main to support multiple WtfApp instances

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 WIP

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Remove common functionality from KeyboardWidget and into Base

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Augment with some descriptive comments

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Add full support for multiple app instances via the AppManager.

Still to do:

* Config support for multiple apps/multiple config files
* The ability to switch between apps

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Move SetTerminal out of main and into its own file

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-12-21 03:25:41 -08:00

102 lines
1.9 KiB
Go

package logger
import (
"fmt"
"os"
"strings"
"github.com/rivo/tview"
log "github.com/wtfutil/wtf/logger"
"github.com/wtfutil/wtf/view"
)
const (
maxBufferSize int64 = 1024
)
type Widget struct {
view.TextWidget
filePath string
settings *Settings
}
func NewWidget(tviewApp *tview.Application, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(tviewApp, nil, settings.Common),
filePath: log.LogFilePath(),
settings: settings,
}
return &widget
}
// Refresh updates the onscreen contents of the widget
func (widget *Widget) Refresh() {
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
if log.LogFileMissing() {
return widget.CommonSettings().Title, "File missing", false
}
logLines := widget.tailFile()
str := ""
for _, line := range logLines {
chunks := strings.Split(line, " ")
if len(chunks) >= 4 {
str += fmt.Sprintf(
"[green]%s[white] [yellow]%s[white] %s\n",
chunks[0],
chunks[1],
strings.Join(chunks[3:], " "),
)
}
}
return widget.CommonSettings().Title, str, false
}
func (widget *Widget) tailFile() []string {
file, err := os.Open(widget.filePath)
if err != nil {
return []string{}
}
defer func() { _ = file.Close() }()
stat, err := file.Stat()
if err != nil {
return []string{}
}
bufferSize := maxBufferSize
if maxBufferSize > stat.Size() {
bufferSize = stat.Size()
}
startPos := stat.Size() - bufferSize
buff := make([]byte, bufferSize)
_, err = file.ReadAt(buff, startPos)
if err != nil {
return []string{}
}
logLines := strings.Split(string(buff), "\n")
// Reverse the array of lines
// Offset by two to account for the blank line at the end
last := len(logLines) - 2
for i := 0; i < len(logLines)/2; i++ {
logLines[i], logLines[last-i] = logLines[last-i], logLines[i]
}
return logLines
}