gilo/editor/draw.go

103 lines
2.0 KiB
Go
Raw Normal View History

2021-03-26 16:18:03 -04:00
// Editor methods involved in drawing to the console
package editor
import (
"fmt"
"strings"
"timshome.page/gilo/terminal"
)
// ----------------------------------------------------------------------------
// !Editor Methods
// ----------------------------------------------------------------------------
func (e *editor) RefreshScreen() {
ab := newBuffer()
ab.append(terminal.HideCursor)
ab.append(terminal.ResetCursor)
e.drawRows(ab)
ab.append(terminal.MoveCursor(e.cursor.x, e.cursor.y))
ab.append(terminal.ShowCursor)
terminal.Write(ab.toString())
}
func (e *editor) drawRows(ab *buffer) {
2021-03-30 18:43:46 -04:00
for y :=0; y < e.screen.Rows; y++ {
if y >= len(e.rows) {
if len(e.rows) == 0 && y == e.screen.Rows / 3 {
2021-03-30 18:00:06 -04:00
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
if len(welcome) > e.screen.Cols {
welcome = truncateString(welcome, e.screen.Cols)
}
padding := (e.screen.Cols - len(welcome)) / 2
if padding > 0 {
ab.appendRune('~')
padding--
}
for padding > 0 {
padding--
ab.appendRune(' ')
}
ab.append(welcome)
} else {
2021-03-26 16:18:03 -04:00
ab.appendRune('~')
}
} else {
2021-03-30 18:43:46 -04:00
row := truncateString(string(e.rows[y].chars), e.screen.Cols)
ab.append(row)
2021-03-26 16:18:03 -04:00
}
ab.append(terminal.ClearLine)
if y < (e.screen.Rows - 1) {
ab.append("\r\n")
}
}
}
// ----------------------------------------------------------------------------
// !Output Buffer
// ----------------------------------------------------------------------------
type buffer struct {
buf *strings.Builder
}
func newBuffer() *buffer {
var buf strings.Builder
b := new(buffer)
b.buf = &buf
return b
}
func (b *buffer) appendRune(r rune) int {
size, _ := b.buf.WriteRune(r)
return size
}
func (b *buffer) append(s string) int {
size, _ := b.buf.WriteString(s)
return size
}
func (b *buffer) appendLn(s string) int {
str := fmt.Sprintf("%s\r\n", s)
size, _ := b.buf.WriteString(str)
return size
}
func (b *buffer) toString() string {
return b.buf.String()
}