From c328ba4c11224ed1e76ea8b26be9db4b56b6d716 Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Fri, 10 May 2019 18:21:52 -0400 Subject: [PATCH] Split up logger and widget This allows us to use the logger from the wtf directory For example when trying to debug sort ordering in focus_tracker --- flags/flags.go | 4 +- logger/log.go | 111 ++----------------------- main.go | 3 +- maker/widget_maker.go | 2 +- modules/datadog/widget.go | 1 + modules/gcal/client.go | 6 +- modules/gspreadsheets/client.go | 3 +- {logger => modules/logger}/settings.go | 0 modules/logger/widget.go | 105 +++++++++++++++++++++++ modules/textfile/widget.go | 7 +- {wtf => utils}/homedir.go | 2 +- wtf/utils.go | 4 +- 12 files changed, 131 insertions(+), 117 deletions(-) rename {logger => modules/logger}/settings.go (100%) create mode 100644 modules/logger/widget.go rename {wtf => utils}/homedir.go (98%) diff --git a/flags/flags.go b/flags/flags.go index 8f3e9af9..05da8042 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -7,7 +7,7 @@ import ( goFlags "github.com/jessevdk/go-flags" "github.com/wtfutil/wtf/help" - "github.com/wtfutil/wtf/wtf" + "github.com/wtfutil/wtf/utils" ) type Flags struct { @@ -63,7 +63,7 @@ func (flags *Flags) Parse() { // If no config file is explicitly passed in as a param, // set the flag to the default config file if !flags.HasConfig() { - homeDir, err := wtf.Home() + homeDir, err := utils.Home() if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) diff --git a/logger/log.go b/logger/log.go index 3e45aac8..c095ea42 100644 --- a/logger/log.go +++ b/logger/log.go @@ -1,46 +1,21 @@ package logger import ( - "fmt" "log" "os" "path/filepath" - "strings" - "github.com/rivo/tview" - "github.com/wtfutil/wtf/wtf" + "github.com/wtfutil/wtf/utils" ) -const maxBufferSize int64 = 1024 - -type Widget struct { - wtf.TextWidget - - app *tview.Application - filePath string - settings *Settings -} - -func NewWidget(app *tview.Application, settings *Settings) *Widget { - widget := Widget{ - TextWidget: wtf.NewTextWidget(app, settings.common, true), - - app: app, - filePath: logFilePath(), - settings: settings, - } - - return &widget -} - /* -------------------- Exported Functions -------------------- */ func Log(msg string) { - if logFileMissing() { + if LogFileMissing() { return } - f, err := os.OpenFile(logFilePath(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) + f, err := os.OpenFile(LogFilePath(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) if err != nil { log.Fatalf("error opening file: %v", err) } @@ -50,87 +25,15 @@ func Log(msg string) { log.Println(msg) } -// Refresh updates the onscreen contents of the widget -func (widget *Widget) Refresh() { - if logFileMissing() { - return - } - - logLines := widget.tailFile() - - widget.app.QueueUpdateDraw(func() { - widget.View.SetTitle(widget.CommonSettings.Title) - widget.View.SetText(widget.contentFrom(logLines)) - }) +func LogFileMissing() bool { + return LogFilePath() == "" } -/* -------------------- Unexported Functions -------------------- */ - -func (widget *Widget) contentFrom(logLines []string) string { - str := "" - - for _, line := range logLines { - chunks := strings.Split(line, " ") - - if len(chunks) >= 4 { - str = str + fmt.Sprintf( - "[green]%s[white] [yellow]%s[white] %s\n", - chunks[0], - chunks[1], - strings.Join(chunks[3:], " "), - ) - } - } - - return str -} - -func logFileMissing() bool { - return logFilePath() == "" -} - -func logFilePath() string { - dir, err := wtf.Home() +func LogFilePath() string { + dir, err := utils.Home() if err != nil { return "" } return filepath.Join(dir, ".config", "wtf", "log.txt") } - -func (widget *Widget) tailFile() []string { - file, err := os.Open(widget.filePath) - if err != nil { - return []string{} - } - defer 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 -} diff --git a/main.go b/main.go index 6cc080a3..1b6cb4e3 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/flags" "github.com/wtfutil/wtf/maker" + "github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/wtf" ) @@ -65,7 +66,7 @@ func refreshAllWidgets(widgets []wtf.Wtfable) { func watchForConfigChanges(app *tview.Application, configFilePath string, grid *tview.Grid, pages *tview.Pages) { watch := watcher.New() - absPath, _ := wtf.ExpandHomeDir(configFilePath) + absPath, _ := utils.ExpandHomeDir(configFilePath) // Notify write events watch.FilterOps(watcher.Write) diff --git a/maker/widget_maker.go b/maker/widget_maker.go index 4e460d2e..309d9228 100644 --- a/maker/widget_maker.go +++ b/maker/widget_maker.go @@ -3,7 +3,6 @@ package maker import ( "github.com/olebedev/config" "github.com/rivo/tview" - "github.com/wtfutil/wtf/logger" "github.com/wtfutil/wtf/modules/bamboohr" "github.com/wtfutil/wtf/modules/bargraph" "github.com/wtfutil/wtf/modules/circleci" @@ -25,6 +24,7 @@ import ( "github.com/wtfutil/wtf/modules/ipaddresses/ipinfo" "github.com/wtfutil/wtf/modules/jenkins" "github.com/wtfutil/wtf/modules/jira" + "github.com/wtfutil/wtf/modules/logger" "github.com/wtfutil/wtf/modules/mercurial" "github.com/wtfutil/wtf/modules/nbascore" "github.com/wtfutil/wtf/modules/newrelic" diff --git a/modules/datadog/widget.go b/modules/datadog/widget.go index 137dfbf1..c15592be 100644 --- a/modules/datadog/widget.go +++ b/modules/datadog/widget.go @@ -39,6 +39,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * KeyboardWidget: wtf.NewKeyboardWidget(), TextWidget: wtf.NewTextWidget(app, settings.common, true), + app: app, settings: settings, } diff --git a/modules/gcal/client.go b/modules/gcal/client.go index ee475af5..23c45601 100644 --- a/modules/gcal/client.go +++ b/modules/gcal/client.go @@ -19,7 +19,7 @@ import ( "time" "github.com/wtfutil/wtf/cfg" - "github.com/wtfutil/wtf/wtf" + "github.com/wtfutil/wtf/utils" "golang.org/x/oauth2" "golang.org/x/oauth2/google" "google.golang.org/api/calendar/v3" @@ -30,7 +30,7 @@ import ( func (widget *Widget) Fetch() ([]*CalEvent, error) { ctx := context.Background() - secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile) + secretPath, _ := utils.ExpandHomeDir(widget.settings.secretFile) b, err := ioutil.ReadFile(secretPath) if err != nil { @@ -123,7 +123,7 @@ func isAuthenticated() bool { } func (widget *Widget) authenticate() { - secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile) + secretPath, _ := utils.ExpandHomeDir(widget.settings.secretFile) b, err := ioutil.ReadFile(secretPath) if err != nil { diff --git a/modules/gspreadsheets/client.go b/modules/gspreadsheets/client.go index 929f9748..e4adf75f 100644 --- a/modules/gspreadsheets/client.go +++ b/modules/gspreadsheets/client.go @@ -18,6 +18,7 @@ import ( "path/filepath" "strings" + "github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/wtf" "golang.org/x/oauth2" "golang.org/x/oauth2/google" @@ -29,7 +30,7 @@ import ( func (widget *Widget) Fetch() ([]*sheets.ValueRange, error) { ctx := context.Background() - secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile) + secretPath, _ := utils.ExpandHomeDir(widget.settings.secretFile) b, err := ioutil.ReadFile(secretPath) if err != nil { diff --git a/logger/settings.go b/modules/logger/settings.go similarity index 100% rename from logger/settings.go rename to modules/logger/settings.go diff --git a/modules/logger/widget.go b/modules/logger/widget.go new file mode 100644 index 00000000..b58d10e6 --- /dev/null +++ b/modules/logger/widget.go @@ -0,0 +1,105 @@ +package logger + +import ( + "fmt" + "os" + "strings" + + "github.com/rivo/tview" + log "github.com/wtfutil/wtf/logger" + "github.com/wtfutil/wtf/wtf" +) + +const maxBufferSize int64 = 1024 + +type Widget struct { + wtf.TextWidget + + app *tview.Application + filePath string + settings *Settings +} + +func NewWidget(app *tview.Application, settings *Settings) *Widget { + widget := Widget{ + TextWidget: wtf.NewTextWidget(app, settings.common, true), + + app: app, + filePath: log.LogFilePath(), + settings: settings, + } + + return &widget +} + +// Refresh updates the onscreen contents of the widget +func (widget *Widget) Refresh() { + if log.LogFileMissing() { + return + } + + logLines := widget.tailFile() + + widget.app.QueueUpdateDraw(func() { + widget.View.SetTitle(widget.CommonSettings.Title) + widget.View.SetText(widget.contentFrom(logLines)) + }) +} + +/* -------------------- Unexported Functions -------------------- */ + +func (widget *Widget) contentFrom(logLines []string) string { + str := "" + + for _, line := range logLines { + chunks := strings.Split(line, " ") + + if len(chunks) >= 4 { + str = str + fmt.Sprintf( + "[green]%s[white] [yellow]%s[white] %s\n", + chunks[0], + chunks[1], + strings.Join(chunks[3:], " "), + ) + } + } + + return str +} + +func (widget *Widget) tailFile() []string { + file, err := os.Open(widget.filePath) + if err != nil { + return []string{} + } + defer 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 +} diff --git a/modules/textfile/widget.go b/modules/textfile/widget.go index 64d01e27..7a53752d 100644 --- a/modules/textfile/widget.go +++ b/modules/textfile/widget.go @@ -14,6 +14,7 @@ import ( "github.com/alecthomas/chroma/styles" "github.com/radovskyb/watcher" "github.com/rivo/tview" + "github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/wtf" ) @@ -98,7 +99,7 @@ func (widget *Widget) fileName() string { } func (widget *Widget) formattedText() string { - filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource()) + filePath, _ := utils.ExpandHomeDir(widget.CurrentSource()) file, err := os.Open(filePath) if err != nil { @@ -129,7 +130,7 @@ func (widget *Widget) formattedText() string { } func (widget *Widget) plainText() string { - filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource()) + filePath, _ := utils.ExpandHomeDir(widget.CurrentSource()) fmt.Println(filePath) @@ -159,7 +160,7 @@ func (widget *Widget) watchForFileChanges() { // Watch each textfile for changes for _, source := range widget.Sources { - fullPath, err := wtf.ExpandHomeDir(source) + fullPath, err := utils.ExpandHomeDir(source) if err == nil { if err := watch.Add(fullPath); err != nil { log.Fatalln(err) diff --git a/wtf/homedir.go b/utils/homedir.go similarity index 98% rename from wtf/homedir.go rename to utils/homedir.go index ef43abf7..7c208de7 100644 --- a/wtf/homedir.go +++ b/utils/homedir.go @@ -2,7 +2,7 @@ // Copied (mostly) verbatim from https://github.com/Atrox/homedir -package wtf +package utils import ( "errors" diff --git a/wtf/utils.go b/wtf/utils.go index a5a2f60a..fc743dec 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -7,6 +7,8 @@ import ( "regexp" "runtime" "strings" + + "github.com/wtfutil/wtf/utils" ) const SimpleDateFormat = "Jan 2" @@ -92,7 +94,7 @@ func OpenFile(path string) { default: } } else { - filePath, _ := ExpandHomeDir(path) + filePath, _ := utils.ExpandHomeDir(path) cmd := exec.Command(OpenFileUtil, filePath) ExecuteCommand(cmd) }