1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

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
This commit is contained in:
Sean Smith 2019-05-10 18:21:52 -04:00
parent 0b59a3b62f
commit c328ba4c11
12 changed files with 131 additions and 117 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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)

View File

@ -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"

View File

@ -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,
}

View File

@ -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 {

View File

@ -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 {

105
modules/logger/widget.go Normal file
View File

@ -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
}

View File

@ -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)

View File

@ -2,7 +2,7 @@
// Copied (mostly) verbatim from https://github.com/Atrox/homedir
package wtf
package utils
import (
"errors"

View File

@ -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)
}