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-26 03:29:23 +00:00
|
|
|
"github.com/sqshq/sampler/component/util"
|
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
|
|
|
)
|
|
|
|
|
2019-03-16 23:59:28 +00:00
|
|
|
func RenderAlert(alert *data.Alert, area image.Rectangle, buffer *ui.Buffer) {
|
2019-03-11 03:43:47 +00:00
|
|
|
|
2019-03-16 23:59:28 +00:00
|
|
|
if alert == nil {
|
2019-03-11 03:43:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-13 03:15:55 +00:00
|
|
|
color := console.ColorWhite
|
|
|
|
|
2019-03-16 23:59:28 +00:00
|
|
|
if alert.Color != nil {
|
|
|
|
color = *alert.Color
|
2019-03-13 03:15:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 03:29:23 +00:00
|
|
|
width := util.Max([]int{len(alert.Title), len(alert.Text)}) + 10
|
2019-03-13 03:15:55 +00:00
|
|
|
|
|
|
|
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-16 23:59:28 +00:00
|
|
|
strings.ToUpper(alert.Title), alert.Text), ui.NewStyle(console.ColorWhite)), uint(width))
|
2019-03-13 03:15:55 +00:00
|
|
|
|
|
|
|
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)))
|
2019-03-19 01:53:45 +00:00
|
|
|
block.BorderStyle = ui.Style{Fg: color, Bg: ui.ColorClear}
|
2019-03-13 03:15:55 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 03:29:23 +00:00
|
|
|
// TODO move to utils
|
2019-03-13 03:15:55 +00:00
|
|
|
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
|
|
|
}
|