2021-03-19 16:36:02 -04:00
|
|
|
package editor
|
2021-03-19 17:03:56 -04:00
|
|
|
|
|
|
|
import (
|
2021-03-24 15:09:28 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"timshome.page/gilo/internal/ansi"
|
2021-03-19 17:03:56 -04:00
|
|
|
"timshome.page/gilo/internal/char"
|
|
|
|
"timshome.page/gilo/internal/terminal"
|
|
|
|
)
|
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Editor
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
type editor struct {
|
2021-03-23 15:51:59 -04:00
|
|
|
rows int
|
|
|
|
cols int
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
func New() *editor {
|
2021-03-24 15:09:28 -04:00
|
|
|
rows, cols := terminal.Size()
|
2021-03-24 14:17:29 -04:00
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
return &editor{rows, cols}
|
2021-03-22 09:12:39 -04:00
|
|
|
}
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
func (e *editor) RefreshScreen() {
|
2021-03-24 15:09:28 -04:00
|
|
|
ab := newBuffer()
|
|
|
|
|
|
|
|
ab.append(ansi.ClearScreen)
|
|
|
|
ab.append(ansi.ResetCursor)
|
2021-03-22 09:12:39 -04:00
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
e.drawRows(ab)
|
2021-03-22 09:12:39 -04:00
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
ab.append(ansi.ResetCursor)
|
|
|
|
|
|
|
|
terminal.Write(ab.toString())
|
2021-03-19 17:39:15 -04:00
|
|
|
}
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
func (e *editor) ProcessKeypress() bool {
|
2021-03-24 13:24:40 -04:00
|
|
|
ch, _ := terminal.ReadKey()
|
2021-03-19 17:39:15 -04:00
|
|
|
|
|
|
|
// Clean up on exit
|
|
|
|
if ch == char.Ctrl('q') {
|
2021-03-24 15:09:28 -04:00
|
|
|
terminal.Write(ansi.ClearScreen)
|
|
|
|
terminal.Write(ansi.ResetCursor)
|
2021-03-19 17:39:15 -04:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2021-03-19 17:03:56 -04:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2021-03-24 14:17:29 -04:00
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
func (e *editor) drawRows(ab *buffer) {
|
2021-03-24 14:17:29 -04:00
|
|
|
for y :=0; y < e.rows; y += 1 {
|
2021-03-24 15:09:28 -04:00
|
|
|
ab.appendRune('~')
|
2021-03-24 14:17:29 -04:00
|
|
|
|
|
|
|
if y < (e.rows - 1) {
|
2021-03-24 15:09:28 -04:00
|
|
|
ab.append("\r\n")
|
2021-03-24 14:17:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 15:09:28 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !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()
|
|
|
|
}
|