147 lines
2.7 KiB
Go
147 lines
2.7 KiB
Go
// The main interface/implementation of the editor object
|
|
package editor
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
doc "timshome.page/gilo/internal/editor/document"
|
|
"timshome.page/gilo/internal/gilo"
|
|
"timshome.page/gilo/internal/key"
|
|
"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 *doc.Document
|
|
status *statusMsg
|
|
quitTimes uint8
|
|
renderX int
|
|
}
|
|
|
|
func NewEditor() *editor {
|
|
screen := terminal.Size()
|
|
|
|
// Subtract rows for status bar and message bar/prompt
|
|
screen.Rows -= 2
|
|
|
|
cursor := gilo.DefaultPoint()
|
|
offset := gilo.DefaultPoint()
|
|
document := doc.NewDocument()
|
|
status := &statusMsg{
|
|
"",
|
|
time.Now(),
|
|
}
|
|
|
|
return &editor{
|
|
screen,
|
|
cursor,
|
|
offset,
|
|
document,
|
|
status,
|
|
gilo.QuitTimes,
|
|
0,
|
|
}
|
|
}
|
|
|
|
func (e *editor) Open(filename string) {
|
|
e.document.Open(filename)
|
|
}
|
|
|
|
func (e *editor) Save() {
|
|
//if e.document.Filename == "" {
|
|
// e.document.Filename = e.Prompt("Save as: %s")
|
|
//}
|
|
|
|
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) Prompt(prompt string) string {
|
|
buf := gilo.NewBuffer()
|
|
|
|
for {
|
|
if buf.Len() > 0 {
|
|
e.SetStatusMessage(prompt, buf.ToString())
|
|
} else {
|
|
e.SetStatusMessage(prompt)
|
|
}
|
|
|
|
e.RefreshScreen()
|
|
|
|
ch, _ := terminal.ReadKey()
|
|
if ch == key.Enter {
|
|
if buf.Len() != 0 {
|
|
e.SetStatusMessage("")
|
|
|
|
return buf.ToString()
|
|
} else if ! key.IsCtrl(ch) && key.IsAscii(ch) {
|
|
buf.AppendRune(ch)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (e *editor) insertChar(ch rune) {
|
|
if e.cursor.Y == e.document.RowCount() {
|
|
e.document.AppendRow("")
|
|
}
|
|
|
|
e.document.InsertChar(e.cursor, ch)
|
|
e.cursor.X += 1
|
|
}
|
|
|
|
func (e *editor) delChar() {
|
|
if e.cursor.Y == e.document.RowCount() {
|
|
return
|
|
}
|
|
|
|
if e.cursor.X == 0 && e.cursor.Y == 0 {
|
|
return
|
|
}
|
|
|
|
if e.cursor.X > 0 {
|
|
at := e.cursor
|
|
at.X -= 1
|
|
e.document.DelChar(at)
|
|
|
|
e.cursor.X -= 1
|
|
} else {
|
|
// Move cursor to the current end of the previous line
|
|
e.cursor.X = e.document.GetRow(e.cursor.Y - 1).Size()
|
|
|
|
// Move the contents of the current row to the previous
|
|
e.document.MergeRows(e.cursor.Y - 1, e.cursor.Y)
|
|
|
|
e.cursor.Y -= 1
|
|
}
|
|
}
|