mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Add character identifiers to focusable widgets
When no widget has focus, press the letter key to focus on the widget
assigned to that letter.
Example:
GitHub (d)
Press "d" to focus on the GitHub widget.
This commit is contained in:
@@ -72,10 +72,18 @@ func (widget *BarGraph) Focusable() bool {
|
||||
return widget.enabled && widget.focusable
|
||||
}
|
||||
|
||||
func (widget *BarGraph) FocusChar() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (widget *BarGraph) RefreshInterval() int {
|
||||
return widget.RefreshInt
|
||||
}
|
||||
|
||||
func (widget *BarGraph) SetFocusChar(char string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (widget *BarGraph) TextView() *tview.TextView {
|
||||
return widget.View
|
||||
}
|
||||
|
||||
@@ -22,6 +22,28 @@ type FocusTracker struct {
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
// AssignHotKeys assigns an alphabetic keyboard character to each focusable
|
||||
// widget so that the widget can be brought into focus by pressing that keyboard key
|
||||
func (tracker *FocusTracker) AssignHotKeys() {
|
||||
i := 0
|
||||
|
||||
for _, focusable := range tracker.focusables() {
|
||||
focusable.SetFocusChar(string('a' + i))
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
func (tracker *FocusTracker) FocusOn(char string) {
|
||||
for idx, focusable := range tracker.focusables() {
|
||||
if focusable.FocusChar() == char {
|
||||
tracker.blur(tracker.Idx)
|
||||
tracker.Idx = idx
|
||||
tracker.focus(tracker.Idx)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Next sets the focus on the next widget in the widget list. If the current widget is
|
||||
// the last widget, sets focus on the first widget.
|
||||
func (tracker *FocusTracker) Next() {
|
||||
|
||||
@@ -13,6 +13,7 @@ var Config *config.Config
|
||||
type TextWidget struct {
|
||||
enabled bool
|
||||
focusable bool
|
||||
focusChar string
|
||||
|
||||
Name string
|
||||
RefreshedAt time.Time
|
||||
@@ -53,6 +54,14 @@ func (widget *TextWidget) BorderColor() string {
|
||||
return Config.UString("wtf.colors.border.normal", "gray")
|
||||
}
|
||||
|
||||
func (widget *TextWidget) ContextualTitle(defaultStr string) string {
|
||||
if widget.FocusChar() == "" {
|
||||
return fmt.Sprintf(" %s ", defaultStr)
|
||||
} else {
|
||||
return fmt.Sprintf(" %s [darkgray](%s)[white] ", defaultStr, widget.FocusChar())
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *TextWidget) Disable() {
|
||||
widget.enabled = false
|
||||
}
|
||||
@@ -69,10 +78,18 @@ func (widget *TextWidget) Focusable() bool {
|
||||
return widget.enabled && widget.focusable
|
||||
}
|
||||
|
||||
func (widget *TextWidget) FocusChar() string {
|
||||
return widget.focusChar
|
||||
}
|
||||
|
||||
func (widget *TextWidget) RefreshInterval() int {
|
||||
return widget.RefreshInt
|
||||
}
|
||||
|
||||
func (widget *TextWidget) SetFocusChar(char string) {
|
||||
widget.focusChar = char
|
||||
}
|
||||
|
||||
func (widget *TextWidget) TextView() *tview.TextView {
|
||||
return widget.View
|
||||
}
|
||||
@@ -86,7 +103,7 @@ func (widget *TextWidget) addView() {
|
||||
view.SetBorder(true)
|
||||
view.SetBorderColor(colorFor(widget.BorderColor()))
|
||||
view.SetDynamicColors(true)
|
||||
view.SetTitle(widget.Name)
|
||||
view.SetTitle(widget.ContextualTitle(widget.Name))
|
||||
view.SetWrap(false)
|
||||
|
||||
widget.View = view
|
||||
|
||||
@@ -10,6 +10,8 @@ type Wtfable interface {
|
||||
|
||||
BorderColor() string
|
||||
Focusable() bool
|
||||
FocusChar() string
|
||||
SetFocusChar(string)
|
||||
TextView() *tview.TextView
|
||||
|
||||
Top() int
|
||||
|
||||
Reference in New Issue
Block a user