gilo/editor/editor.go

337 lines
6.1 KiB
Go
Raw Normal View History

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-04-01 16:17:13 -04:00
"timshome.page/gilo/key"
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 {
x int
y int
}
2021-04-01 13:25:59 -04:00
type statusMsg struct {
message string
created time.Time
}
type editor struct {
2021-04-02 10:48:51 -04:00
screen *terminal.Screen
cursor *point
offset *point
document *document
status *statusMsg
quitTimes uint8
2021-04-02 10:48:51 -04:00
renderX int
2021-03-23 15:51:59 -04:00
}
func New() *editor {
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-04-01 16:17:13 -04:00
cursor := &point{0, 0}
offset := &point{0, 0}
document := newDocument()
2021-04-01 13:25:59 -04:00
status := &statusMsg{
"",
time.Now(),
}
2021-03-31 09:43:47 -04:00
return &editor{
screen,
cursor,
offset,
document,
2021-04-01 13:25:59 -04:00
status,
KiloQuitTimes,
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) {
e.document.open(filename)
2021-03-30 18:00:06 -04:00
}
2021-04-01 20:23:51 -04:00
func (e *editor) save() {
size := e.document.save()
if size > 0 {
e.SetStatusMessage("%d bytes written to disk", size)
} else {
e.SetStatusMessage("Failed to save file")
}
}
2021-04-01 13:25:59 -04:00
func (e *editor) SetStatusMessage(template string, a ...interface{}) {
2021-04-01 16:17:13 -04:00
e.status = &statusMsg{
2021-04-01 13:25:59 -04:00
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
ch, _ := terminal.ReadKey()
2021-03-24 15:52:35 -04:00
2021-04-02 11:57:24 -04:00
return e.processKeypressChar(ch)
}
/**
* Determine what to do with an individual character of input
*/
func (e *editor) processKeypressChar(ch rune) bool {
2021-04-01 16:17:13 -04:00
switch ch {
case key.Ctrl('q'):
if e.document.dirty && e.quitTimes > 0 {
e.SetStatusMessage("WARNING!!! File has unsaved changes. Press Ctrl-Q %d more tiems to quite.", e.quitTimes)
e.quitTimes -= 1
return true
}
2021-04-01 16:17:13 -04:00
// Clean up on exit
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
2021-03-30 14:42:59 -04:00
2021-04-01 16:17:13 -04:00
return false
2021-04-01 20:23:51 -04:00
case key.Ctrl('s'):
e.save()
2021-04-01 18:51:52 -04:00
case key.Enter:
case key.Backspace, key.Ctrl('h'):
2021-04-02 10:48:51 -04:00
e.delChar()
2021-04-01 18:51:52 -04:00
2021-04-01 20:23:51 -04:00
case key.Esc, key.Ctrl('l'):
2021-04-01 16:17:13 -04:00
// Modifier keys that return ANSI escape sequences
str := parseEscapeSequence()
2021-04-02 11:57:24 -04:00
// Don't swallow a character after ESC if it doesn't
// start an ANSI escape sequence
if len(str) == 1 {
return e.processKeypressChar(rune(str[0]))
2021-04-01 16:17:13 -04:00
}
2021-03-26 12:01:17 -04:00
2021-04-02 11:57:24 -04:00
e.processKeypressStr(str)
2021-03-30 15:45:13 -04:00
default:
2021-04-01 16:17:13 -04:00
e.insertChar(ch)
2021-03-30 11:52:44 -04:00
}
2021-03-26 16:18:03 -04:00
2021-04-02 11:57:24 -04:00
// Clear the quit message and restart the
// confirmation count if confirmation is not
// completed
if e.quitTimes != KiloQuitTimes {
e.quitTimes = KiloQuitTimes
2021-04-02 11:57:24 -04:00
e.SetStatusMessage("")
}
2021-03-30 11:52:44 -04:00
return true
2021-03-26 12:01:17 -04:00
}
2021-04-02 11:57:24 -04:00
/**
* Determine what do do with a parsed ANSI escape sequence
*/
func (e *editor) processKeypressStr(key string) {
switch key {
case keyUp,
keyDown,
keyLeft,
keyRight,
keyPageUp,
keyPageDown,
keyHome,
keyEnd:
e.moveCursor(key)
case keyDelete:
e.moveCursor(keyRight)
e.delChar()
}
}
2021-04-01 16:17:13 -04:00
func (e *editor) moveCursor(key string) {
var row *row
if e.cursor.y >= e.document.rowCount() {
row = nil
} else {
row = e.document.rows[e.cursor.y]
}
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
}
// Move from beginning of current row to end of previous row
if e.cursor.y > 0 {
e.cursor.y -= 1
e.cursor.x = e.document.rows[e.cursor.y].size()
}
2021-03-30 15:45:13 -04:00
case keyRight:
if row != nil && e.cursor.x < row.size() {
2021-03-30 14:42:59 -04:00
e.cursor.x += 1
}
// Move from end of current line to beginning of next line
2021-04-01 16:17:13 -04:00
if row != nil && e.cursor.x == row.size() && e.cursor.y < e.document.rowCount()-1 {
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:
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 16:17:13 -04:00
if e.cursor.y+e.screen.Rows > e.document.rowCount() {
e.cursor.y += e.screen.Rows
} else {
e.cursor.y = e.document.rowCount() - 1
}
2021-03-30 15:45:13 -04:00
case keyHome:
e.cursor.x = 0
case keyEnd:
if row != nil {
e.cursor.x = row.size()
}
}
if e.cursor.y < e.document.rowCount() {
row = e.document.rows[e.cursor.y]
rowLen := row.size()
// 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-04-01 16:17:13 -04:00
func (e *editor) insertChar(ch rune) {
if e.cursor.y == e.document.rowCount() {
e.document.appendRow("")
}
e.document.rows[e.cursor.y].insertRune(ch, e.cursor.x)
e.cursor.x += 1
2021-04-02 10:07:37 -04:00
e.document.dirty = true
2021-04-01 16:17:13 -04:00
}
2021-04-02 10:48:51 -04:00
func (e *editor) delChar() {
if e.cursor.y == e.document.rowCount() {
return
}
if e.cursor.x > 0 {
e.document.rows[e.cursor.y].deleteRune(e.cursor.x - 1)
e.cursor.x -= 1
}
e.document.dirty = true
}
2021-03-30 16:05:33 -04:00
// Convert the raw ANSI escape sequences to the type of key input
2021-04-01 16:17:13 -04:00
func parseEscapeSequence() string {
2021-04-02 11:57:24 -04:00
// If we aren't starting an escape sequence,
// return the character
startChar, _ := terminal.ReadKey()
if startChar != '[' && startChar != 'O' {
return string(startChar)
}
2021-03-30 15:45:13 -04:00
2021-04-02 11:57:24 -04:00
// Read one or two characters after
// \e[ or \eO, which is the end of the
// handled escape sequences
runes := [2]rune{'\000', '\000'}
for i := 0; i < 2; i++ {
2021-03-30 15:45:13 -04:00
ch, size := terminal.ReadKey()
if size == 0 {
2021-04-02 11:57:24 -04:00
return string(rune(key.Esc))
2021-03-30 15:45:13 -04:00
}
2021-04-02 11:57:24 -04:00
runes[i] = ch
if i == 0 && runes[0] >= 'A' && runes[0] <= 'Z' {
return escSeqToKey([]rune{startChar, runes[0]})
2021-03-30 15:45:13 -04:00
}
2021-04-02 11:57:24 -04:00
// \e[*~
if i == 1 && startChar == '[' && runes[1] == '~' {
return escSeqToKey([]rune{startChar, runes[0], runes[1]})
}
}
return string(rune(key.Esc))
}
func escSeqToKey (seq []rune) string {
// \eO*
// \e[*
if len(seq) == 2 {
startChar, cmd := seq[0], seq[1]
if startChar == 'O' {
switch cmd {
case 'H':
2021-03-30 15:45:13 -04:00
return keyHome
2021-04-02 11:57:24 -04:00
case 'F':
2021-03-30 15:45:13 -04:00
return keyEnd
2021-04-02 11:57:24 -04:00
}
} else if startChar == '[' {
switch cmd {
case 'A':
return keyUp
case 'B':
return keyDown
case 'C':
return keyRight
case 'D':
return keyLeft
case 'H':
2021-03-30 15:45:13 -04:00
return keyHome
2021-04-02 11:57:24 -04:00
case 'F':
2021-03-30 15:45:13 -04:00
return keyEnd
}
}
2021-04-02 11:57:24 -04:00
} else if len(seq) == 3 { // \e[*~
cmd := seq[1]
switch cmd {
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
}
2021-03-30 15:45:13 -04:00
}
2021-04-02 11:57:24 -04:00
return string(rune(key.Esc))
2021-03-30 18:43:46 -04:00
}