sampler-fork/component/asciibox/asciibox.go

74 lines
1.5 KiB
Go
Raw Normal View History

2019-02-17 23:00:00 +00:00
package asciibox
import (
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-02-17 23:00:00 +00:00
"github.com/sqshq/sampler/data"
ui "github.com/sqshq/termui"
"image"
)
type AsciiBox struct {
ui.Block
text string
ascii string
style ui.Style
render *fl.AsciiRender
options *fl.RenderOptions
}
type AsciiFont string
const (
AsciiFontFlat AsciiFont = "flat"
AsciiFont3D AsciiFont = "3d"
)
2019-02-26 04:36:23 +00:00
const asciiFontExtension = ".flf"
2019-02-17 23:00:00 +00:00
func NewAsciiBox(title string, font AsciiFont, color ui.Color) *AsciiBox {
block := *ui.NewBlock()
block.Title = title
options := fl.NewRenderOptions()
options.FontName = string(font)
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 {
panic("Can't load the font: " + err.Error())
}
render := fl.NewAsciiRender()
_ = render.LoadBindataFont(fontStr, options.FontName)
2019-02-17 23:00:00 +00:00
return &AsciiBox{
Block: block,
style: ui.NewStyle(color),
render: render,
options: options,
}
}
func (a *AsciiBox) ConsumeSample(sample data.Sample) {
a.text = sample.Value
a.ascii, _ = a.render.RenderOpts(sample.Value, a.options)
}
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
cells := ui.ParseText(a.ascii, a.style)
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))
}
}
}