1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/textfile/widget.go
Chris Cummer 218b6937b1 Implement ASCII-only widget titles
Widget titles can now be specified in the config file via a 'title' key.
Example:

  wtf:
    mods:
      todo:
        title: Tada

which can include emoji. No need to force everyone to look at my emoji,
now they can define their own.
2018-06-03 20:54:07 -07:00

98 lines
1.9 KiB
Go

package textfile
import (
"fmt"
"io/ioutil"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)
// Config is a pointer to the global config object
var Config *config.Config
const HelpText = `
Keyboard commands for Textfile:
/: Show/hide this help window
o: Open the text file in the operating system
`
type Widget struct {
wtf.TextWidget
app *tview.Application
filePath string
pages *tview.Pages
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Text File ", "textfile", true),
app: app,
filePath: Config.UString("wtf.mods.textfile.filePath"),
pages: pages,
}
widget.View.SetWrap(true)
widget.View.SetWordWrap(true)
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.UpdateRefreshedAt()
widget.View.SetTitle(fmt.Sprintf("%s %s", widget.Name, widget.filePath))
filePath, _ := wtf.ExpandHomeDir(widget.filePath)
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
fileData = []byte{}
}
if err != nil {
widget.View.SetText(fmt.Sprintf("%s", err))
} else {
widget.View.SetText(fmt.Sprintf("%s", string(fileData)))
}
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch string(event.Rune()) {
case "/":
widget.showHelp()
return nil
case "o":
wtf.OpenFile(widget.filePath)
return nil
}
return event
}
func (widget *Widget) showHelp() {
closeFunc := func() {
widget.pages.RemovePage("help")
widget.app.SetFocus(widget.View)
}
modal := wtf.NewBillboardModal(HelpText, closeFunc)
widget.pages.AddPage("help", modal, false, true)
widget.app.SetFocus(modal)
}