forked from wtfutil/wtf
/
widget.go
89 lines (67 loc) · 1.76 KB
/
widget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package textfile
import (
"fmt"
"io/ioutil"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)
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: wtf.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() {
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(err.Error())
} else {
widget.View.SetText(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)
}