Merge pull request #58 from sqshq/fix_segfalt_for_newlines_in_asciibox

Support newlines in asciibox
This commit is contained in:
Alexander Lukyanchikov 2019-08-24 09:23:24 -04:00 committed by GitHub
commit cec214dc0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 3 deletions

View File

@ -17,7 +17,6 @@ type AsciiBox struct {
*ui.Block *ui.Block
*data.Consumer *data.Consumer
alert *data.Alert alert *data.Alert
text string
ascii string ascii string
style ui.Style style ui.Style
render *fl.AsciiRender render *fl.AsciiRender
@ -58,8 +57,7 @@ func NewAsciiBox(c config.AsciiBoxConfig, palette console.Palette) *AsciiBox {
for { for {
select { select {
case sample := <-box.SampleChannel: case sample := <-box.SampleChannel:
box.text = strings.TrimSpace(sample.Value) box.renderText(sample)
box.ascii, _ = box.render.RenderOpts(box.text, box.options)
case alert := <-box.AlertChannel: case alert := <-box.AlertChannel:
box.alert = alert box.alert = alert
} }
@ -69,6 +67,16 @@ func NewAsciiBox(c config.AsciiBoxConfig, palette console.Palette) *AsciiBox {
return &box return &box
} }
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"
}
}
func (a *AsciiBox) Draw(buffer *ui.Buffer) { func (a *AsciiBox) Draw(buffer *ui.Buffer) {
buffer.Fill(ui.NewCell(' ', ui.NewStyle(ui.ColorBlack)), a.GetRect()) buffer.Fill(ui.NewCell(' ', ui.NewStyle(ui.ColorBlack)), a.GetRect())