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

WTF-748 Fixes and improves module templating (#786)

Fixes and improves the module templating for creating new bare-bones
text widgets.

This command:

    WTF_WIDGET_NAME=MyNewWidget go generate -run=text

now properly generates:

    * the module directory in the /modules directory
    * the widget.go file
    * the settings.go file

with no linter warnings or errors.

Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
Chris Cummer
2019-12-14 08:46:02 -08:00
committed by GitHub
parent 1686c9a6a9
commit 703619bf0a
7 changed files with 84 additions and 45 deletions

29
generator/settings.tpl Normal file
View File

@@ -0,0 +1,29 @@
package {{(Lower .Name)}}
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
const (
defaultFocusable = false
defaultTitle = "{{(.Name)}}"
)
// Settings defines the configuration properties for this module
type Settings struct {
common *cfg.Common
// Define your settings attributes here
}
// NewSettingsFromYAML creates a new settings instance from a YAML config block
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
// Configure your settings attributes here. See http://github.com/olebedev/config for type details
}
return &settings
}

View File

@@ -36,17 +36,29 @@ func main() {
widgetName,
}
tpl, _ := template.New("textwidget.tpl").Funcs(template.FuncMap{
"Lower": strings.ToLower,
"Title": strings.Title,
}).ParseFiles("generator/textwidget.tpl")
createModuleDirectory(data)
err := os.Mkdir(strings.ToLower(widgetName), os.ModePerm)
generateWidgetFile(data)
generateSettingsFile(data)
fmt.Println("Done")
}
/* -------------------- Unexported Functions -------------------- */
func createModuleDirectory(data struct { Name string }) {
err := os.MkdirAll(strings.ToLower(fmt.Sprintf("modules/%s", data.Name)), os.ModePerm)
if err != nil {
fmt.Println(err.Error())
}
}
out, err := os.Create(fmt.Sprintf("%s/widget.go", strings.ToLower(widgetName)))
func generateWidgetFile(data struct { Name string }) {
tpl, _ := template.New("textwidget.tpl").Funcs(template.FuncMap{
"Lower": strings.ToLower,
}).ParseFiles("generator/textwidget.tpl")
out, err := os.Create(fmt.Sprintf("modules/%s/widget.go", strings.ToLower(data.Name)))
if err != nil {
fmt.Println(err.Error())
}
@@ -54,3 +66,17 @@ func main() {
tpl.Execute(out, data)
}
func generateSettingsFile(data struct { Name string }) {
tpl, _ := template.New("settings.tpl").Funcs(template.FuncMap{
"Lower": strings.ToLower,
}).ParseFiles("generator/settings.tpl")
out, err := os.Create(fmt.Sprintf("modules/%s/settings.go", strings.ToLower(data.Name)))
if err != nil {
fmt.Println(err.Error())
}
defer out.Close()
tpl.Execute(out, data)
}

View File

@@ -1,56 +1,47 @@
// Package {{(Lower .Name)}}
package {{(Lower .Name)}}
import (
"strconv"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
"github.com/wtfutil/wtf/view"
)
const HelpText = `
Keyboard commands for {{(Title .Name)}}:
`
// Widget is the container for your module's data
type Widget struct {
wtf.HelpfulWidget
wtf.KeyboardWidget
wtf.TextWidget
view.TextWidget
app *tview.Application
settings *Settings
}
// NewWidget creates and returns an instance of Widget
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
KeyboardWidget: wtf.NewKeyboardWidget(),
TextWidget: wtf.NewTextWidget(app, settings.common, false),
TextWidget: view.NewTextWidget(app, settings.common),
app: app,
settings: settings,
}
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.View.SetScrollable(true)
widget.View.SetRegions(true)
widget.HelpfulWidget.SetView(widget.View)
return &widget
}
/* -------------------- Exported Functions -------------------- */
// Refresh updates the onscreen contents of the widget
func (widget *Widget) Refresh() {
// The last call should always be to the display function
widget.display()
// The last call should always be to the display function
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) display() {
widget.Redraw(widget.CommonSettings().Title, "Some text", false)
func (widget *Widget) content() string {
return "This is my widget"
}
func (widget *Widget) display() {
widget.Redraw(func() (string, string, bool) {
return widget.CommonSettings().Title, widget.content(), false
})
}