2017-11-25 18:30:50 +00:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2017-11-28 13:40:43 +00:00
|
|
|
ui "github.com/gizak/termui"
|
2017-11-25 18:30:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TextView struct {
|
|
|
|
ui.Block
|
2017-11-28 13:40:43 +00:00
|
|
|
inputStream <-chan string
|
2017-11-25 18:30:50 +00:00
|
|
|
render chan bool
|
|
|
|
Text []string // all the text
|
|
|
|
TextOut []string // text to be displayed
|
|
|
|
TextFgColor ui.Attribute
|
|
|
|
TextBgColor ui.Attribute
|
|
|
|
padding Padding
|
|
|
|
}
|
|
|
|
|
2017-11-28 13:40:43 +00:00
|
|
|
func NewTextView(lines <-chan string) *TextView {
|
2017-12-01 15:29:11 +00:00
|
|
|
t := &TextView{
|
2017-11-25 18:30:50 +00:00
|
|
|
Block: *ui.NewBlock(),
|
|
|
|
inputStream: lines,
|
|
|
|
render: make(chan bool),
|
|
|
|
Text: []string{},
|
|
|
|
TextOut: []string{},
|
|
|
|
TextFgColor: ui.ThemeAttr("menu.text.fg"),
|
|
|
|
TextBgColor: ui.ThemeAttr("menu.text.bg"),
|
|
|
|
padding: Padding{4, 2},
|
|
|
|
}
|
|
|
|
|
2017-12-01 15:29:11 +00:00
|
|
|
t.BorderFg = ui.ThemeAttr("menu.border.fg")
|
|
|
|
t.BorderLabelFg = ui.ThemeAttr("menu.label.fg")
|
|
|
|
t.Height = ui.TermHeight()
|
|
|
|
t.Width = ui.TermWidth()
|
2017-11-25 18:30:50 +00:00
|
|
|
|
2017-12-01 15:29:11 +00:00
|
|
|
t.readInputLoop()
|
|
|
|
t.renderLoop()
|
|
|
|
return t
|
2017-11-25 18:30:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 15:29:11 +00:00
|
|
|
func (t *TextView) Resize() {
|
2017-11-28 13:55:29 +00:00
|
|
|
ui.Clear()
|
2017-12-01 15:29:11 +00:00
|
|
|
t.Height = ui.TermHeight()
|
|
|
|
t.Width = ui.TermWidth()
|
|
|
|
t.render <- true
|
2017-11-28 13:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 15:29:11 +00:00
|
|
|
func (t *TextView) Buffer() ui.Buffer {
|
2017-11-25 18:30:50 +00:00
|
|
|
var cell ui.Cell
|
2017-12-01 15:29:11 +00:00
|
|
|
buf := t.Block.Buffer()
|
2017-11-25 18:30:50 +00:00
|
|
|
|
2017-12-01 15:29:11 +00:00
|
|
|
x := t.Block.X + t.padding[0]
|
|
|
|
y := t.Block.Y + t.padding[1]
|
2017-11-25 18:30:50 +00:00
|
|
|
|
2017-12-01 15:29:11 +00:00
|
|
|
for _, line := range t.TextOut {
|
2017-11-25 18:30:50 +00:00
|
|
|
for _, ch := range line {
|
2017-12-01 15:29:11 +00:00
|
|
|
cell = ui.Cell{Ch: ch, Fg: t.TextFgColor, Bg: t.TextBgColor}
|
2017-11-25 18:30:50 +00:00
|
|
|
buf.Set(x, y, cell)
|
|
|
|
x++
|
|
|
|
}
|
2017-12-01 15:29:11 +00:00
|
|
|
x = t.Block.X + t.padding[0]
|
2017-11-25 18:30:50 +00:00
|
|
|
y++
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2017-12-01 15:29:11 +00:00
|
|
|
func (t *TextView) renderLoop() {
|
2017-11-25 18:30:50 +00:00
|
|
|
go func() {
|
2017-12-01 15:29:11 +00:00
|
|
|
for range t.render {
|
|
|
|
maxWidth := t.Width - (t.padding[0] * 2)
|
|
|
|
height := t.Height - (t.padding[1] * 2)
|
|
|
|
t.TextOut = []string{}
|
|
|
|
for i := len(t.Text) - 1; i >= 0; i-- {
|
|
|
|
lines := splitLine(t.Text[i], maxWidth)
|
|
|
|
t.TextOut = append(lines, t.TextOut...)
|
|
|
|
if len(t.TextOut) > height {
|
|
|
|
t.TextOut = t.TextOut[:height]
|
|
|
|
break
|
|
|
|
}
|
2017-11-25 18:30:50 +00:00
|
|
|
}
|
2017-12-01 15:29:11 +00:00
|
|
|
ui.Render(t)
|
2017-11-25 18:30:50 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-12-01 15:29:11 +00:00
|
|
|
func (t *TextView) readInputLoop() {
|
2017-11-25 18:30:50 +00:00
|
|
|
go func() {
|
2017-12-01 15:29:11 +00:00
|
|
|
for line := range t.inputStream {
|
|
|
|
t.Text = append(t.Text, line)
|
|
|
|
t.render <- true
|
2017-11-25 18:30:50 +00:00
|
|
|
}
|
2017-12-01 15:29:11 +00:00
|
|
|
close(t.render)
|
2017-11-25 18:30:50 +00:00
|
|
|
}()
|
|
|
|
}
|
2017-12-01 15:29:11 +00:00
|
|
|
|
|
|
|
func splitLine(line string, lineSize int) []string {
|
|
|
|
var lines []string
|
|
|
|
for {
|
|
|
|
if len(line) < lineSize {
|
|
|
|
lines = append(lines, line)
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
lines = append(lines, line[:lineSize])
|
|
|
|
line = line[lineSize:]
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|