mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-03-14 04:53:43 +03:00
Simplify the render's write() method and it now takes a slice of lines
This commit is contained in:
parent
0b19d41e0a
commit
f332bf2cc2
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type renderer struct {
|
type renderer struct {
|
||||||
@ -14,40 +15,30 @@ type renderer struct {
|
|||||||
TerminalHeight int
|
TerminalHeight int
|
||||||
}
|
}
|
||||||
|
|
||||||
// sync paints the whole area.
|
|
||||||
func (r *renderer) sync(content string) {
|
|
||||||
r.clear()
|
|
||||||
moveTo(r.Out, r.Y, 0)
|
|
||||||
r.write(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
// clear clears the viewport region.
|
// clear clears the viewport region.
|
||||||
func (r *renderer) clear() {
|
func (r *renderer) clear() {
|
||||||
b := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
moveTo(b, r.Y, 0)
|
moveTo(buf, r.Y, 0)
|
||||||
for i := 0; i < r.Height; i++ {
|
for i := 0; i < r.Height; i++ {
|
||||||
clearLine(b)
|
clearLine(buf)
|
||||||
cursorDown(b, 1)
|
cursorDown(buf, 1)
|
||||||
}
|
}
|
||||||
r.Out.Write(b.Bytes())
|
r.Out.Write(buf.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
// sync paints the whole area.
|
||||||
|
func (r *renderer) sync(lines []string) {
|
||||||
|
r.clear()
|
||||||
|
moveTo(r.Out, r.Y, 0)
|
||||||
|
r.write(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write writes to the set writer.
|
// write writes to the set writer.
|
||||||
func (r *renderer) write(s string) {
|
func (r *renderer) write(lines []string) {
|
||||||
if len(s) == 0 {
|
if len(lines) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
io.WriteString(r.Out, strings.Join(lines, "\r\n"))
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
for _, r := range []rune(s) {
|
|
||||||
if r == '\n' {
|
|
||||||
buf.WriteString("\r\n")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
buf.WriteRune(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
r.Out.Write(buf.Bytes())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Effectively scroll up. That is, insert a line at the top, scrolling
|
// Effectively scroll up. That is, insert a line at the top, scrolling
|
||||||
|
@ -62,7 +62,7 @@ func (m *Model) SetContent(s string) {
|
|||||||
m.lines = strings.Split(s, "\n")
|
m.lines = strings.Split(s, "\n")
|
||||||
|
|
||||||
lines := bounded(m.lines, m.YOffset, m.Height)
|
lines := bounded(m.lines, m.YOffset, m.Height)
|
||||||
m.r.sync(strings.Join(lines, "\n"))
|
m.r.sync(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ViewDown moves the view down by the number of lines in the viewport.
|
// ViewDown moves the view down by the number of lines in the viewport.
|
||||||
|
Loading…
Reference in New Issue
Block a user