175 lines
3.4 KiB
Go
175 lines
3.4 KiB
Go
// Package editor The main interface/implementation of the editor object
|
|
package editor
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"timshome.page/gilo/char"
|
|
"timshome.page/gilo/editor/document"
|
|
"timshome.page/gilo/internal/gilo"
|
|
"timshome.page/gilo/terminal"
|
|
)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// !Editor
|
|
// ----------------------------------------------------------------------------
|
|
|
|
type statusMsg struct {
|
|
message string
|
|
created time.Time
|
|
}
|
|
|
|
type Editor struct {
|
|
screen *terminal.Screen
|
|
cursor *gilo.Point
|
|
offset *gilo.Point
|
|
doc *document.Document
|
|
status *statusMsg
|
|
search *search
|
|
quitTimes uint8
|
|
renderX int
|
|
}
|
|
|
|
func NewEditor() *Editor {
|
|
// Subtract rows for status bar and message bar/prompt
|
|
screen := terminal.Size()
|
|
screen.Rows -= 2
|
|
|
|
status := &statusMsg{
|
|
"",
|
|
time.Now(),
|
|
}
|
|
|
|
return &Editor{
|
|
screen,
|
|
gilo.DefaultPoint(),
|
|
gilo.DefaultPoint(),
|
|
document.NewDocument(),
|
|
status,
|
|
newSearch(),
|
|
gilo.QuitTimes,
|
|
0,
|
|
}
|
|
}
|
|
|
|
func (e *Editor) Open(filename string) {
|
|
e.doc.Open(filename)
|
|
}
|
|
|
|
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) save() {
|
|
if e.doc.Filename == "" {
|
|
e.doc.Filename = e.prompt("Save as: %s (ESC to cancel)", nil)
|
|
if e.doc.Filename == "" {
|
|
e.SetStatusMessage("Save aborted")
|
|
return
|
|
}
|
|
}
|
|
|
|
// Set the syntax highlighting type, now that we now the file extension
|
|
e.doc.SelectSyntaxHighlight()
|
|
|
|
size := e.doc.Save()
|
|
|
|
if size > 0 {
|
|
e.SetStatusMessage("%d bytes written to disk", size)
|
|
} else {
|
|
e.SetStatusMessage("Failed to save file")
|
|
}
|
|
}
|
|
|
|
func (e *Editor) prompt(prompt string, callback func(string, string)) string {
|
|
buf := gilo.NewBuffer()
|
|
|
|
// Show the prompt message
|
|
e.SetStatusMessage(prompt, "")
|
|
e.RefreshScreen()
|
|
|
|
for {
|
|
if buf.Len() > 0 {
|
|
e.SetStatusMessage(prompt, buf.ToString())
|
|
e.RefreshScreen()
|
|
}
|
|
|
|
ch, _ := terminal.ReadKey()
|
|
if ch == char.Enter {
|
|
if buf.Len() != 0 {
|
|
e.SetStatusMessage("")
|
|
if callback != nil {
|
|
callback(buf.ToString(), string(ch))
|
|
}
|
|
|
|
return buf.ToString()
|
|
}
|
|
} else if char.IsAscii(ch) && !char.IsCtrl(ch) {
|
|
buf.AppendRune(ch)
|
|
} else if ch == char.Backspace || ch == char.Ctrl('h') {
|
|
buf.Truncate(buf.Len() - 1)
|
|
} else if ch == char.Esc {
|
|
k := parseEscapeSequence()
|
|
if k == keyDelete {
|
|
buf.Truncate(buf.Len() - 1)
|
|
} else if k == string(char.Esc) {
|
|
e.SetStatusMessage("")
|
|
if callback != nil {
|
|
callback(buf.ToString(), k)
|
|
}
|
|
return ""
|
|
} else {
|
|
if callback != nil {
|
|
callback(buf.ToString(), k)
|
|
}
|
|
}
|
|
}
|
|
|
|
if callback != nil {
|
|
callback(buf.ToString(), string(ch))
|
|
}
|
|
}
|
|
}
|
|
|
|
func (e *Editor) insertChar(ch rune) {
|
|
if e.cursor.Y == e.doc.RowCount() {
|
|
e.doc.AppendRow("")
|
|
}
|
|
|
|
e.doc.InsertChar(e.cursor, ch)
|
|
e.cursor.X += 1
|
|
}
|
|
|
|
func (e *Editor) delChar() {
|
|
if e.cursor.Y == e.doc.RowCount() {
|
|
return
|
|
}
|
|
|
|
if e.cursor.X == 0 && e.cursor.Y == 0 {
|
|
return
|
|
}
|
|
|
|
if e.cursor.X > 0 {
|
|
at := e.cursor
|
|
at.X -= 1
|
|
e.doc.DelChar(at)
|
|
} else {
|
|
// Move cursor to the current end of the previous line
|
|
e.cursor.X = e.doc.GetRow(e.cursor.Y - 1).Size()
|
|
|
|
// Move the contents of the current row to the previous
|
|
e.doc.MergeRows(e.cursor.Y-1, e.cursor.Y)
|
|
|
|
e.cursor.Y -= 1
|
|
}
|
|
}
|