github.com/wtfutil/wtf@v0.43.0/view/base.go (about) 1 package view 2 3 import ( 4 "fmt" 5 "sync" 6 "time" 7 8 "github.com/rivo/tview" 9 "github.com/wtfutil/wtf/cfg" 10 "github.com/wtfutil/wtf/utils" 11 ) 12 13 type Base struct { 14 bordered bool 15 commonSettings *cfg.Common 16 enabled bool 17 enabledMutex *sync.Mutex 18 focusChar string 19 focusable bool 20 helpTextFunc func() string 21 name string 22 pages *tview.Pages 23 quitChan chan bool 24 refreshInterval time.Duration 25 refreshing bool 26 tviewApp *tview.Application 27 view *tview.TextView 28 29 RedrawChan chan bool 30 } 31 32 // NewBase creates and returns an instance of the Base module, the lowest-level 33 // primitive module from which all others are derived 34 func NewBase(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, commonSettings *cfg.Common) *Base { 35 base := &Base{ 36 commonSettings: commonSettings, 37 38 bordered: commonSettings.Bordered, 39 enabled: commonSettings.Enabled, 40 enabledMutex: &sync.Mutex{}, 41 focusChar: commonSettings.FocusChar(), 42 focusable: commonSettings.Focusable, 43 name: commonSettings.Name, 44 pages: pages, 45 quitChan: make(chan bool), 46 refreshInterval: commonSettings.RefreshInterval, 47 refreshing: false, 48 tviewApp: tviewApp, 49 50 RedrawChan: redrawChan, 51 } 52 53 return base 54 } 55 56 /* -------------------- Exported Functions -------------------- */ 57 58 // Bordered returns whether or not this widget should be drawn with a border 59 func (base *Base) Bordered() bool { 60 return base.bordered 61 } 62 63 // BorderColor returns the color that the border of this widget should be drawn in 64 func (base *Base) BorderColor() string { 65 if base.Focusable() { 66 return base.commonSettings.Colors.BorderTheme.Focusable 67 } 68 69 return base.commonSettings.Colors.BorderTheme.Unfocusable 70 } 71 72 func (base *Base) CommonSettings() *cfg.Common { 73 return base.commonSettings 74 } 75 76 func (base *Base) ConfigText() string { 77 return utils.HelpFromInterface(cfg.Common{}) 78 } 79 80 func (base *Base) ContextualTitle(defaultStr string) string { 81 switch { 82 case defaultStr == "" && base.FocusChar() == "": 83 return "" 84 case defaultStr != "" && base.FocusChar() == "": 85 return fmt.Sprintf(" %s ", defaultStr) 86 case defaultStr == "" && base.FocusChar() != "": 87 return fmt.Sprintf(" [darkgray::u]%s[::-][white] ", base.FocusChar()) 88 } 89 90 return fmt.Sprintf(" %s [darkgray::u]%s[::-][white] ", defaultStr, base.FocusChar()) 91 } 92 93 func (base *Base) Disable() { 94 base.enabledMutex.Lock() 95 base.enabled = false 96 base.enabledMutex.Unlock() 97 } 98 99 func (base *Base) Disabled() bool { 100 base.enabledMutex.Lock() 101 result := !base.enabled 102 base.enabledMutex.Unlock() 103 return result 104 } 105 106 func (base *Base) Enabled() bool { 107 base.enabledMutex.Lock() 108 result := base.enabled 109 base.enabledMutex.Unlock() 110 return result 111 } 112 113 func (base *Base) Focusable() bool { 114 base.enabledMutex.Lock() 115 result := base.enabled && base.focusable 116 base.enabledMutex.Unlock() 117 return result 118 } 119 120 func (base *Base) FocusChar() string { 121 return base.focusChar 122 } 123 124 func (base *Base) Name() string { 125 return base.name 126 } 127 128 func (base *Base) QuitChan() chan bool { 129 return base.quitChan 130 } 131 132 // Refreshing returns TRUE if the base is currently refreshing its data, FALSE if it is not 133 func (base *Base) Refreshing() bool { 134 return base.refreshing 135 } 136 137 // RefreshInterval returns how often the base will return its data 138 func (base *Base) RefreshInterval() time.Duration { 139 return base.refreshInterval 140 } 141 142 func (base *Base) SetFocusChar(char string) { 143 base.focusChar = char 144 } 145 146 // SetView assigns the passed-in tview.TextView view to this widget 147 func (base *Base) SetView(view *tview.TextView) { 148 base.view = view 149 } 150 151 // ShowHelp displays the modal help dialog for a module 152 func (base *Base) ShowHelp() { 153 if base.pages == nil { 154 return 155 } 156 157 closeFunc := func() { 158 base.pages.RemovePage("help") 159 base.tviewApp.SetFocus(base.view) 160 } 161 162 modal := NewBillboardModal(base.helpTextFunc(), closeFunc) 163 164 base.pages.AddPage("help", modal, false, true) 165 base.tviewApp.SetFocus(modal) 166 167 // Tell the app to force redraw the screen 168 base.RedrawChan <- true 169 } 170 171 func (base *Base) Stop() { 172 base.enabledMutex.Lock() 173 base.enabled = false 174 base.enabledMutex.Unlock() 175 base.quitChan <- true 176 } 177 178 func (base *Base) String() string { 179 return base.name 180 }