1
1
mirror of https://github.com/walles/moar.git synced 2024-11-22 03:14:56 +03:00
moar/twin/fake-screen.go
Johan Walles 189e622658 Simplify last-cell-on-a-row-is-wide handling
By doing the clipping in SetCell(). This is less intrusive than doing it
in renderLine().
2024-09-20 08:04:56 +02:00

100 lines
2.2 KiB
Go

// Package twin provides Terminal Window interaction
package twin
// Used for testing.
//
// Try GetRow() after some SetCell() calls to see what you got.
type FakeScreen struct {
width int
height int
cells [][]StyledRune
}
func NewFakeScreen(width int, height int) *FakeScreen {
rows := make([][]StyledRune, height)
for i := 0; i < height; i++ {
rows[i] = make([]StyledRune, width)
}
return &FakeScreen{
width: width,
height: height,
cells: rows,
}
}
func (screen *FakeScreen) Close() {
// This method intentionally left blank
}
func (screen *FakeScreen) Clear() {
// This method's contents has been copied from UnixScreen.Clear()
empty := NewStyledRune(' ', StyleDefault)
width, height := screen.Size()
for row := 0; row < height; row++ {
for column := 0; column < width; column++ {
screen.cells[row][column] = empty
}
}
}
func (screen *FakeScreen) SetCell(column int, row int, styledRune StyledRune) int {
// This method's contents has been copied from UnixScreen.Clear()
if column < 0 {
return styledRune.Width()
}
if row < 0 {
return styledRune.Width()
}
width, height := screen.Size()
if column >= width {
return styledRune.Width()
}
if row >= height {
return styledRune.Width()
}
if column+styledRune.Width() > width {
// This cell is too wide for the screen, write a space instead
screen.cells[row][column] = NewStyledRune(' ', styledRune.Style)
return styledRune.Width()
}
screen.cells[row][column] = styledRune
return styledRune.Width()
}
func (screen *FakeScreen) Show() {
// This method intentionally left blank
}
func (screen *FakeScreen) ShowNLines(int) {
// This method intentionally left blank
}
func (screen *FakeScreen) Size() (width int, height int) {
return screen.width, screen.height
}
func (screen *FakeScreen) RequestTerminalBackgroundColor() {
// This method intentionally left blank
}
func (screen *FakeScreen) ShowCursorAt(_ int, _ int) {
// This method intentionally left blank
}
func (screen *FakeScreen) Events() chan Event {
// TODO: Do better here if or when this becomes a problem
return nil
}
func (screen *FakeScreen) GetRow(row int) []StyledRune {
return withoutHiddenRunes(screen.cells[row])
}