Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: towards more optimal rendering in cOPs #17

Open
wants to merge 19 commits into
base: render-writer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions internal/cops/braille/braille.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import (
"github.com/borkshop/bork/internal/cops/display"
)

// BrailleAt returns the braille bitmap that coresponds to the 2x4 grid at the
// BitmapReader FIXME
type BitmapReader interface {
BitAt(x, y int) bool
Bounds() image.Rectangle
}

// At returns the braille bitmap that coresponds to the 2x4 grid at the
// given point in a bitmap.
func BrailleAt(src display.BitmapReader, sp image.Point) string {
func At(src BitmapReader, sp image.Point) string {
var r rune
if src.BitAt(sp.X, sp.Y) {
r |= 0x1
Expand Down Expand Up @@ -59,7 +65,7 @@ func Draw(dst *display.Display, r image.Rectangle, src image.Image, sp image.Poi
pt := image.Pt(x*3, y*6).Add(sp)
dx := r.Min.X + x
dy := r.Min.Y + y
br := BrailleAt(bits, pt)
br := At(bits, pt)
if br != "" {
dst.Text.Set(dx, dy, br)
dst.Foreground.Set(dx, dy, on)
Expand Down
78 changes: 32 additions & 46 deletions internal/cops/display/ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,42 @@ func init() {
}
}

func renderNoColor(buf []byte, c color.RGBA) []byte {
return buf
}

func renderBackgroundColor3(buf []byte, c color.RGBA) []byte {
return renderBackgroundColor(buf, Palette3, c)
}

func renderForegroundColor3(buf []byte, c color.RGBA) []byte {
return renderForegroundColor(buf, Palette3, c)
}

func renderBackgroundColor4(buf []byte, c color.RGBA) []byte {
return renderBackgroundColor(buf, Palette4, c)
}

func renderForegroundColor4(buf []byte, c color.RGBA) []byte {
return renderForegroundColor(buf, Palette4, c)
}

func renderBackgroundColor8(buf []byte, c color.RGBA) []byte {
return renderBackgroundColor(buf, Palette8, c)
}

func renderForegroundColor8(buf []byte, c color.RGBA) []byte {
return renderForegroundColor(buf, Palette8, c)
}

func renderForegroundColor(buf []byte, p color.Palette, c color.RGBA) []byte {
i := p.Index(c)
return append(buf, fgColorStrings[i]...)
}

func renderBackgroundColor(buf []byte, p color.Palette, c color.RGBA) []byte {
i := p.Index(c)
return append(buf, bgColorStrings[i]...)
}

func renderForegroundColor24(buf []byte, c color.RGBA) []byte {
if i, ok := colorIndex[c]; ok {
return append(buf, fgColorStrings[i]...)
func renderNoColor(buf []byte, cur Cursor, _, _ color.RGBA) ([]byte, Cursor) { return buf, cur }

func renderCompatColor24(buf []byte, cur Cursor, fg, bg color.RGBA) ([]byte, Cursor) {
if fg != cur.Foreground {
if i, ok := colorIndex[fg]; ok {
buf = append(buf, fgColorStrings[i]...)
} else {
buf = append(buf, "\033[38;2"...)
buf = renderColor24(buf, fg)
}
cur.Foreground = fg
}
return renderColor24(append(buf, "\033[38;2"...), c)
if bg != cur.Background {
if i, ok := colorIndex[bg]; ok {
buf = append(buf, bgColorStrings[i]...)
} else {
buf = append(buf, "\033[48;2"...)
buf = renderColor24(buf, bg)
}
cur.Background = bg
}
return buf, cur
}

func renderBackgroundColor24(buf []byte, c color.RGBA) []byte {
if i, ok := colorIndex[c]; ok {
return append(buf, bgColorStrings[i]...)
func renderJustColor24(buf []byte, cur Cursor, fg, bg color.RGBA) ([]byte, Cursor) {
if fg != cur.Foreground {
buf = append(buf, "\033[38;2"...)
buf = renderColor24(buf, fg)
cur.Foreground = fg
}
if bg != cur.Background {
buf = append(buf, "\033[48;2"...)
buf = renderColor24(buf, bg)
cur.Background = bg
}
return renderColor24(append(buf, "\033[48;2"...), c)
return buf, cur
}

func renderColor24(buf []byte, c color.RGBA) []byte {
Expand Down
113 changes: 74 additions & 39 deletions internal/cops/display/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,13 @@ func (c Cursor) Home(buf []byte) ([]byte, Cursor) {
return append(buf, "\033[H"...), c
}

// Go moves the cursor to another position, preferring to use relative motion,
// using line relative if the column is unknown, using display origin relative
// only if the line is also unknown. If the column is unknown, use "\r" to seek
// to column 0 of the same line.
func (c Cursor) Go(buf []byte, to image.Point) ([]byte, Cursor) {
func (c Cursor) recover(buf []byte, to image.Point) ([]byte, Cursor) {
if c.Position == Lost {
// If the cursor position is completely unknown, move relative to
// screen origin. This mode must be avoided to render relative to
// cursor position inline with a scrolling log, by setting the cursor
// position relative to an arbitrary origin before rendering.
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(to.Y+1), 10)
buf = append(buf, ";"...)
buf = strconv.AppendInt(buf, int64(to.X+1), 10)
buf = append(buf, "H"...)
c.Position = to
return buf, c
return c.jumpTo(buf, to)
}

if c.Position.X == -1 {
Expand All @@ -148,6 +138,70 @@ func (c Cursor) Go(buf []byte, to image.Point) ([]byte, Cursor) {
// Continue...
}

return buf, c
}

func (c Cursor) jumpTo(buf []byte, to image.Point) ([]byte, Cursor) {
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(to.Y+1), 10)
buf = append(buf, ";"...)
buf = strconv.AppendInt(buf, int64(to.X+1), 10)
buf = append(buf, "H"...)
c.Position = to
return buf, c
}

func (c Cursor) linedown(buf []byte, n int) ([]byte, Cursor) {
// Use \r\n to advance cursor Y on the chance it will advance the
// display bounds.
buf = append(buf, "\r\n"...)
for m := n - 1; m > 0; m-- {
buf = append(buf, "\n"...)
}
c.Position.X = 0
c.Position.Y += n
return buf, c
}

func (c Cursor) up(buf []byte, n int) ([]byte, Cursor) {
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(n), 10)
buf = append(buf, "A"...)
c.Position.Y -= n
return buf, c
}

func (c Cursor) down(buf []byte, n int) ([]byte, Cursor) {
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(n), 10)
buf = append(buf, "B"...)
c.Position.Y += n
return buf, c
}

func (c Cursor) left(buf []byte, n int) ([]byte, Cursor) {
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(n), 10)
buf = append(buf, "D"...)
c.Position.X -= n
return buf, c
}

func (c Cursor) right(buf []byte, n int) ([]byte, Cursor) {
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(n), 10)
buf = append(buf, "C"...)
c.Position.X += n
return buf, c
}

// Go moves the cursor to another position, preferring to use relative motion,
// using line relative if the column is unknown, using display origin relative
// only if the line is also unknown. If the column is unknown, use "\r" to seek
// to column 0 of the same line.
func (c Cursor) Go(buf []byte, to image.Point) ([]byte, Cursor) {
buf, c = c.recover(buf, to)

if to.X == 0 && to.Y == c.Position.Y+1 {
buf, c = c.Reset(buf)
buf = append(buf, "\r\n"...)
Expand All @@ -164,37 +218,18 @@ func (c Cursor) Go(buf []byte, to image.Point) ([]byte, Cursor) {
// skin tone emoji may or may not render as a single column glyph.
}

// DOWN
// Use \r\n to advance cursor Y on the chance it will advance the
// display bounds.
if to.Y > c.Position.Y {
buf = append(buf, "\r"...)
c.Position.X = 0
}
for to.Y > c.Position.Y {
buf = append(buf, "\n"...)
c.Position.Y++
if n := to.Y - c.Position.Y; n > 0 {
buf, c = c.linedown(buf, n)
} else if n < 0 {
buf, c = c.up(buf, -n)
}

// UP
if to.Y < c.Position.Y {
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(c.Position.Y-to.Y), 10)
buf = append(buf, "A"...)
if n := to.X - c.Position.X; n > 0 {
buf, c = c.right(buf, n)
} else if n < 0 {
buf, c = c.left(buf, -n)
}

// LEFT OR RIGHT
if to.X < c.Position.X {
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(c.Position.X-to.X), 10)
buf = append(buf, "D"...)
} else if to.X > c.Position.X {
buf = append(buf, "\033["...)
buf = strconv.AppendInt(buf, int64(to.X-c.Position.X), 10)
buf = append(buf, "C"...)
}

c.Position = to
return buf, c
}

Expand Down
Loading