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

Merge branch 'go-generate' of https://github.com/retgits/wtf into retgits-go-generate

This commit is contained in:
Chris Cummer 2019-01-26 19:10:34 -08:00
commit 4739673aae
3 changed files with 130 additions and 0 deletions

56
generator/textwidget.go Normal file
View File

@ -0,0 +1,56 @@
// +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 "NewTextWidget". On Linux and macOS the command can be run as
// 'WTF_WIDGET_NAME=MyNewWidget go generate text'.
package main
import (
"fmt"
"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")
err := os.Mkdir(strings.ToLower(widgetName), os.ModePerm)
if err != nil {
fmt.Println(err.Error())
}
out, err := os.Create(fmt.Sprintf("%s/widget.go", strings.ToLower(widgetName)))
if err != nil {
fmt.Println(err.Error())
}
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"