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:
parent
1686c9a6a9
commit
703619bf0a
29
generator/settings.tpl
Normal file
29
generator/settings.tpl
Normal 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
|
||||||
|
}
|
@ -36,17 +36,29 @@ func main() {
|
|||||||
widgetName,
|
widgetName,
|
||||||
}
|
}
|
||||||
|
|
||||||
tpl, _ := template.New("textwidget.tpl").Funcs(template.FuncMap{
|
createModuleDirectory(data)
|
||||||
"Lower": strings.ToLower,
|
|
||||||
"Title": strings.Title,
|
|
||||||
}).ParseFiles("generator/textwidget.tpl")
|
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
@ -54,3 +66,17 @@ func main() {
|
|||||||
|
|
||||||
tpl.Execute(out, data)
|
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)
|
||||||
|
}
|
@ -1,56 +1,47 @@
|
|||||||
// Package {{(Lower .Name)}}
|
|
||||||
package {{(Lower .Name)}}
|
package {{(Lower .Name)}}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/wtfutil/wtf/wtf"
|
"github.com/wtfutil/wtf/view"
|
||||||
)
|
)
|
||||||
|
|
||||||
const HelpText = `
|
// Widget is the container for your module's data
|
||||||
Keyboard commands for {{(Title .Name)}}:
|
|
||||||
`
|
|
||||||
|
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
wtf.HelpfulWidget
|
view.TextWidget
|
||||||
wtf.KeyboardWidget
|
|
||||||
wtf.TextWidget
|
|
||||||
|
|
||||||
|
app *tview.Application
|
||||||
settings *Settings
|
settings *Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewWidget creates and returns an instance of Widget
|
||||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
TextWidget: view.NewTextWidget(app, settings.common),
|
||||||
KeyboardWidget: wtf.NewKeyboardWidget(),
|
|
||||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
|
||||||
|
|
||||||
|
app: app,
|
||||||
settings: settings,
|
settings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.initializeKeyboardControls()
|
|
||||||
widget.View.SetInputCapture(widget.InputCapture)
|
|
||||||
|
|
||||||
widget.View.SetScrollable(true)
|
|
||||||
widget.View.SetRegions(true)
|
|
||||||
|
|
||||||
widget.HelpfulWidget.SetView(widget.View)
|
|
||||||
|
|
||||||
return &widget
|
return &widget
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------- Exported Functions -------------------- */
|
/* -------------------- Exported Functions -------------------- */
|
||||||
|
|
||||||
|
// Refresh updates the onscreen contents of the widget
|
||||||
func (widget *Widget) Refresh() {
|
func (widget *Widget) Refresh() {
|
||||||
|
|
||||||
// The last call should always be to the display function
|
// The last call should always be to the display function
|
||||||
widget.display()
|
widget.display()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- Unexported Functions -------------------- */
|
||||||
|
|
||||||
func (widget *Widget) display() {
|
func (widget *Widget) content() string {
|
||||||
widget.Redraw(widget.CommonSettings().Title, "Some text", false)
|
return "This is my widget"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) display() {
|
||||||
|
widget.Redraw(func() (string, string, bool) {
|
||||||
|
return widget.CommonSettings().Title, widget.content(), false
|
||||||
|
})
|
||||||
|
}
|
@ -24,5 +24,4 @@ func (widget *Widget) display() {
|
|||||||
widget.Redraw(func() (string, string, bool) {
|
widget.Redraw(func() (string, string, bool) {
|
||||||
return widget.CommonSettings().Title, renderWidget(*widget.settings), false
|
return widget.CommonSettings().Title, renderWidget(*widget.settings), false
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,8 @@ const (
|
|||||||
defaultTitle = "Clocks"
|
defaultTitle = "Clocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Settings struct to define settings got digital clock
|
// Settings defines the configuration properties for this module
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
// hello
|
|
||||||
common *cfg.Common
|
common *cfg.Common
|
||||||
|
|
||||||
hourFormat string `help:"The format of the clock." values:"12 or 24"`
|
hourFormat string `help:"The format of the clock." values:"12 or 24"`
|
||||||
@ -20,7 +19,7 @@ type Settings struct {
|
|||||||
font string `help:"The font of the clock." values:"bigfont or digitalfont"`
|
font string `help:"The font of the clock." values:"bigfont or digitalfont"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSettingsFromYAML functino to create setting instance from yaml file
|
// NewSettingsFromYAML creates a new settings instance from a YAML config block
|
||||||
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||||
|
|
||||||
settings := Settings{
|
settings := Settings{
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"github.com/wtfutil/wtf/view"
|
"github.com/wtfutil/wtf/view"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Widget a text widget struct to hold info about the current widget
|
// Widget is a text widget struct to hold info about the current widget
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
view.TextWidget
|
view.TextWidget
|
||||||
|
|
||||||
@ -15,7 +15,6 @@ type Widget struct {
|
|||||||
|
|
||||||
// NewWidget creates a new widget using settings
|
// NewWidget creates a new widget using settings
|
||||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||||
|
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
TextWidget: view.NewTextWidget(app, settings.common),
|
TextWidget: view.NewTextWidget(app, settings.common),
|
||||||
|
|
||||||
@ -30,9 +29,5 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
|||||||
|
|
||||||
// Refresh updates the onscreen contents of the widget
|
// Refresh updates the onscreen contents of the widget
|
||||||
func (widget *Widget) Refresh() {
|
func (widget *Widget) Refresh() {
|
||||||
widget.app.QueueUpdateDraw(func() {
|
widget.display()
|
||||||
widget.display()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
|
||||||
|
@ -28,7 +28,7 @@ func (t *tokenSource) Token() (*oauth2.Token, error) {
|
|||||||
|
|
||||||
/* -------------------- Widget -------------------- */
|
/* -------------------- Widget -------------------- */
|
||||||
|
|
||||||
// Widget is the container for transmission data
|
// Widget is the container for droplet data
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
view.KeyboardWidget
|
view.KeyboardWidget
|
||||||
view.ScrollableWidget
|
view.ScrollableWidget
|
||||||
|
Loading…
x
Reference in New Issue
Block a user