From d8f337884ff30a7f0dacc433d34ec5baaa8efe51 Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Fri, 24 May 2019 00:06:10 -0400 Subject: [PATCH] Add more 'man page' like functionality --- cfg/common_settings.go | 8 ++--- help/help.go | 7 ++-- modules/git/settings.go | 17 ++++++--- modules/git/widget.go | 4 --- utils/help_parser.go | 77 +++++++++++++++++++++++++++++++++++++++++ wtf/bargraph.go | 4 +++ wtf/text_widget.go | 5 +++ wtf/wtfable.go | 1 + 8 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 utils/help_parser.go diff --git a/cfg/common_settings.go b/cfg/common_settings.go index d80ab0a0..12a58026 100644 --- a/cfg/common_settings.go +++ b/cfg/common_settings.go @@ -51,15 +51,15 @@ type Sigils struct { type Common struct { Colors Module - Position + Position `help:"Defines where in the grid this module’s widget will be displayed."` Sigils - Enabled bool - RefreshInterval int + Enabled bool `help:"Determines whether or not this module is executed and if its data displayed onscreen." values:"true, false"` + RefreshInterval int `help:"How often, in seconds, this module will update its data." values:"A positive integer, 0..n." optional:"true"` Title string Config *config.Config - focusChar int + focusChar int `help:"Define one of the number keys as a short cut key to access the widget." optional:"true"` } func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config.Config, globalSettings *config.Config) *Common { diff --git a/help/help.go b/help/help.go index 9a7eae26..9135b39b 100644 --- a/help/help.go +++ b/help/help.go @@ -3,9 +3,9 @@ package help import ( "fmt" - //"github.com/wtfutil/wtf/cfg" "github.com/olebedev/config" "github.com/wtfutil/wtf/maker" + "github.com/wtfutil/wtf/utils" ) func Display(moduleName string, config *config.Config) { @@ -18,5 +18,8 @@ func Display(moduleName string, config *config.Config) { func helpFor(moduleName string, config *config.Config) string { widget := maker.MakeWidget(nil, nil, moduleName, moduleName, config, config) - return widget.HelpText() + result := "" + result += utils.StripColorTags(widget.HelpText()) + result += widget.ConfigText() + return result } diff --git a/modules/git/settings.go b/modules/git/settings.go index e71f970e..567d045f 100644 --- a/modules/git/settings.go +++ b/modules/git/settings.go @@ -3,6 +3,7 @@ package git import ( "github.com/olebedev/config" "github.com/wtfutil/wtf/cfg" + "github.com/wtfutil/wtf/utils" ) const defaultTitle = "Git" @@ -10,10 +11,10 @@ const defaultTitle = "Git" type Settings struct { common *cfg.Common - commitCount int - commitFormat string - dateFormat string - repositories []interface{} + commitCount int `help:"The number of past commits to display." values:"A positive integer, 0..n."` + commitFormat string `help:"The string format for the commit message." optional:"true"` + dateFormat string `help:"The string format for the date/time in the commit message." optional:"true"` + repositories []interface{} `help:"Defines which git repositories to watch." values:"A list of zero or more local file paths pointing to valid git repositories."` } func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings { @@ -29,3 +30,11 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co return &settings } + +func (widget *Widget) ConfigText() string { + return utils.HelpFromInterface(Settings{}) +} + +func (widget *Widget) HelpText() string { + return widget.KeyboardWidget.HelpText() +} diff --git a/modules/git/widget.go b/modules/git/widget.go index 52d067b4..31163c8f 100644 --- a/modules/git/widget.go +++ b/modules/git/widget.go @@ -85,10 +85,6 @@ func (widget *Widget) Refresh() { widget.display() } -func (widget *Widget) HelpText() string { - return widget.KeyboardWidget.HelpText() -} - /* -------------------- Unexported Functions -------------------- */ func (widget *Widget) addCheckoutButton(form *tview.Form, fctn func()) { diff --git a/utils/help_parser.go b/utils/help_parser.go new file mode 100644 index 00000000..624d1933 --- /dev/null +++ b/utils/help_parser.go @@ -0,0 +1,77 @@ +package utils + +import ( + "reflect" + "regexp" + "strconv" + "unicode" + "unicode/utf8" + + "github.com/wtfutil/wtf/cfg" +) + +func lowercaseTitle(title string) string { + if title == "" { + return "" + } + r, n := utf8.DecodeRuneInString(title) + return string(unicode.ToLower(r)) + title[n:] +} + +var ( + openColorRegex = regexp.MustCompile(`\[.*?\]`) +) + +func StripColorTags(input string) string { + return openColorRegex.ReplaceAllString(input, "") +} + +func helpFromValue(field reflect.StructField) string { + result := "" + var help string = field.Tag.Get("help") + optional, err := strconv.ParseBool(field.Tag.Get("optional")) + if err != nil { + optional = false + } + var values string = field.Tag.Get("values") + if optional { + help = "Optional " + help + } + if help != "" { + result += "\n\n" + lowercaseTitle(field.Name) + result += "\n" + help + if values != "" { + result += "\nValues: " + values + } + } + + return result +} + +func HelpFromInterface(item interface{}) string { + + result := "" + t := reflect.TypeOf(item) + + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + + kind := field.Type.Kind() + if field.Type.Kind() == reflect.Ptr { + kind = field.Type.Elem().Kind() + } + + if field.Name == "common" { + result += HelpFromInterface(cfg.Common{}) + } + + switch kind { + case reflect.Interface: + result += HelpFromInterface(field.Type.Elem()) + default: + result += helpFromValue(field) + } + } + + return result +} diff --git a/wtf/bargraph.go b/wtf/bargraph.go index efc33709..faf13f4e 100644 --- a/wtf/bargraph.go +++ b/wtf/bargraph.go @@ -116,6 +116,10 @@ func (widget *BarGraph) HelpText() string { return "No help available for this widget" } +func (widget *BarGraph) ConfigText() string { + return "" +} + /* -------------------- Unexported Functions -------------------- */ func (widget *BarGraph) addView() *tview.TextView { diff --git a/wtf/text_widget.go b/wtf/text_widget.go index e13298bd..70d96e9a 100644 --- a/wtf/text_widget.go +++ b/wtf/text_widget.go @@ -5,6 +5,7 @@ import ( "github.com/rivo/tview" "github.com/wtfutil/wtf/cfg" + "github.com/wtfutil/wtf/utils" ) type TextWidget struct { @@ -123,6 +124,10 @@ func (widget *TextWidget) HelpText() string { return fmt.Sprintf("\n There is no help available for widget %s", widget.CommonSettings.Module.Type) } +func (widget *TextWidget) ConfigText() string { + return utils.HelpFromInterface(cfg.Common{}) +} + /* -------------------- Unexported Functions -------------------- */ func (widget *TextWidget) addView() *tview.TextView { diff --git a/wtf/wtfable.go b/wtf/wtfable.go index ba8fe17a..29dd2ff8 100644 --- a/wtf/wtfable.go +++ b/wtf/wtfable.go @@ -15,6 +15,7 @@ type Wtfable interface { SetFocusChar(string) TextView() *tview.TextView HelpText() string + ConfigText() string Height() int Left() int