63 lines
976 B
Go
63 lines
976 B
Go
package editor
|
|
|
|
import (
|
|
"timshome.page/gilo/internal/char"
|
|
"timshome.page/gilo/internal/terminal"
|
|
)
|
|
|
|
type editor struct {
|
|
rows int
|
|
cols int
|
|
}
|
|
|
|
func New() *editor {
|
|
cols, rows := terminal.Size()
|
|
|
|
e := new(editor)
|
|
e.cols = cols
|
|
e.rows = rows
|
|
|
|
return e
|
|
}
|
|
|
|
func (e *editor) RefreshScreen() {
|
|
terminal.ANSICode(terminal.ClearScreen)
|
|
terminal.ANSICode(terminal.ResetCursor)
|
|
|
|
e.drawRows()
|
|
|
|
terminal.ANSICode(terminal.ResetCursor)
|
|
}
|
|
|
|
func (e *editor) ProcessKeypress() bool {
|
|
ch, _ := terminal.ReadKey()
|
|
|
|
// Clean up on exit
|
|
if ch == char.Ctrl('q') {
|
|
terminal.ANSICode(terminal.ClearScreen)
|
|
terminal.ANSICode(terminal.ResetCursor)
|
|
|
|
return false
|
|
}
|
|
|
|
// Ugliest syntax structure ever?
|
|
switch {
|
|
case char.IsCtrl(ch):
|
|
terminal.WriteLn("%d", ch)
|
|
default:
|
|
terminal.WriteLn("%d ('%c')", ch, ch)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (e *editor) drawRows() {
|
|
for y :=0; y < e.rows; y += 1 {
|
|
terminal.Write("~")
|
|
|
|
if y < (e.rows - 1) {
|
|
terminal.Write("\r\n")
|
|
}
|
|
}
|
|
}
|