107 lines
1.7 KiB
Go
107 lines
1.7 KiB
Go
package editor
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"timshome.page/gilo/terminal"
|
|
)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// !Editor
|
|
// ----------------------------------------------------------------------------
|
|
|
|
type point struct {
|
|
x int
|
|
y int
|
|
}
|
|
|
|
type statusMsg struct {
|
|
message string
|
|
created time.Time
|
|
}
|
|
|
|
type editor struct {
|
|
screen *terminal.Screen
|
|
cursor *point
|
|
offset *point
|
|
document *document
|
|
status *statusMsg
|
|
quitTimes uint8
|
|
renderX int
|
|
}
|
|
|
|
func New() *editor {
|
|
screen := terminal.Size()
|
|
|
|
// Subtract rows for status bar and message bar/prompt
|
|
screen.Rows -= 2
|
|
|
|
cursor := &point{0, 0}
|
|
offset := &point{0, 0}
|
|
document := newDocument()
|
|
status := &statusMsg{
|
|
"",
|
|
time.Now(),
|
|
}
|
|
|
|
return &editor{
|
|
screen,
|
|
cursor,
|
|
offset,
|
|
document,
|
|
status,
|
|
KiloQuitTimes,
|
|
0,
|
|
}
|
|
}
|
|
|
|
func (e *editor) Open(filename string) {
|
|
e.document.open(filename)
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
func (e *editor) SetStatusMessage(template string, a ...interface{}) {
|
|
e.status = &statusMsg{
|
|
fmt.Sprintf(template, a...),
|
|
time.Now(),
|
|
}
|
|
}
|
|
|
|
func (e *editor) ProcessKeypress() bool {
|
|
ch, _ := terminal.ReadKey()
|
|
|
|
return e.processKeypressChar(ch)
|
|
}
|
|
|
|
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
|
|
e.document.dirty = true
|
|
}
|
|
|
|
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
|
|
}
|