64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package widgets
|
|
|
|
import (
|
|
"ArinDash/apis/nina"
|
|
"ArinDash/util"
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"time"
|
|
|
|
"github.com/mum4k/termdash/cell"
|
|
"github.com/mum4k/termdash/terminal/terminalapi"
|
|
"github.com/mum4k/termdash/widgetapi"
|
|
"github.com/mum4k/termdash/widgets/text"
|
|
)
|
|
|
|
type NinaWarningsOptions struct {
|
|
}
|
|
|
|
func NinaWarnings() NinaWarningsOptions {
|
|
widgetOptions["NinaWarningsOptions"] = createNinaWarnings
|
|
return NinaWarningsOptions{}
|
|
}
|
|
|
|
func createNinaWarnings(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
|
|
widget := util.PanicOnErrorWithResult(text.New(text.WrapAtWords()))
|
|
|
|
go util.Periodic(ctx, 1*time.Minute, func() error {
|
|
warnings := nina.FetchWarnings()
|
|
widget.Reset()
|
|
for _, warning := range warnings {
|
|
for _, info := range warning.Info {
|
|
var options []cell.Option
|
|
if info.Severity == "Moderate" {
|
|
options = append(options, cell.FgColor(cell.ColorRed))
|
|
} else if info.Severity == "Minor" {
|
|
options = append(options, cell.FgColor(cell.ColorYellow))
|
|
} else if info.Severity == "Fine" {
|
|
options = append(options, cell.FgColor(cell.ColorGray))
|
|
} else if info.Severity == "Cancel" {
|
|
options = append(options, cell.FgColor(cell.ColorGreen))
|
|
} else {
|
|
options = append(options, cell.FgColor(cell.ColorRed))
|
|
options = append(options, cell.Blink())
|
|
}
|
|
if err := widget.Write(fmt.Sprintf("%s\n\n", info.Headline), text.WriteCellOpts(append(options, cell.Bold())...)); err != nil {
|
|
return err
|
|
}
|
|
if err := widget.Write(fmt.Sprintf("%s\n\n\n", removeElements(info.Description)), text.WriteCellOpts(options...)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
return widget
|
|
}
|
|
func removeElements(input string) string {
|
|
// Pattern to match <tag>content</tag> or <tag/>
|
|
re := regexp.MustCompile(`<a .*>.*</a>|<br>| `)
|
|
return re.ReplaceAllString(input, "")
|
|
}
|