1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/textfile/widget.go
Chris Cummer 037c90db85 Widget#focus now a thing
Widgets can inform whether or not they should get tab focus.

Widgets that provide additional functionality should return true.

Widgets that have no extra capability should return false.

This allows the FocusTracker to only tab through and focus on widgets
for which it provides value.
2018-04-28 23:41:51 -07:00

52 lines
931 B
Go

package textfile
import (
"fmt"
"time"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
// Config is a pointer to the global config object
var Config *config.Config
type Widget struct {
wtf.TextWidget
FilePath string
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" 📄 Text File ", "textfile", true),
FilePath: Config.UString("wtf.mods.textfile.filename"),
}
widget.View.SetWrap(true)
widget.View.SetWordWrap(true)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.View.SetTitle(fmt.Sprintf(" 📄 %s ", widget.FilePath))
widget.RefreshedAt = time.Now()
widget.View.Clear()
fileData, err := wtf.ReadFile(widget.FilePath)
if err != nil {
fmt.Fprintf(widget.View, "%s", err)
} else {
fmt.Fprintf(widget.View, "%s", fileData)
}
}