gilo/internal/editor/editor.go

63 lines
926 B
Go
Raw Normal View History

2021-03-19 16:36:02 -04:00
package editor
2021-03-19 17:03:56 -04:00
import (
"bufio"
2021-03-19 17:39:15 -04:00
"os"
2021-03-19 17:03:56 -04:00
"timshome.page/gilo/internal/char"
"timshome.page/gilo/internal/terminal"
)
2021-03-23 15:51:59 -04:00
type Editor struct {
rows int
cols int
}
2021-03-19 17:39:15 -04:00
var reader = bufio.NewReader(os.Stdin)
func readKey() (rune, int) {
2021-03-19 17:03:56 -04:00
ch, size, err := reader.ReadRune()
if err != nil {
panic(err)
}
return ch, size
}
2021-03-22 09:12:39 -04:00
func drawRows() {
for y :=0; y < 24; y += 1 {
terminal.OutLn("~")
}
}
2021-03-19 17:39:15 -04:00
func RefreshScreen() {
terminal.ANSICode(terminal.ClearScreen)
terminal.ANSICode(terminal.ResetCursor)
2021-03-22 09:12:39 -04:00
drawRows()
terminal.ANSICode(terminal.ResetCursor)
2021-03-19 17:39:15 -04:00
}
func ProcessKeypress() bool {
ch, _ := readKey()
// Clean up on exit
if ch == char.Ctrl('q') {
terminal.ANSICode(terminal.ClearScreen)
terminal.ANSICode(terminal.ResetCursor)
return false
}
2021-03-19 17:03:56 -04:00
// Ugliest syntax structure ever?
switch {
case char.IsCtrl(ch):
terminal.OutLn("%d", ch)
default:
terminal.OutLn("%d ('%c')", ch, ch)
}
return true
}