2021-03-19 16:36:02 -04:00
|
|
|
package editor
|
2021-03-19 17:03:56 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"timshome.page/gilo/internal/char"
|
|
|
|
"timshome.page/gilo/internal/terminal"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
cols, rows := terminal.Size()
|
2021-03-24 13:24:40 -04:00
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
e := new(editor)
|
|
|
|
e.cols = cols
|
|
|
|
e.rows = rows
|
|
|
|
|
|
|
|
return e
|
2021-03-22 09:12:39 -04:00
|
|
|
}
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
func (e *editor) RefreshScreen() {
|
2021-03-19 17:39:15 -04:00
|
|
|
terminal.ANSICode(terminal.ClearScreen)
|
|
|
|
terminal.ANSICode(terminal.ResetCursor)
|
2021-03-22 09:12:39 -04:00
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
e.drawRows()
|
2021-03-22 09:12:39 -04:00
|
|
|
|
|
|
|
terminal.ANSICode(terminal.ResetCursor)
|
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') {
|
|
|
|
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):
|
2021-03-24 14:17:29 -04:00
|
|
|
terminal.WriteLn("%d", ch)
|
2021-03-19 17:03:56 -04:00
|
|
|
default:
|
2021-03-24 14:17:29 -04:00
|
|
|
terminal.WriteLn("%d ('%c')", ch, ch)
|
2021-03-19 17:03:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2021-03-24 14:17:29 -04:00
|
|
|
|
|
|
|
func (e *editor) drawRows() {
|
|
|
|
for y :=0; y < e.rows; y += 1 {
|
|
|
|
terminal.Write("~")
|
|
|
|
|
|
|
|
if y < (e.rows - 1) {
|
|
|
|
terminal.Write("\r\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|