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

Extract common funcitons from BarGraph and TextWidget into Base

This commit is contained in:
Chris Cummer 2019-08-04 23:00:09 -07:00
parent 275ea37a01
commit b6b695290c
3 changed files with 108 additions and 180 deletions

View File

@ -28,16 +28,7 @@ type Bar struct {
// NewBarGraph initialize your fancy new graph
func NewBarGraph(app *tview.Application, name string, commonSettings *cfg.Common, focusable bool) BarGraph {
widget := BarGraph{
Base: Base{
app: app,
bordered: commonSettings.Bordered,
commonSettings: commonSettings,
enabled: commonSettings.Enabled,
focusable: focusable,
name: commonSettings.Title,
quitChan: make(chan bool),
refreshInterval: commonSettings.RefreshInterval,
},
Base: NewBase(app, commonSettings, focusable),
maxStars: commonSettings.Config.UInt("graphStars", 20),
starChar: commonSettings.Config.UString("graphIcon", "|"),
@ -49,81 +40,12 @@ func NewBarGraph(app *tview.Application, name string, commonSettings *cfg.Common
return widget
}
func (widget *BarGraph) BorderColor() string {
if widget.Focusable() {
return widget.commonSettings.Colors.BorderFocusable
}
return widget.commonSettings.Colors.BorderNormal
}
func (widget *BarGraph) CommonSettings() *cfg.Common {
return widget.commonSettings
}
func (widget *BarGraph) Disable() {
widget.enabled = false
}
func (widget *BarGraph) Disabled() bool {
return !widget.Enabled()
}
func (widget *BarGraph) Enabled() bool {
return widget.enabled
}
func (widget *BarGraph) Focusable() bool {
return widget.enabled && widget.focusable
}
func (widget *BarGraph) FocusChar() string {
return ""
}
func (widget *BarGraph) Key() string {
return widget.key
}
func (widget *BarGraph) Name() string {
return widget.name
}
func (widget *BarGraph) QuitChan() chan bool {
return widget.quitChan
}
// Refreshing returns TRUE if the widget is currently refreshing its data, FALSE if it is not
func (widget *BarGraph) Refreshing() bool {
return widget.refreshing
}
// RefreshInterval returns how often, in seconds, the widget will return its data
func (widget *BarGraph) RefreshInterval() int {
return widget.refreshInterval
}
func (widget *BarGraph) SetFocusChar(char string) {
return
}
func (widget *BarGraph) Stop() {
widget.enabled = false
widget.quitChan <- true
}
/* -------------------- Exported Functions -------------------- */
func (widget *BarGraph) TextView() *tview.TextView {
return widget.View
}
func (widget *BarGraph) HelpText() string {
return "No help available for this widget"
}
func (widget *BarGraph) ConfigText() string {
return ""
}
// BuildBars will build a string of * to represent your data of [time][value]
// time should be passed as a int64
func (widget *BarGraph) BuildBars(data []Bar) {

View File

@ -1,8 +1,11 @@
package view
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/utils"
)
type Base struct {
@ -12,9 +15,110 @@ type Base struct {
enabled bool
focusChar string
focusable bool
key string
name string
quitChan chan bool
refreshing bool
refreshInterval int
}
func NewBase(app *tview.Application, commonSettings *cfg.Common, focusable bool) Base {
base := Base{
commonSettings: commonSettings,
app: app,
bordered: commonSettings.Bordered,
enabled: commonSettings.Enabled,
focusChar: commonSettings.FocusChar(),
focusable: focusable,
name: commonSettings.Name,
quitChan: make(chan bool),
refreshInterval: commonSettings.RefreshInterval,
refreshing: false,
}
return base
}
/* -------------------- Exported Functions -------------------- */
// Bordered returns whether or not this widget should be drawn with a border
func (base *Base) Bordered() bool {
return base.bordered
}
func (base *Base) BorderColor() string {
if base.Focusable() {
return base.commonSettings.Colors.BorderFocusable
}
return base.commonSettings.Colors.BorderNormal
}
func (base *Base) CommonSettings() *cfg.Common {
return base.commonSettings
}
func (base *Base) ConfigText() string {
return utils.HelpFromInterface(cfg.Common{})
}
func (base *Base) ContextualTitle(defaultStr string) string {
if base.FocusChar() == "" {
return fmt.Sprintf(" %s ", defaultStr)
}
return fmt.Sprintf(" %s [darkgray::u]%s[::-][green] ", defaultStr, base.FocusChar())
}
func (base *Base) Disable() {
base.enabled = false
}
func (base *Base) Disabled() bool {
return !base.enabled
}
func (base *Base) Enabled() bool {
return base.enabled
}
func (base *Base) Focusable() bool {
return base.enabled && base.focusable
}
func (base *Base) FocusChar() string {
return base.focusChar
}
func (base *Base) HelpText() string {
return fmt.Sprintf("\n There is no help available for widget %s", base.commonSettings.Module.Type)
}
func (base *Base) Name() string {
return base.name
}
func (base *Base) QuitChan() chan bool {
return base.quitChan
}
// Refreshing returns TRUE if the base is currently refreshing its data, FALSE if it is not
func (base *Base) Refreshing() bool {
return base.refreshing
}
// RefreshInterval returns how often, in seconds, the base will return its data
func (base *Base) RefreshInterval() int {
return base.refreshInterval
}
func (base *Base) SetFocusChar(char string) {
base.focusChar = char
}
func (base *Base) Stop() {
base.enabled = false
base.quitChan <- true
}
func (base *Base) String() string {
return base.name
}

View File

@ -1,11 +1,8 @@
package view
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/wtf"
)
@ -16,18 +13,7 @@ type TextWidget struct {
func NewTextWidget(app *tview.Application, commonSettings *cfg.Common, focusable bool) TextWidget {
widget := TextWidget{
Base: Base{
commonSettings: commonSettings,
app: app,
bordered: commonSettings.Bordered,
enabled: commonSettings.Enabled,
focusChar: commonSettings.FocusChar(),
focusable: focusable,
name: commonSettings.Name,
quitChan: make(chan bool),
refreshInterval: commonSettings.RefreshInterval,
refreshing: false,
},
Base: NewBase(app, commonSettings, focusable),
}
widget.View = widget.addView()
@ -38,90 +24,6 @@ func NewTextWidget(app *tview.Application, commonSettings *cfg.Common, focusable
/* -------------------- Exported Functions -------------------- */
// Bordered returns whether or not this widget should be drawn with a border
func (widget *TextWidget) Bordered() bool {
return widget.bordered
}
func (widget *TextWidget) BorderColor() string {
if widget.Focusable() {
return widget.commonSettings.Colors.BorderFocusable
}
return widget.commonSettings.Colors.BorderNormal
}
func (widget *TextWidget) CommonSettings() *cfg.Common {
return widget.commonSettings
}
func (widget *TextWidget) ConfigText() string {
return utils.HelpFromInterface(cfg.Common{})
}
func (widget *TextWidget) ContextualTitle(defaultStr string) string {
if widget.FocusChar() == "" {
return fmt.Sprintf(" %s ", defaultStr)
}
return fmt.Sprintf(" %s [darkgray::u]%s[::-][green] ", defaultStr, widget.FocusChar())
}
func (widget *TextWidget) Disable() {
widget.enabled = false
}
func (widget *TextWidget) Disabled() bool {
return !widget.Enabled()
}
func (widget *TextWidget) Enabled() bool {
return widget.enabled
}
func (widget *TextWidget) Focusable() bool {
return widget.enabled && widget.focusable
}
func (widget *TextWidget) FocusChar() string {
return widget.focusChar
}
func (widget *TextWidget) HelpText() string {
return fmt.Sprintf("\n There is no help available for widget %s", widget.commonSettings.Module.Type)
}
func (widget *TextWidget) QuitChan() chan bool {
return widget.quitChan
}
func (widget *TextWidget) Name() string {
return widget.name
}
// Refreshing returns TRUE if the widget is currently refreshing its data, FALSE if it is not
func (widget *TextWidget) Refreshing() bool {
return widget.refreshing
}
// RefreshInterval returns how often, in seconds, the widget will return its data
func (widget *TextWidget) RefreshInterval() int {
return widget.refreshInterval
}
func (widget *TextWidget) SetFocusChar(char string) {
widget.focusChar = char
}
func (widget *TextWidget) Stop() {
widget.enabled = false
widget.quitChan <- true
}
func (widget *TextWidget) String() string {
return widget.name
}
func (widget *TextWidget) TextView() *tview.TextView {
return widget.View
}