gilo/internal/editor/editor.go
2021-03-23 15:51:59 -04:00

63 lines
926 B
Go

package editor
import (
"bufio"
"os"
"timshome.page/gilo/internal/char"
"timshome.page/gilo/internal/terminal"
)
type Editor struct {
rows int
cols int
}
var reader = bufio.NewReader(os.Stdin)
func readKey() (rune, int) {
ch, size, err := reader.ReadRune()
if err != nil {
panic(err)
}
return ch, size
}
func drawRows() {
for y :=0; y < 24; y += 1 {
terminal.OutLn("~")
}
}
func RefreshScreen() {
terminal.ANSICode(terminal.ClearScreen)
terminal.ANSICode(terminal.ResetCursor)
drawRows()
terminal.ANSICode(terminal.ResetCursor)
}
func ProcessKeypress() bool {
ch, _ := 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.OutLn("%d", ch)
default:
terminal.OutLn("%d ('%c')", ch, ch)
}
return true
}