95 lines
1.7 KiB
Go
95 lines
1.7 KiB
Go
package editor
|
|
|
|
import (
|
|
"timshome.page/gilo/fn"
|
|
"timshome.page/gilo/terminal"
|
|
)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// !Editor
|
|
// ----------------------------------------------------------------------------
|
|
|
|
type cursor struct {
|
|
x int
|
|
y int
|
|
}
|
|
|
|
type editor struct {
|
|
screen *terminal.Screen
|
|
cursor *cursor
|
|
}
|
|
|
|
func New() *editor {
|
|
screen := terminal.Size()
|
|
cursor := &cursor { 0, 0 }
|
|
|
|
return &editor{screen, cursor }
|
|
}
|
|
|
|
func (e *editor) ProcessKeypress() bool {
|
|
ch, size := terminal.ReadKey()
|
|
var runes []rune
|
|
|
|
for ; size != 0; ch, size = terminal.ReadKey() {
|
|
if ch == fn.Ctrl('q') {
|
|
// Clean up on exit
|
|
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
|
|
|
|
return false
|
|
}
|
|
|
|
runes = append(runes, ch)
|
|
|
|
print(runes)
|
|
}
|
|
|
|
terminal.Write("%v", runes)
|
|
|
|
return false
|
|
|
|
//str = terminal.Read()
|
|
//
|
|
//// Handle simplest inputs first
|
|
//ch, _ := terminal.ReadKey()
|
|
|
|
|
|
//runes := terminal.Read()
|
|
//str := string(runes)
|
|
//
|
|
//terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
|
|
//terminal.WriteLn(fmt.Sprintf("%v\r\r", runes))
|
|
//
|
|
////// Escape sequences can be less fun...
|
|
//if strings.Contains(str, terminal.EscPrefix) {
|
|
// code := strings.TrimPrefix(str, terminal.EscPrefix)
|
|
//
|
|
// switch code {
|
|
// case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight:
|
|
// e.moveCursor(code)
|
|
// return true
|
|
// default:
|
|
// // Do something later
|
|
// }
|
|
//}
|
|
//
|
|
//return true
|
|
}
|
|
|
|
//func (e *editor) processEscapeCode() bool {
|
|
//
|
|
//}
|
|
|
|
func (e *editor) moveCursor (rawKey string) {
|
|
key := keyMap[rawKey]
|
|
|
|
switch key {
|
|
case keyLeft:
|
|
e.cursor.x -= 1
|
|
case keyRight:
|
|
e.cursor.x += 1
|
|
case keyUp:
|
|
e.cursor.y -= 1
|
|
case keyDown:
|
|
e.cursor.y += 1
|
|
}
|
|
} |