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

Merge branch 'master' of github.com:wtfutil/wtf

This commit is contained in:
Chris Cummer
2019-05-11 08:59:58 -07:00
12 changed files with 131 additions and 117 deletions

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 {

View File

@@ -0,0 +1,20 @@
package logger
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
const defaultTitle = "Logger"
type Settings struct {
common *cfg.Common
}
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig),
}
return &settings
}

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)