sampler-fork/component/alerter.go

94 lines
1.8 KiB
Go
Raw Normal View History

2019-03-11 03:43:47 +00:00
package component
import (
2019-03-13 03:15:55 +00:00
"fmt"
2019-03-14 03:01:44 +00:00
ui "github.com/gizak/termui/v3"
2019-03-11 03:43:47 +00:00
"github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data"
"image"
2019-03-13 03:15:55 +00:00
"strings"
2019-03-11 03:43:47 +00:00
)
type Alerter struct {
channel <-chan data.Alert
alert *data.Alert
}
func NewAlerter(channel <-chan data.Alert) *Alerter {
alerter := Alerter{channel: channel}
alerter.consume()
return &alerter
}
func (a *Alerter) consume() {
go func() {
for {
select {
case alert := <-a.channel:
a.alert = &alert
}
}
}()
}
func (a *Alerter) RenderAlert(buffer *ui.Buffer, area image.Rectangle) {
if a.alert == nil {
return
}
2019-03-13 03:15:55 +00:00
color := console.ColorWhite
if a.alert.Color != nil {
color = *a.alert.Color
}
width := max(len(a.alert.Title), len(a.alert.Text)) + 10
if width > area.Dx() {
width = area.Dx()
}
2019-03-14 03:01:44 +00:00
cells := ui.WrapCells(ui.ParseStyles(fmt.Sprintf("%s\n%s\n",
2019-03-13 03:15:55 +00:00
strings.ToUpper(a.alert.Title), a.alert.Text), ui.NewStyle(console.ColorWhite)), uint(width))
var lines []string
line := ""
for i := 0; i < len(cells); i++ {
if cells[i].Rune == '\n' {
lines = append(lines, line)
line = ""
} else {
line += string(cells[i].Rune)
}
}
block := *ui.NewBlock()
block.SetRect(getRectCoordinates(area, width, len(lines)))
block.BorderStyle = ui.Style{Fg: color}
block.Draw(buffer)
buffer.Fill(ui.NewCell(' ', ui.NewStyle(console.ColorBlack)), block.Inner)
for i := 0; i < len(lines); i++ {
buffer.SetString(lines[i],
ui.NewStyle(color), getMiddlePoint(block.Inner, lines[i], i-1))
}
}
//TODO move to utils
func max(a int, b int) int {
if a > b {
return a
} else {
return b
}
}
func getRectCoordinates(area image.Rectangle, width int, height int) (int, int, int, int) {
x1 := area.Min.X + area.Dx()/2 - width/2
y1 := area.Min.Y + area.Dy()/2 - height
return x1, y1, x1 + width, y1 + height + 2
2019-03-11 03:43:47 +00:00
}