104 lines
1.8 KiB
Go
104 lines
1.8 KiB
Go
// The main interface/implementation of the editor object
|
|
package editor
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"timshome.page/gilo/internal/gilo"
|
|
"timshome.page/gilo/internal/terminal"
|
|
)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// !Editor
|
|
// ----------------------------------------------------------------------------
|
|
|
|
type statusMsg struct {
|
|
message string
|
|
created time.Time
|
|
}
|
|
|
|
type editor struct {
|
|
screen *terminal.Screen
|
|
cursor *gilo.Point
|
|
offset *gilo.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 := gilo.DefaultPoint()
|
|
offset := gilo.DefaultPoint()
|
|
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.Row(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.Row(e.cursor.Y).deleteRune(e.cursor.X - 1)
|
|
e.cursor.X -= 1
|
|
}
|
|
|
|
e.document.dirty = true
|
|
}
|