72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package component
|
|
|
|
import (
|
|
"fmt"
|
|
ui "github.com/gizak/termui/v3"
|
|
"github.com/sqshq/sampler/console"
|
|
"github.com/sqshq/sampler/data"
|
|
"image"
|
|
"strings"
|
|
)
|
|
|
|
func RenderAlert(alert *data.Alert, area image.Rectangle, buffer *ui.Buffer) {
|
|
|
|
if alert == nil {
|
|
return
|
|
}
|
|
|
|
color := console.ColorWhite
|
|
|
|
if alert.Color != nil {
|
|
color = *alert.Color
|
|
}
|
|
|
|
width := max(len(alert.Title), len(alert.Text)) + 10
|
|
|
|
if width > area.Dx() {
|
|
width = area.Dx()
|
|
}
|
|
|
|
cells := ui.WrapCells(ui.ParseStyles(fmt.Sprintf("%s\n%s\n",
|
|
strings.ToUpper(alert.Title), 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, Bg: ui.ColorClear}
|
|
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
|
|
}
|