2019-02-17 23:00:00 +00:00
|
|
|
package asciibox
|
|
|
|
|
|
|
|
import (
|
2019-03-14 03:01:44 +00:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2019-02-26 04:36:23 +00:00
|
|
|
fl "github.com/mbndr/figlet4go"
|
2019-02-24 06:14:12 +00:00
|
|
|
"github.com/sqshq/sampler/asset"
|
2019-03-13 03:19:19 +00:00
|
|
|
"github.com/sqshq/sampler/component"
|
2019-03-08 04:04:46 +00:00
|
|
|
"github.com/sqshq/sampler/config"
|
2019-03-24 00:16:59 +00:00
|
|
|
"github.com/sqshq/sampler/console"
|
2019-03-16 23:59:28 +00:00
|
|
|
"github.com/sqshq/sampler/data"
|
2019-02-17 23:00:00 +00:00
|
|
|
"image"
|
2019-04-09 02:04:08 +00:00
|
|
|
"strings"
|
2019-02-17 23:00:00 +00:00
|
|
|
)
|
|
|
|
|
2019-07-27 04:15:35 +00:00
|
|
|
// AsciiBox represents a component with ascii-style text
|
2019-02-17 23:00:00 +00:00
|
|
|
type AsciiBox struct {
|
2019-03-16 23:59:28 +00:00
|
|
|
*ui.Block
|
|
|
|
*data.Consumer
|
|
|
|
alert *data.Alert
|
2019-02-17 23:00:00 +00:00
|
|
|
ascii string
|
|
|
|
style ui.Style
|
|
|
|
render *fl.AsciiRender
|
|
|
|
options *fl.RenderOptions
|
2019-03-24 00:16:59 +00:00
|
|
|
palette console.Palette
|
2019-02-17 23:00:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 04:36:23 +00:00
|
|
|
const asciiFontExtension = ".flf"
|
|
|
|
|
2019-03-24 00:16:59 +00:00
|
|
|
func NewAsciiBox(c config.AsciiBoxConfig, palette console.Palette) *AsciiBox {
|
2019-02-17 23:00:00 +00:00
|
|
|
|
|
|
|
options := fl.NewRenderOptions()
|
2019-03-08 04:04:46 +00:00
|
|
|
options.FontName = string(*c.Font)
|
2019-02-17 23:00:00 +00:00
|
|
|
|
2019-02-26 04:36:23 +00:00
|
|
|
fontStr, err := asset.Asset(options.FontName + asciiFontExtension)
|
2019-02-24 06:14:12 +00:00
|
|
|
if err != nil {
|
2019-05-20 04:14:13 +00:00
|
|
|
panic("Failed to load the font: " + err.Error())
|
2019-02-24 06:14:12 +00:00
|
|
|
}
|
2019-03-16 04:35:00 +00:00
|
|
|
|
2019-02-24 06:14:12 +00:00
|
|
|
render := fl.NewAsciiRender()
|
|
|
|
_ = render.LoadBindataFont(fontStr, options.FontName)
|
|
|
|
|
2019-03-24 00:16:59 +00:00
|
|
|
color := c.Color
|
|
|
|
if color == nil {
|
|
|
|
color = &palette.BaseColor
|
|
|
|
}
|
|
|
|
|
2019-03-08 04:04:46 +00:00
|
|
|
box := AsciiBox{
|
2019-04-10 02:34:45 +00:00
|
|
|
Block: component.NewBlock(c.Title, *c.Border, palette),
|
2019-03-16 23:59:28 +00:00
|
|
|
Consumer: data.NewConsumer(),
|
2019-03-24 00:16:59 +00:00
|
|
|
style: ui.NewStyle(*color),
|
2019-03-16 23:59:28 +00:00
|
|
|
render: render,
|
|
|
|
options: options,
|
2019-03-24 00:16:59 +00:00
|
|
|
palette: palette,
|
2019-02-17 23:00:00 +00:00
|
|
|
}
|
2019-03-08 04:04:46 +00:00
|
|
|
|
2019-03-16 04:35:00 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case sample := <-box.SampleChannel:
|
2019-08-24 13:15:07 +00:00
|
|
|
box.renderText(sample)
|
2019-03-16 23:59:28 +00:00
|
|
|
case alert := <-box.AlertChannel:
|
|
|
|
box.alert = alert
|
2019-03-16 04:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-03-08 04:04:46 +00:00
|
|
|
|
|
|
|
return &box
|
2019-02-17 23:00:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 13:15:07 +00:00
|
|
|
func (a *AsciiBox) renderText(sample *data.Sample) {
|
|
|
|
text := strings.TrimSpace(sample.Value)
|
|
|
|
lines := strings.Split(text, "\n")
|
|
|
|
a.ascii = ""
|
|
|
|
for _, line := range lines {
|
|
|
|
ascii, _ := a.render.RenderOpts(line, a.options)
|
|
|
|
a.ascii += ascii + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 23:00:00 +00:00
|
|
|
func (a *AsciiBox) Draw(buffer *ui.Buffer) {
|
|
|
|
|
|
|
|
buffer.Fill(ui.NewCell(' ', ui.NewStyle(ui.ColorBlack)), a.GetRect())
|
|
|
|
a.Block.Draw(buffer)
|
|
|
|
|
|
|
|
point := a.Inner.Min
|
2019-03-14 03:01:44 +00:00
|
|
|
cells := ui.ParseStyles(a.ascii, a.style)
|
2019-02-17 23:00:00 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(cells) && point.Y < a.Inner.Max.Y; i++ {
|
|
|
|
if cells[i].Rune == '\n' {
|
|
|
|
point = image.Pt(a.Inner.Min.X, point.Y+1)
|
|
|
|
} else if point.In(a.Inner) {
|
|
|
|
buffer.SetCell(cells[i], point)
|
|
|
|
point = point.Add(image.Pt(1, 0))
|
|
|
|
}
|
|
|
|
}
|
2019-03-16 23:59:28 +00:00
|
|
|
|
|
|
|
component.RenderAlert(a.alert, a.Rectangle, buffer)
|
2019-02-17 23:00:00 +00:00
|
|
|
}
|