From 754017a2db2d8a01dd31f77d1a799d1903c63e66 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 16 Nov 2022 21:20:48 -0500 Subject: [PATCH] Added Buffer interface, and clear method --- application.go | 4 ++-- buffer.go | 58 +++++++++++++++++++++++++++++++++++--------------- config.go | 4 ++-- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/application.go b/application.go index 0523ac1..a97ccd3 100644 --- a/application.go +++ b/application.go @@ -4,7 +4,7 @@ import "image" // Application represents an application. type Application struct { - Buffer + DamageBuffer title string icons []image.Image @@ -22,7 +22,7 @@ func (application *Application) Run () ( width, height := application.Size() if width < 1 { width = 80 } if height < 1 { height = 20 } - application.Buffer.SetSize(width, height) + application.DamageBuffer.SetSize(width, height) application.config.load() diff --git a/buffer.go b/buffer.go index 5580838..0609001 100644 --- a/buffer.go +++ b/buffer.go @@ -53,11 +53,22 @@ func (cell Cell) Rune () (content rune) { return } -// Buffer is a two dimensional text buffer that stores a grid of cells, as well -// as information stating whether each cell is clean or dirty. Cells are dirty -// by default, are only clean when marked as clean, and become dirty again when -// they are altered in some way. -type Buffer struct { +// Buffer represents a two dimensional text buffer. +type Buffer interface { + Size () (with, height int) + Cell (x, y int) (cell Cell) + SetColor (x, y int, color Color) + SetSize (with, height int) + SetStyle (x, y int, style Style) + SetRune (x, y int, content rune) + Clear () +} + +// DamageBuffer is a two dimensional text buffer that stores a grid of cells, as +// well as information stating whether each cell is clean or dirty. Cells are +// dirty by default, are only clean when marked as clean, and become dirty again +// when they are altered in some way. +type DamageBuffer struct { content []Cell clean []bool @@ -73,7 +84,7 @@ type Buffer struct { lock sync.RWMutex } -func (buffer *Buffer) isOutOfBounds (x, y int) (outOfBounds bool) { +func (buffer *DamageBuffer) isOutOfBounds (x, y int) (outOfBounds bool) { outOfBounds = x < 0 || y < 0 || @@ -83,7 +94,7 @@ func (buffer *Buffer) isOutOfBounds (x, y int) (outOfBounds bool) { } // Size returns the width and height of the buffer. -func (buffer *Buffer) Size () (width, height int) { +func (buffer *DamageBuffer) Size () (width, height int) { buffer.lock.RLock() defer buffer.lock.RUnlock() @@ -94,14 +105,14 @@ func (buffer *Buffer) Size () (width, height int) { // SetDot sets the buffer's text insertion position relative to the buffer // origin point (0, 0). -func (buffer *Buffer) SetDot (x, y int) { +func (buffer *DamageBuffer) SetDot (x, y int) { buffer.dot.x = x buffer.dot.y = y } // Cell returns the cell at the specified x and y coordinates. If the // coordinates are out of bounds, this method will return a blank cell. -func (buffer *Buffer) Cell (x, y int) (cell Cell) { +func (buffer *DamageBuffer) Cell (x, y int) (cell Cell) { buffer.lock.RLock() defer buffer.lock.RUnlock() @@ -111,7 +122,7 @@ func (buffer *Buffer) Cell (x, y int) (cell Cell) { } // SetColor sets the color of the cell at the specified x and y coordinates. -func (buffer *Buffer) SetColor (x, y int, color Color) { +func (buffer *DamageBuffer) SetColor (x, y int, color Color) { buffer.lock.RLock() defer buffer.lock.RUnlock() @@ -123,7 +134,7 @@ func (buffer *Buffer) SetColor (x, y int, color Color) { // SetSize sets the width and height of the buffer. This clears all data in the // buffer. If the width or height is negative, this method does nothing. -func (buffer *Buffer) SetSize (width, height int) { +func (buffer *DamageBuffer) SetSize (width, height int) { buffer.lock.Lock() defer buffer.lock.Unlock() @@ -138,7 +149,7 @@ func (buffer *Buffer) SetSize (width, height int) { } // SetStyle sets the style of the cell at the specified x and y coordinates. -func (buffer *Buffer) SetStyle (x, y int, style Style) { +func (buffer *DamageBuffer) SetStyle (x, y int, style Style) { buffer.lock.RLock() defer buffer.lock.RUnlock() @@ -149,14 +160,27 @@ func (buffer *Buffer) SetStyle (x, y int, style Style) { } // SetRune sets the rune of the cell at the specified x and y coordinates. -func (buffer *Buffer) SetRune (x, y int, content rune) { +func (buffer *DamageBuffer) SetRune (x, y int, content rune) { buffer.lock.RLock() defer buffer.lock.RUnlock() buffer.setRune(x, y, content) } -func (buffer *Buffer) setRune (x, y int, content rune) { +// Clear resets the entire buffer. +func (buffer *DamageBuffer) Clear () { + buffer.lock.RLock() + defer buffer.lock.RUnlock() + + for index := 0; index < len(buffer.content); index ++ { + buffer.clean[index] = false + buffer.content[index] = Cell { + color: ColorForeground, + } + } +} + +func (buffer *DamageBuffer) setRune (x, y int, content rune) { if buffer.isOutOfBounds(x, y) { return } index := x + y * buffer.width buffer.clean[index] = buffer.content[index].content == content @@ -165,7 +189,7 @@ func (buffer *Buffer) setRune (x, y int, content rune) { // Write writes data stored in a byte slice to the buffer at the current dot // position. This makes Buffer an io.Writer. -func (buffer *Buffer) Write (bytes []byte) (bytesWritten int, err error) { +func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) { buffer.lock.RLock() defer buffer.lock.RUnlock() @@ -183,7 +207,7 @@ func (buffer *Buffer) Write (bytes []byte) (bytesWritten int, err error) { // Clean returns whether or not the cell at the specified x and y coordinates is // clean. -func (buffer *Buffer) Clean (x, y int) (clean bool) { +func (buffer *DamageBuffer) Clean (x, y int) (clean bool) { buffer.lock.RLock() defer buffer.lock.RUnlock() @@ -193,7 +217,7 @@ func (buffer *Buffer) Clean (x, y int) (clean bool) { } // MarkClean marks the cell at the specified x and y coordinates as clean. -func (buffer *Buffer) MarkClean (x, y int) { +func (buffer *DamageBuffer) MarkClean (x, y int) { buffer.lock.RLock() defer buffer.lock.RUnlock() diff --git a/config.go b/config.go index 1cbf732..6075f2d 100644 --- a/config.go +++ b/config.go @@ -64,9 +64,9 @@ func (config *Config) load () { // green color.RGBA { R: 0x00, G: 0xFF, B: 0x00, A: 0xFF }, // blue - color.RGBA { R: 0x00, G: 0x00, B: 0xFF, A: 0xFF }, + color.RGBA { R: 0x00, G: 0x80, B: 0xFF, A: 0xFF }, // purple - color.RGBA { R: 0x80, G: 0x00, B: 0xFF, A: 0xFF }, + color.RGBA { R: 0x80, G: 0x40, B: 0xFF, A: 0xFF }, } config.fontName = "" config.fontSize = 11