1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/grafana/widget.go
Chris Cummer fd794707cd
☢️ WTF-1031 Support multiple simultaneous configurations (#1032)
* WTF-1031 Rename WtfApp.app to WtfApp.tviewApp

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 Add scaffolding for main to support multiple WtfApp instances

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 WIP

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Remove common functionality from KeyboardWidget and into Base

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Augment with some descriptive comments

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Add full support for multiple app instances via the AppManager.

Still to do:

* Config support for multiple apps/multiple config files
* The ability to switch between apps

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Move SetTerminal out of main and into its own file

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-12-21 03:25:41 -08:00

99 lines
2.1 KiB
Go

package grafana
import (
"fmt"
"strconv"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
)
type Widget struct {
view.TextWidget
Client *Client
Alerts []Alert
Err error
Selected int
settings *Settings
}
func NewWidget(tviewApp *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(tviewApp, nil, settings.Common),
Client: NewClient(settings),
Selected: -1,
settings: settings,
}
widget.initializeKeyboardControls()
widget.View.SetRegions(true)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
alerts, err := widget.Client.Alerts()
if err != nil {
widget.Err = err
widget.Alerts = nil
} else {
widget.Err = nil
widget.Alerts = alerts
}
widget.Redraw(widget.content)
}
// GetSelected returns the index of the currently highlighted item as an int
func (widget *Widget) GetSelected() int {
if widget.Selected < 0 {
return 0
}
return widget.Selected
}
// Next cycles the currently highlighted text down
func (widget *Widget) Next() {
widget.Selected++
if widget.Selected >= len(widget.Alerts) {
widget.Selected = 0
}
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
}
// Prev cycles the currently highlighted text up
func (widget *Widget) Prev() {
widget.Selected--
if widget.Selected < 0 {
widget.Selected = len(widget.Alerts) - 1
}
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
}
// Unselect stops highlighting the text and jumps the scroll position to the top
func (widget *Widget) Unselect() {
widget.Selected = -1
widget.View.Highlight()
widget.View.ScrollToBeginning()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) openAlert() {
currentSelection := widget.View.GetHighlights()
if widget.Selected >= 0 && currentSelection[0] != "" {
url := widget.Alerts[widget.GetSelected()].URL
if url[0] == '/' {
url = fmt.Sprintf("%s%s", widget.settings.baseURI, url)
}
utils.OpenFile(url)
}
}