2021-03-19 16:36:02 -04:00
|
|
|
package editor
|
2021-03-19 17:03:56 -04:00
|
|
|
|
|
|
|
import (
|
2021-04-01 13:25:59 -04:00
|
|
|
"fmt"
|
|
|
|
"time"
|
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-31 09:43:47 -04:00
|
|
|
type point struct {
|
2021-03-25 12:27:48 -04:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2021-04-01 13:25:59 -04:00
|
|
|
type statusMsg struct {
|
|
|
|
message string
|
|
|
|
created time.Time
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:17:29 -04:00
|
|
|
type editor struct {
|
2021-03-25 12:27:48 -04:00
|
|
|
screen *terminal.Screen
|
2021-03-31 09:43:47 -04:00
|
|
|
cursor *point
|
|
|
|
offset *point
|
2021-04-01 12:02:17 -04:00
|
|
|
document *document
|
2021-04-01 13:25:59 -04:00
|
|
|
status *statusMsg
|
2021-03-31 14:56:46 -04:00
|
|
|
renderX int
|
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()
|
2021-04-01 09:41:16 -04:00
|
|
|
|
2021-04-01 13:25:59 -04:00
|
|
|
// Subtract rows for status bar and message bar/prompt
|
|
|
|
screen.Rows -= 2
|
2021-04-01 09:41:16 -04:00
|
|
|
|
2021-03-31 09:43:47 -04:00
|
|
|
cursor := &point { 0, 0 }
|
|
|
|
offset := &point { 0, 0 }
|
2021-04-01 12:02:17 -04:00
|
|
|
document := newDocument()
|
2021-04-01 13:25:59 -04:00
|
|
|
status := &statusMsg{
|
|
|
|
"",
|
|
|
|
time.Now(),
|
|
|
|
}
|
2021-03-24 14:17:29 -04:00
|
|
|
|
2021-03-31 09:43:47 -04:00
|
|
|
return &editor{
|
|
|
|
screen,
|
|
|
|
cursor,
|
|
|
|
offset,
|
2021-04-01 12:02:17 -04:00
|
|
|
document,
|
2021-04-01 13:25:59 -04:00
|
|
|
status,
|
2021-03-31 14:56:46 -04:00
|
|
|
0,
|
2021-03-31 09:43:47 -04:00
|
|
|
}
|
2021-03-22 09:12:39 -04:00
|
|
|
}
|
|
|
|
|
2021-03-30 18:29:23 -04:00
|
|
|
func (e *editor) Open(filename string) {
|
2021-04-01 12:02:17 -04:00
|
|
|
e.document.open(filename)
|
2021-03-30 18:00:06 -04:00
|
|
|
}
|
|
|
|
|
2021-04-01 13:25:59 -04:00
|
|
|
func (e *editor) SetStatusMessage(template string, a ...interface{}) {
|
|
|
|
e.status = &statusMsg {
|
|
|
|
fmt.Sprintf(template, a...),
|
|
|
|
time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:18:03 -04:00
|
|
|
func (e *editor) ProcessKeypress() bool {
|
2021-03-30 15:45:13 -04:00
|
|
|
var str string
|
2021-03-24 15:09:28 -04:00
|
|
|
|
2021-03-30 15:45:13 -04:00
|
|
|
ch, _ := terminal.ReadKey()
|
2021-03-30 16:35:03 -04:00
|
|
|
if isCtrl(ch) {
|
|
|
|
switch ch {
|
|
|
|
case ctrl('q'):
|
|
|
|
// Clean up on exit
|
|
|
|
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
|
2021-03-24 15:52:35 -04:00
|
|
|
|
2021-03-30 16:35:03 -04:00
|
|
|
return false
|
|
|
|
}
|
2021-03-30 15:45:13 -04:00
|
|
|
}
|
2021-03-30 14:42:59 -04:00
|
|
|
|
2021-03-30 15:45:13 -04:00
|
|
|
if ch == '\x1b' {
|
2021-03-30 15:47:42 -04:00
|
|
|
str = parseEscapeSequence()
|
2021-03-30 15:45:13 -04:00
|
|
|
} else {
|
|
|
|
str = string(ch)
|
2021-03-26 12:01:17 -04:00
|
|
|
}
|
|
|
|
|
2021-03-30 15:45:13 -04:00
|
|
|
switch str {
|
|
|
|
case keyUp, keyDown, keyLeft, keyRight, keyPageUp, keyPageDown, keyHome, keyEnd:
|
|
|
|
e.moveCursor(str)
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
// Do something later
|
|
|
|
terminal.Write("Code: %v", str)
|
2021-03-30 11:52:44 -04:00
|
|
|
}
|
2021-03-26 16:18:03 -04:00
|
|
|
|
2021-03-30 11:52:44 -04:00
|
|
|
return true
|
2021-03-26 12:01:17 -04:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:42:59 -04:00
|
|
|
func (e *editor) moveCursor (key string) {
|
2021-03-31 11:12:09 -04:00
|
|
|
var row *row
|
2021-04-01 12:02:17 -04:00
|
|
|
if e.cursor.y >= e.document.rowCount() {
|
2021-03-31 11:12:09 -04:00
|
|
|
row = nil
|
|
|
|
} else {
|
2021-04-01 12:02:17 -04:00
|
|
|
row = e.document.rows[e.cursor.y]
|
2021-03-31 11:12:09 -04:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:46:53 -04:00
|
|
|
switch key {
|
2021-03-30 15:45:13 -04:00
|
|
|
case keyLeft:
|
2021-03-30 14:42:59 -04:00
|
|
|
if e.cursor.x != 0 {
|
|
|
|
e.cursor.x -= 1
|
|
|
|
}
|
2021-03-31 11:12:09 -04:00
|
|
|
|
|
|
|
// Move from beginning of current row to end of previous row
|
|
|
|
if e.cursor.y > 0 {
|
|
|
|
e.cursor.y -= 1
|
2021-04-01 12:02:17 -04:00
|
|
|
e.cursor.x = e.document.rows[e.cursor.y].size()
|
2021-03-31 11:12:09 -04:00
|
|
|
}
|
2021-03-30 15:45:13 -04:00
|
|
|
case keyRight:
|
2021-03-31 14:32:43 -04:00
|
|
|
if row != nil && e.cursor.x < row.size() {
|
2021-03-30 14:42:59 -04:00
|
|
|
e.cursor.x += 1
|
|
|
|
}
|
2021-03-31 11:12:09 -04:00
|
|
|
|
|
|
|
// Move from end of current line to beginning of next line
|
2021-04-01 12:02:17 -04:00
|
|
|
if row != nil && e.cursor.x == row.size() && e.cursor.y < e.document.rowCount() - 1 {
|
2021-03-31 11:12:09 -04:00
|
|
|
e.cursor.y += 1
|
|
|
|
e.cursor.x = 0
|
|
|
|
}
|
2021-03-30 15:45:13 -04:00
|
|
|
case keyUp:
|
2021-03-30 14:42:59 -04:00
|
|
|
if e.cursor.y != 0 {
|
|
|
|
e.cursor.y -= 1
|
|
|
|
}
|
2021-03-30 15:45:13 -04:00
|
|
|
case keyDown:
|
2021-04-01 13:25:59 -04:00
|
|
|
if e.cursor.y < e.document.rowCount() {
|
2021-03-30 14:42:59 -04:00
|
|
|
e.cursor.y += 1
|
|
|
|
}
|
2021-03-30 15:45:13 -04:00
|
|
|
case keyPageUp:
|
2021-03-31 14:32:43 -04:00
|
|
|
if e.cursor.y > e.screen.Rows {
|
|
|
|
e.cursor.y -= e.screen.Rows
|
|
|
|
} else {
|
|
|
|
e.cursor.y = 0
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:45:13 -04:00
|
|
|
case keyPageDown:
|
2021-04-01 12:02:17 -04:00
|
|
|
if e.cursor.y + e.screen.Rows > e.document.rowCount() {
|
2021-03-31 14:32:43 -04:00
|
|
|
e.cursor.y += e.screen.Rows
|
|
|
|
} else {
|
2021-04-01 12:02:17 -04:00
|
|
|
e.cursor.y = e.document.rowCount() - 1
|
2021-03-31 14:32:43 -04:00
|
|
|
}
|
|
|
|
|
2021-03-30 15:45:13 -04:00
|
|
|
case keyHome:
|
|
|
|
e.cursor.x = 0
|
|
|
|
case keyEnd:
|
2021-03-31 11:12:09 -04:00
|
|
|
if row != nil {
|
2021-03-31 14:32:43 -04:00
|
|
|
e.cursor.x = row.size()
|
2021-03-31 11:12:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 12:02:17 -04:00
|
|
|
if e.cursor.y < e.document.rowCount() {
|
|
|
|
row = e.document.rows[e.cursor.y]
|
2021-03-31 14:32:43 -04:00
|
|
|
rowLen := row.size()
|
2021-03-31 11:12:09 -04:00
|
|
|
|
|
|
|
// Snap to the end of a shorter line from a longer one
|
|
|
|
if e.cursor.x > rowLen {
|
|
|
|
e.cursor.x = rowLen
|
|
|
|
}
|
2021-03-25 12:46:53 -04:00
|
|
|
}
|
2021-03-30 15:45:13 -04:00
|
|
|
}
|
|
|
|
|
2021-03-30 16:05:33 -04:00
|
|
|
// Convert the raw ANSI escape sequences to the type of key input
|
2021-03-30 15:45:13 -04:00
|
|
|
func parseEscapeSequence () string {
|
|
|
|
var runes []rune
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
ch, size := terminal.ReadKey()
|
|
|
|
if size == 0 {
|
|
|
|
return "\x1b"
|
|
|
|
}
|
|
|
|
runes = append(runes, ch)
|
|
|
|
|
|
|
|
if i == 1 && runes[1] >= 'A' {
|
2021-03-30 16:22:35 -04:00
|
|
|
// \eOH \eOF
|
2021-03-30 15:45:13 -04:00
|
|
|
if runes[0] == 'O' {
|
|
|
|
switch runes[1] {
|
|
|
|
case 'H':
|
|
|
|
return keyHome
|
|
|
|
case 'F':
|
|
|
|
return keyEnd
|
|
|
|
}
|
|
|
|
}
|
2021-03-30 16:22:35 -04:00
|
|
|
// \e[A
|
2021-03-30 15:45:13 -04:00
|
|
|
if runes[0] == '[' {
|
|
|
|
switch runes[1] {
|
|
|
|
case 'A':
|
|
|
|
return keyUp
|
|
|
|
case 'B':
|
|
|
|
return keyDown
|
|
|
|
case 'C':
|
|
|
|
return keyRight
|
|
|
|
case 'D':
|
|
|
|
return keyLeft
|
|
|
|
case 'H':
|
|
|
|
return keyHome
|
|
|
|
case 'F':
|
|
|
|
return keyEnd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 16:22:35 -04:00
|
|
|
// \e[1~
|
2021-03-30 15:45:13 -04:00
|
|
|
if i == 2 && runes[0] == '[' && runes[2] == '~' {
|
|
|
|
switch runes[1] {
|
|
|
|
case '1':
|
|
|
|
return keyHome
|
|
|
|
case '3':
|
|
|
|
return keyDelete
|
|
|
|
case '4':
|
|
|
|
return keyEnd
|
|
|
|
case '5':
|
|
|
|
return keyPageUp
|
|
|
|
case '6':
|
|
|
|
return keyPageDown
|
|
|
|
case '7':
|
|
|
|
return keyHome
|
|
|
|
case '8':
|
|
|
|
return keyEnd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string('\x1b')
|
2021-03-30 18:43:46 -04:00
|
|
|
}
|