mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
package textfile
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"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"),
|
|
FilePath: Config.UString("wtf.mods.textfile.filepath"),
|
|
}
|
|
|
|
widget.View.SetWrap(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 := widget.readFile()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(widget.View, "%s", err)
|
|
} else {
|
|
fmt.Fprintf(widget.View, "%s", fileData)
|
|
}
|
|
}
|
|
|
|
/* -------------------- Uneported Functions -------------------- */
|
|
|
|
func (widget *Widget) readFile() (string, error) {
|
|
absPath, _ := wtf.ExpandHomeDir(widget.FilePath)
|
|
|
|
bytes, err := ioutil.ReadFile(absPath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(bytes), nil
|
|
}
|