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

feat: add generators

This commit is contained in:
retgits 2019-01-20 12:45:24 -08:00
parent 1d888118c5
commit ac5d69e676
No known key found for this signature in database
GPG Key ID: 9D3AD75D6182395C
3 changed files with 121 additions and 0 deletions

47
generator/textwidget.go Normal file
View File

@ -0,0 +1,47 @@
// +build ignore
// This package takes care of generates for empty widgets. Each generator is named after the
// type of widget it generate, so textwidget.go will generate the skeleton for a new TextWidget
// using the textwidget.tpl template.
// The TextWidget generator needs one environment variable, called WTF_WIDGET_NAME, which will
// be the name of the TextWidget it generates. If the variable hasn't been set, the generator
// will use "NewWiNewTextWidgetdget". On Linux and macOS the command can be run as
// 'WTF_WIDGET_NAME=MyNewWidget go generate text'.
package main
import (
"os"
"strings"
"text/template"
)
var (
widgetName string
)
const (
defaultWidgetName = "NewTextWidget"
)
func main() {
widgetName, present := os.LookupEnv("WTF_WIDGET_NAME")
if !present {
widgetName = defaultWidgetName
}
data := struct {
Name string
}{
widgetName,
}
tpl, _ := template.New("textwidget.tpl").Funcs(template.FuncMap{
"Lower": strings.ToLower,
"Title": strings.Title,
}).ParseFiles("generator/textwidget.tpl")
out, _ := os.Create("generator/test.go")
defer out.Close()
tpl.Execute(out, data)
}

69
generator/textwidget.tpl Normal file
View File

@ -0,0 +1,69 @@
// Package {{(Lower .Name)}}
package {{(Lower .Name)}}
import (
"strconv"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)
const HelpText = `
Keyboard commands for {{(Title .Name)}}:
`
type Widget struct {
wtf.HelpfulWidget
wtf.TextWidget
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
TextWidget: wtf.NewTextWidget(app, "{{(Title .Name)}}", "{{(Lower .Name)}}", true),
}
widget.HelpfulWidget.SetView(widget.View)
widget.unselect()
widget.View.SetScrollable(true)
widget.View.SetRegions(true)
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
// The last call should always be to the display function
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) display() {
widget.View.SetWrap(false)
widget.View.Clear()
widget.View.SetTitle(widget.Name)
widget.View.SetText("Some Text")
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
}
func (widget *Widget) unselect() {
widget.selected = -1
widget.display()
}
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
// This switch statement could handle alphanumeric keys
switch string(event.Rune()) {
}
// This switch statement could handle events like the "enter" key
switch event.Key() {
}
}

View File

@ -1,5 +1,10 @@
package main
// Generators
// To generate the skeelton for a new TextWidget use 'WTF_WIDGET_NAME=bla go generate text'
//go:generate -command text go run generator/textwidget.go
//go:generate text
import (
"fmt"
"log"