From ac5d69e676af1608b55c6c7b1123416829c8520b Mon Sep 17 00:00:00 2001 From: retgits Date: Sun, 20 Jan 2019 12:45:24 -0800 Subject: [PATCH 1/3] feat: add generators --- generator/textwidget.go | 47 +++++++++++++++++++++++++++ generator/textwidget.tpl | 69 ++++++++++++++++++++++++++++++++++++++++ main.go | 5 +++ 3 files changed, 121 insertions(+) create mode 100644 generator/textwidget.go create mode 100644 generator/textwidget.tpl diff --git a/generator/textwidget.go b/generator/textwidget.go new file mode 100644 index 00000000..0790c974 --- /dev/null +++ b/generator/textwidget.go @@ -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) +} diff --git a/generator/textwidget.tpl b/generator/textwidget.tpl new file mode 100644 index 00000000..3bed5c5c --- /dev/null +++ b/generator/textwidget.tpl @@ -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() { + } +} diff --git a/main.go b/main.go index 6651cc41..2a2079a6 100644 --- a/main.go +++ b/main.go @@ -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" From d219d3e2766d36b36dfd7d16e0f876cbf943ccf6 Mon Sep 17 00:00:00 2001 From: retgits Date: Sun, 20 Jan 2019 12:47:18 -0800 Subject: [PATCH 2/3] fix: fixed typo --- generator/textwidget.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/textwidget.go b/generator/textwidget.go index 0790c974..b847ecc7 100644 --- a/generator/textwidget.go +++ b/generator/textwidget.go @@ -5,7 +5,7 @@ // 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 +// will use "NewTextWidget". On Linux and macOS the command can be run as // 'WTF_WIDGET_NAME=MyNewWidget go generate text'. package main From fa07e36b2e307837b75c61a617ca77237dc164ac Mon Sep 17 00:00:00 2001 From: retgits Date: Sun, 20 Jan 2019 13:00:26 -0800 Subject: [PATCH 3/3] fix: fix errorhandling and folder generation --- generator/textwidget.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/generator/textwidget.go b/generator/textwidget.go index b847ecc7..4b152f1e 100644 --- a/generator/textwidget.go +++ b/generator/textwidget.go @@ -10,6 +10,7 @@ package main import ( + "fmt" "os" "strings" "text/template" @@ -40,7 +41,15 @@ func main() { "Title": strings.Title, }).ParseFiles("generator/textwidget.tpl") - out, _ := os.Create("generator/test.go") + 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)