2021-01-10 21:52:29 +01:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
2023-04-24 16:41:36 +02:00
|
|
|
"git.milar.in/milarin/slices"
|
2021-01-10 21:52:29 +01:00
|
|
|
"github.com/gdamore/tcell"
|
2023-04-24 16:41:36 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2021-01-10 21:52:29 +01:00
|
|
|
)
|
|
|
|
|
2022-04-01 20:10:51 +02:00
|
|
|
func drawBuffer(scr tcell.Screen, buf *ViewBuffer) {
|
2022-04-02 13:01:41 +02:00
|
|
|
buf.ForEachLine(func(y int, content []Rune) {
|
|
|
|
for x := 0; x < buf.Width(); x++ {
|
|
|
|
rn := content[x]
|
2023-04-24 16:41:36 +02:00
|
|
|
|
|
|
|
extraRuneCount := runewidth.RuneWidth(rn.Rn) - 1
|
|
|
|
var extraRunes []Rune
|
|
|
|
if x+1+extraRuneCount < len(content) {
|
|
|
|
extraRunes = content[x+1 : x+1+extraRuneCount]
|
2022-04-02 13:01:41 +02:00
|
|
|
} else {
|
2023-04-24 16:41:36 +02:00
|
|
|
extraRunes = content[x+1 : x+1]
|
2022-04-02 13:01:41 +02:00
|
|
|
}
|
2023-04-24 16:41:36 +02:00
|
|
|
|
|
|
|
scr.SetContent(x, y, rn.Rn, slices.Map(extraRunes, getRune), rn.Style)
|
|
|
|
x += extraRuneCount
|
2022-04-02 13:01:41 +02:00
|
|
|
}
|
2021-01-10 21:52:29 +01:00
|
|
|
})
|
2022-04-02 13:01:41 +02:00
|
|
|
|
2021-01-10 21:52:29 +01:00
|
|
|
scr.Show()
|
|
|
|
}
|
2022-04-02 13:01:41 +02:00
|
|
|
|
2023-04-24 16:41:36 +02:00
|
|
|
func getRune(rn Rune) rune {
|
|
|
|
return rn.Rn
|
|
|
|
}
|
|
|
|
|
2022-04-02 13:01:41 +02:00
|
|
|
func truncateBuffer(buf *ViewBuffer, w, h int) *ViewBuffer {
|
|
|
|
if w < 0 {
|
|
|
|
w = buf.Width()
|
|
|
|
}
|
|
|
|
if h < 0 {
|
|
|
|
h = buf.Height()
|
|
|
|
}
|
|
|
|
return buf.Sub(0, 0, w, h)
|
|
|
|
}
|