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

Add help modal to TextFile module

This commit is contained in:
Chris Cummer 2018-05-03 17:12:39 -07:00
parent 72f9543e75
commit 1baa884ce9
3 changed files with 41 additions and 12 deletions

View File

@ -6,22 +6,35 @@ import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
// Config is a pointer to the global config object // Config is a pointer to the global config object
var Config *config.Config var Config *config.Config
const helpText = `
Keyboard commands for Textfile:
h: Show/hide this help window
o: Open the text file in the operating system
`
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
FilePath string app *tview.Application
filePath string
pages *tview.Pages
} }
func NewWidget() *Widget { func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(" 📄 Text File ", "textfile", true), TextWidget: wtf.NewTextWidget(" 📄 Text File ", "textfile", true),
FilePath: Config.UString("wtf.mods.textfile.filename"),
app: app,
filePath: Config.UString("wtf.mods.textfile.filename"),
pages: pages,
} }
widget.View.SetWrap(true) widget.View.SetWrap(true)
@ -39,12 +52,12 @@ func (widget *Widget) Refresh() {
return return
} }
widget.View.SetTitle(fmt.Sprintf(" 📄 %s ", widget.FilePath)) widget.View.SetTitle(fmt.Sprintf(" 📄 %s ", widget.filePath))
widget.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
widget.View.Clear() widget.View.Clear()
fileData, err := wtf.ReadFile(widget.FilePath) fileData, err := wtf.ReadFile(widget.filePath)
if err != nil { if err != nil {
fmt.Fprintf(widget.View, "%s", err) fmt.Fprintf(widget.View, "%s", err)
@ -54,12 +67,29 @@ func (widget *Widget) Refresh() {
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch string(event.Rune()) { switch string(event.Rune()) {
case "h":
widget.showHelp()
return nil
case "o": case "o":
wtf.OpenFile(widget.FilePath) wtf.OpenFile(widget.filePath)
return nil return nil
} }
return event return event
} }
func (widget *Widget) showHelp() {
closeFunc := func() {
widget.pages.RemovePage("help")
widget.app.SetFocus(widget.View)
widget.Refresh()
}
modal := wtf.NewBillboardModal(helpText, closeFunc)
widget.pages.AddPage("help", modal, false, true)
widget.app.SetFocus(modal)
}

View File

@ -18,7 +18,7 @@ var Config *config.Config
const helpText = ` const helpText = `
Keyboard commands for Todo: Keyboard commands for Todo:
h: Displays the help text h: Show/hide this help window
j: Select the next item in the list j: Select the next item in the list
k: Select the previous item in the list k: Select the previous item in the list
n: Create a new list item n: Create a new list item
@ -27,7 +27,7 @@ const helpText = `
arrow down: Select the next item in the list arrow down: Select the next item in the list
arrow up: Select the previous item in the list arrow up: Select the previous item in the list
ctrl-d: delete selected item ctrl-d: Delete the selected item
esc: Unselect the todo list esc: Unselect the todo list
return: Edit selected item return: Edit selected item
@ -38,9 +38,9 @@ type Widget struct {
wtf.TextWidget wtf.TextWidget
app *tview.Application app *tview.Application
pages *tview.Pages
filePath string filePath string
list *List list *List
pages *tview.Pages
} }
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
@ -48,9 +48,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
TextWidget: wtf.NewTextWidget(" 📝 Todo ", "todo", true), TextWidget: wtf.NewTextWidget(" 📝 Todo ", "todo", true),
app: app, app: app,
pages: pages,
filePath: Config.UString("wtf.mods.todo.filename"), filePath: Config.UString("wtf.mods.todo.filename"),
list: &List{selected: -1}, list: &List{selected: -1},
pages: pages,
} }
widget.init() widget.init()
@ -111,7 +111,6 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
widget.display() widget.display()
return nil return nil
case "h": case "h":
// Show help menu
widget.showHelp() widget.showHelp()
return nil return nil
case "j": case "j":

2
wtf.go
View File

@ -194,7 +194,7 @@ func main() {
security.NewWidget(), security.NewWidget(),
status.NewWidget(), status.NewWidget(),
system.NewWidget(builtat, version), system.NewWidget(builtat, version),
textfile.NewWidget(), textfile.NewWidget(app, pages),
todo.NewWidget(app, pages), todo.NewWidget(app, pages),
weather.NewWidget(), weather.NewWidget(),
} }