Move logic out of main package

This commit is contained in:
Timothy Warren 2021-03-19 17:03:56 -04:00
parent 2f73596d35
commit 64b7a51f69
3 changed files with 38 additions and 18 deletions

21
gilo.go
View File

@ -3,7 +3,8 @@ package main
import ( import (
"bufio" "bufio"
"os" "os"
"timshome.page/gilo/internal/rune"
"timshome.page/gilo/internal/editor"
"timshome.page/gilo/internal/terminal" "timshome.page/gilo/internal/terminal"
) )
@ -14,21 +15,7 @@ func main() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
for { for editor.ProcessKeypress(reader) {
char, _, err := reader.ReadRune() // loop!
if err != nil {
panic(err)
}
// Ugliest syntax structure ever?
switch {
case char == rune.Ctrl('q'):
terminal.OutLn("bye!")
return
case rune.IsCtrl(char):
terminal.OutLn("%d", char)
default:
terminal.OutLn("%d ('%c')", char, char)
}
} }
} }

View File

@ -1,4 +1,4 @@
package rune package char
func isAscii(char rune) bool { func isAscii(char rune) bool {
ord := int(char) ord := int(char)

View File

@ -1 +1,34 @@
package editor package editor
import (
"bufio"
"timshome.page/gilo/internal/char"
"timshome.page/gilo/internal/terminal"
)
func readKey(reader *bufio.Reader) (rune, int) {
ch, size, err := reader.ReadRune()
if err != nil {
panic(err)
}
return ch, size
}
func ProcessKeypress(reader *bufio.Reader) bool {
ch, _ := readKey(reader)
// Ugliest syntax structure ever?
switch {
case ch == char.Ctrl('q'):
terminal.OutLn("bye!")
return false
case char.IsCtrl(ch):
terminal.OutLn("%d", ch)
default:
terminal.OutLn("%d ('%c')", ch, ch)
}
return true
}