Skip to content

Commit

Permalink
Merge pull request #464 from Seanstoppable/configdocs
Browse files Browse the repository at this point in the history
Add more 'man page' like functionality
  • Loading branch information
senorprogrammer authored May 25, 2019
2 parents 745ecbf + 90362de commit 7a93824
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 14 deletions.
8 changes: 4 additions & 4 deletions cfg/common_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
17 changes: 13 additions & 4 deletions modules/git/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package git
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/utils"
)

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 {
Expand All @@ -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()
}
4 changes: 0 additions & 4 deletions modules/git/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
77 changes: 77 additions & 0 deletions utils/help_parser.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions wtf/bargraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions wtf/text_widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/utils"
)

type TextWidget struct {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions wtf/wtfable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Wtfable interface {
SetFocusChar(string)
TextView() *tview.TextView
HelpText() string
ConfigText() string

Height() int
Left() int
Expand Down

0 comments on commit 7a93824

Please sign in to comment.