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:
		
							parent
							
								
									9a877b5e04
								
							
						
					
					
						commit
						d8f337884f
					
				| @ -51,15 +51,15 @@ type Sigils struct { | |||||||
| type Common struct { | type Common struct { | ||||||
| 	Colors | 	Colors | ||||||
| 	Module | 	Module | ||||||
| 	Position | 	Position `help:"Defines where in the grid this module’s widget will be displayed."` | ||||||
| 	Sigils | 	Sigils | ||||||
| 
 | 
 | ||||||
| 	Enabled         bool | 	Enabled         bool `help:"Determines whether or not this module is executed and if its data displayed onscreen." values:"true, false"` | ||||||
| 	RefreshInterval int | 	RefreshInterval int  `help:"How often, in seconds, this module will update its data." values:"A positive integer, 0..n." optional:"true"` | ||||||
| 	Title           string | 	Title           string | ||||||
| 	Config          *config.Config | 	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 { | func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config.Config, globalSettings *config.Config) *Common { | ||||||
|  | |||||||
| @ -3,9 +3,9 @@ package help | |||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 
 | 
 | ||||||
| 	//"github.com/wtfutil/wtf/cfg" |  | ||||||
| 	"github.com/olebedev/config" | 	"github.com/olebedev/config" | ||||||
| 	"github.com/wtfutil/wtf/maker" | 	"github.com/wtfutil/wtf/maker" | ||||||
|  | 	"github.com/wtfutil/wtf/utils" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func Display(moduleName string, config *config.Config) { | 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 { | func helpFor(moduleName string, config *config.Config) string { | ||||||
| 	widget := maker.MakeWidget(nil, nil, moduleName, moduleName, config, config) | 	widget := maker.MakeWidget(nil, nil, moduleName, moduleName, config, config) | ||||||
| 	return widget.HelpText() | 	result := "" | ||||||
|  | 	result += utils.StripColorTags(widget.HelpText()) | ||||||
|  | 	result += widget.ConfigText() | ||||||
|  | 	return result | ||||||
| } | } | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ package git | |||||||
| import ( | import ( | ||||||
| 	"github.com/olebedev/config" | 	"github.com/olebedev/config" | ||||||
| 	"github.com/wtfutil/wtf/cfg" | 	"github.com/wtfutil/wtf/cfg" | ||||||
|  | 	"github.com/wtfutil/wtf/utils" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const defaultTitle = "Git" | const defaultTitle = "Git" | ||||||
| @ -10,10 +11,10 @@ const defaultTitle = "Git" | |||||||
| type Settings struct { | type Settings struct { | ||||||
| 	common *cfg.Common | 	common *cfg.Common | ||||||
| 
 | 
 | ||||||
| 	commitCount  int | 	commitCount  int           `help:"The number of past commits to display." values:"A positive integer, 0..n."` | ||||||
| 	commitFormat string | 	commitFormat string        `help:"The string format for the commit message." optional:"true"` | ||||||
| 	dateFormat   string | 	dateFormat   string        `help:"The string format for the date/time in the commit message." optional:"true"` | ||||||
| 	repositories []interface{} | 	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 { | 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 | 	return &settings | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func (widget *Widget) ConfigText() string { | ||||||
|  | 	return utils.HelpFromInterface(Settings{}) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (widget *Widget) HelpText() string { | ||||||
|  | 	return widget.KeyboardWidget.HelpText() | ||||||
|  | } | ||||||
|  | |||||||
| @ -85,10 +85,6 @@ func (widget *Widget) Refresh() { | |||||||
| 	widget.display() | 	widget.display() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (widget *Widget) HelpText() string { |  | ||||||
| 	return widget.KeyboardWidget.HelpText() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /* -------------------- Unexported Functions -------------------- */ | /* -------------------- Unexported Functions -------------------- */ | ||||||
| 
 | 
 | ||||||
| func (widget *Widget) addCheckoutButton(form *tview.Form, fctn func()) { | func (widget *Widget) addCheckoutButton(form *tview.Form, fctn func()) { | ||||||
|  | |||||||
							
								
								
									
										77
									
								
								utils/help_parser.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								utils/help_parser.go
									
									
									
									
									
										Normal 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 | ||||||
|  | } | ||||||
| @ -116,6 +116,10 @@ func (widget *BarGraph) HelpText() string { | |||||||
| 	return "No help available for this widget" | 	return "No help available for this widget" | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (widget *BarGraph) ConfigText() string { | ||||||
|  | 	return "" | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /* -------------------- Unexported Functions -------------------- */ | /* -------------------- Unexported Functions -------------------- */ | ||||||
| 
 | 
 | ||||||
| func (widget *BarGraph) addView() *tview.TextView { | func (widget *BarGraph) addView() *tview.TextView { | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ import ( | |||||||
| 
 | 
 | ||||||
| 	"github.com/rivo/tview" | 	"github.com/rivo/tview" | ||||||
| 	"github.com/wtfutil/wtf/cfg" | 	"github.com/wtfutil/wtf/cfg" | ||||||
|  | 	"github.com/wtfutil/wtf/utils" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type TextWidget struct { | 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) | 	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 -------------------- */ | /* -------------------- Unexported Functions -------------------- */ | ||||||
| 
 | 
 | ||||||
| func (widget *TextWidget) addView() *tview.TextView { | func (widget *TextWidget) addView() *tview.TextView { | ||||||
|  | |||||||
| @ -15,6 +15,7 @@ type Wtfable interface { | |||||||
| 	SetFocusChar(string) | 	SetFocusChar(string) | ||||||
| 	TextView() *tview.TextView | 	TextView() *tview.TextView | ||||||
| 	HelpText() string | 	HelpText() string | ||||||
|  | 	ConfigText() string | ||||||
| 
 | 
 | ||||||
| 	Height() int | 	Height() int | ||||||
| 	Left() int | 	Left() int | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user