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

Add more 'man page' like functionality

This commit is contained in:
Sean Smith 2019-05-24 00:06:10 -04:00
parent 9a877b5e04
commit d8f337884f
8 changed files with 109 additions and 14 deletions

View File

@ -51,15 +51,15 @@ type Sigils struct {
type Common struct {
Colors
Module
Position
Position `help:"Defines where in the grid this modules 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 {

View File

@ -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
}

View File

@ -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()
}

View File

@ -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()) {

77
utils/help_parser.go Normal file
View File

@ -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
}

View File

@ -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 {

View File

@ -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 {

View File

@ -15,6 +15,7 @@ type Wtfable interface {
SetFocusChar(string)
TextView() *tview.TextView
HelpText() string
ConfigText() string
Height() int
Left() int