2021-03-19 16:36:02 -04:00
|
|
|
package editor
|
2021-03-19 17:03:56 -04:00
|
|
|
|
|
|
|
import (
|
2021-03-24 15:09:28 -04:00
|
|
|
"fmt"
|
2021-03-26 12:01:17 -04:00
|
|
|
"strings"
|
2021-03-24 15:52:35 -04:00
|
|
|
"timshome.page/gilo/fn"
|
2021-03-24 16:23:17 -04:00
|
|
|
"timshome.page/gilo/terminal"
|
2021-03-19 17:03:56 -04:00
|
|
|
)
|
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Editor
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-03-25 12:27:48 -04:00
|
|
|
type cursor struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
type editor struct {
|
2021-03-25 12:27:48 -04:00
|
|
|
screen *terminal.Screen
|
|
|
|
cursor *cursor
|
2021-03-23 15:51:59 -04:00
|
|
|
}
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
func New() *editor {
|
2021-03-25 12:27:48 -04:00
|
|
|
screen := terminal.Size()
|
|
|
|
cursor := &cursor { 0, 0 }
|
2021-03-24 14:17:29 -04:00
|
|
|
|
2021-03-25 12:27:48 -04:00
|
|
|
return &editor{screen, cursor }
|
2021-03-22 09:12:39 -04:00
|
|
|
}
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
func (e *editor) RefreshScreen() {
|
2021-03-24 15:09:28 -04:00
|
|
|
ab := newBuffer()
|
|
|
|
|
2021-03-24 16:23:17 -04:00
|
|
|
ab.append(terminal.HideCursor)
|
|
|
|
ab.append(terminal.ResetCursor)
|
2021-03-22 09:12:39 -04:00
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
e.drawRows(ab)
|
2021-03-25 12:46:53 -04:00
|
|
|
ab.append(terminal.MoveCursor(e.cursor.x, e.cursor.y))
|
2021-03-22 09:12:39 -04:00
|
|
|
|
2021-03-24 16:23:17 -04:00
|
|
|
ab.append(terminal.ShowCursor)
|
2021-03-24 15:09:28 -04:00
|
|
|
|
|
|
|
terminal.Write(ab.toString())
|
2021-03-19 17:39:15 -04:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
func (e *editor) drawRows(ab *buffer) {
|
2021-03-25 12:27:48 -04:00
|
|
|
for y :=0; y < e.screen.Rows; y += 1 {
|
|
|
|
if y == e.screen.Rows / 3 {
|
2021-03-24 15:52:35 -04:00
|
|
|
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
|
2021-03-25 12:27:48 -04:00
|
|
|
if len(welcome) > e.screen.Cols {
|
|
|
|
welcome = fn.TruncateString(welcome, e.screen.Cols)
|
2021-03-24 15:52:35 -04:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:27:48 -04:00
|
|
|
padding := (e.screen.Cols - len(welcome)) / 2
|
2021-03-24 15:52:35 -04:00
|
|
|
if padding > 0 {
|
|
|
|
ab.appendRune('~')
|
|
|
|
padding--
|
|
|
|
}
|
|
|
|
|
|
|
|
for padding > 0 {
|
|
|
|
padding--
|
|
|
|
ab.appendRune(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
ab.append(welcome)
|
|
|
|
} else {
|
|
|
|
ab.appendRune('~')
|
|
|
|
}
|
|
|
|
|
2021-03-24 16:23:17 -04:00
|
|
|
ab.append(terminal.ClearLine)
|
2021-03-24 14:17:29 -04:00
|
|
|
|
2021-03-25 12:27:48 -04:00
|
|
|
if y < (e.screen.Rows - 1) {
|
2021-03-24 15:09:28 -04:00
|
|
|
ab.append("\r\n")
|
2021-03-24 14:17:29 -04:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 12:46:53 -04:00
|
|
|
}
|
|
|
|
|
2021-03-26 12:01:17 -04:00
|
|
|
func (e *editor) ProcessKeypress() bool {
|
2021-03-26 13:35:58 -04:00
|
|
|
// Handle simplest inputs first
|
|
|
|
ch, _ := terminal.ReadKey()
|
|
|
|
if ch == fn.Ctrl('q') {
|
|
|
|
// Clean up on exit
|
|
|
|
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
|
|
|
|
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
// Back up so that the input can be read as a string
|
|
|
|
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
|
|
|
|
fmt.Printf("%v\r\n", keyMap)
|
|
|
|
return false
|
|
|
|
// terminal.UnReadKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
str := terminal.Read()
|
2021-03-26 12:01:17 -04:00
|
|
|
|
2021-03-26 13:35:58 -04:00
|
|
|
//// Escape sequences can be less fun...
|
2021-03-26 12:01:17 -04:00
|
|
|
if strings.Contains(str, terminal.EscPrefix) {
|
|
|
|
code := strings.TrimPrefix(str, terminal.EscPrefix)
|
|
|
|
|
|
|
|
switch code {
|
|
|
|
case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight:
|
|
|
|
e.moveCursor(code)
|
2021-03-26 13:35:58 -04:00
|
|
|
return true
|
2021-03-26 12:01:17 -04:00
|
|
|
default:
|
|
|
|
// Do something later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-26 13:35:58 -04:00
|
|
|
//func (e *editor) processEscapeCode() bool {
|
|
|
|
//
|
|
|
|
//}
|
|
|
|
|
2021-03-26 12:01:17 -04:00
|
|
|
func (e *editor) moveCursor (rawKey string) {
|
|
|
|
key := keyMap[rawKey]
|
|
|
|
|
2021-03-25 12:46:53 -04:00
|
|
|
switch key {
|
2021-03-26 12:01:17 -04:00
|
|
|
case keyLeft:
|
2021-03-25 12:46:53 -04:00
|
|
|
e.cursor.x -= 1
|
2021-03-26 12:01:17 -04:00
|
|
|
case keyRight:
|
2021-03-25 12:46:53 -04:00
|
|
|
e.cursor.x += 1
|
2021-03-26 12:01:17 -04:00
|
|
|
case keyUp:
|
2021-03-25 12:46:53 -04:00
|
|
|
e.cursor.y -= 1
|
2021-03-26 12:01:17 -04:00
|
|
|
case keyDown:
|
2021-03-25 12:46:53 -04:00
|
|
|
e.cursor.y += 1
|
|
|
|
}
|
2021-03-24 15:52:35 -04:00
|
|
|
}
|