started work on new buffer

This commit is contained in:
Liam Galvin 2018-08-05 22:34:51 +01:00
parent cc2aa87e5d
commit 89b8002d48
1 changed files with 14 additions and 13 deletions

View File

@ -6,48 +6,49 @@ import (
type Buffer struct {
lines []line
x int
y int
columnCount int
lineCount int
x uint16
y uint16
columnCount uint16
viewHeight uint16
}
// NewBuffer creates a new terminal buffer
func NewBuffer(columns int) *Buffer {
func NewBuffer() *Buffer {
return &Buffer{
x: 0,
y: 0,
lines: []line{},
columnCount: columns,
columnCount: 0,
}
}
// Column returns cursor column
func (buffer *Buffer) Column() int {
func (buffer *Buffer) Column() uint16 {
return buffer.x
}
// Line returns cursor line
func (buffer *Buffer) Line() int {
func (buffer *Buffer) Line() uint16 {
return buffer.y
}
// Width returns the width of the buffer in columns
func (buffer *Buffer) Width() int {
func (buffer *Buffer) Width() uint16 {
return buffer.columnCount
}
// Write will write a rune to the terminal at the position of the cursor, and increment the cursor position
func (buffer *Buffer) Write(r rune) {
for buffer.Line() >= len(buffer.lines) {
for int(buffer.Line()) >= len(buffer.lines) {
buffer.lines = append(buffer.lines, newLine())
}
line := &buffer.lines[buffer.Line()]
for buffer.Column() >= len(line.cells) {
for int(buffer.Column()) >= len(line.cells) {
line.cells = append(line.cells, newCell())
}
cell := line.cells[buffer.Column()]
cell.setRune(r)
buffer.incrementCursorPosition()
}
func (buffer *Buffer) incrementCursorPosition() {
@ -60,7 +61,7 @@ func (buffer *Buffer) incrementCursorPosition() {
}
}
func (buffer *Buffer) SetPosition(col int, line int) error {
func (buffer *Buffer) SetPosition(col uint16, line uint16) error {
if buffer.x >= buffer.Width() {
return fmt.Errorf("Cannot set cursor position: column %d is outside of the current buffer width (%d columns)", col, buffer.Width())
}
@ -69,6 +70,6 @@ func (buffer *Buffer) SetPosition(col int, line int) error {
return nil
}
func (buffer *Buffer) SetSize(cols int, lines int) {
func (buffer *Buffer) Resize(cols int, lines int) {
}