gilo/internal/editor/editor.go

178 lines
3.4 KiB
Go
Raw Normal View History

2021-04-02 13:11:54 -04:00
// The main interface/implementation of the editor object
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-02 15:36:43 -04:00
doc "timshome.page/gilo/internal/editor/document"
2021-04-02 14:52:44 -04:00
"timshome.page/gilo/internal/gilo"
2021-04-02 19:24:20 -04:00
"timshome.page/gilo/internal/key"
2021-04-02 14:52:44 -04:00
"timshome.page/gilo/internal/terminal"
2021-03-19 17:03:56 -04:00
)
2021-03-24 15:09:28 -04:00
// ----------------------------------------------------------------------------
// !Editor
// ----------------------------------------------------------------------------
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
2021-04-02 14:52:44 -04:00
cursor *gilo.Point
offset *gilo.Point
2021-04-02 15:36:43 -04:00
document *doc.Document
2021-04-02 10:48:51 -04:00
status *statusMsg
quitTimes uint8
2021-04-02 10:48:51 -04:00
renderX int
2021-03-23 15:51:59 -04:00
}
2021-04-02 15:36:43 -04:00
func NewEditor() *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-02 14:52:44 -04:00
cursor := gilo.DefaultPoint()
offset := gilo.DefaultPoint()
2021-04-02 15:36:43 -04:00
document := doc.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,
2021-04-02 15:36:43 -04:00
gilo.QuitTimes,
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) {
2021-04-02 14:52:44 -04:00
e.document.Open(filename)
2021-03-30 18:00:06 -04:00
}
2021-04-06 10:59:24 -04:00
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() {
2021-04-06 09:58:45 -04:00
if e.document.Filename == "" {
2021-04-06 10:59:24 -04:00
e.document.Filename = e.prompt("Save as: %s (ESC to cancel)")
if e.document.Filename == "" {
e.SetStatusMessage("Save aborted")
}
2021-04-06 09:58:45 -04:00
}
2021-04-02 19:24:20 -04:00
2021-04-02 14:52:44 -04:00
size := e.document.Save()
2021-04-01 20:23:51 -04:00
if size > 0 {
e.SetStatusMessage("%d bytes written to disk", size)
} else {
e.SetStatusMessage("Failed to save file")
}
}
2021-04-06 10:59:24 -04:00
func (e *editor) prompt(prompt string) string {
2021-04-02 19:24:20 -04:00
buf := gilo.NewBuffer()
2021-04-06 09:58:45 -04:00
// Show the prompt message
e.SetStatusMessage(prompt, "")
e.RefreshScreen()
2021-04-02 19:24:20 -04:00
for {
if buf.Len() > 0 {
e.SetStatusMessage(prompt, buf.ToString())
2021-04-06 09:58:45 -04:00
e.RefreshScreen()
2021-04-02 19:24:20 -04:00
}
ch, _ := terminal.ReadKey()
if ch == key.Enter {
if buf.Len() != 0 {
e.SetStatusMessage("")
return buf.ToString()
}
2021-04-06 09:58:45 -04:00
} else if key.IsAscii(ch) && !key.IsCtrl(ch) {
buf.AppendRune(ch)
} else if ch == key.Backspace || ch == key.Ctrl('h') {
buf.Truncate(buf.Len() - 1)
} else if ch == key.Esc {
k := parseEscapeSequence()
if k == keyDelete {
buf.Truncate(buf.Len() - 1)
} else if k == string(rune(key.Esc)) {
e.SetStatusMessage("")
return ""
}
2021-04-02 19:24:20 -04:00
}
}
}
2021-04-06 10:59:24 -04:00
func (e *editor) find() {
query := e.prompt("Search: %s (ESC to cancel)")
if query == "" {
return
}
for i := 0; i < e.document.RowCount(); i++ {
row := e.document.GetRow(i)
matchIndex := row.Search(query)
if matchIndex != -1 {
e.cursor.Y = i
e.cursor.X = row.RenderXtoCursorX(matchIndex)
e.offset.Y = e.document.RowCount()
break
}
}
}
2021-04-01 16:17:13 -04:00
func (e *editor) insertChar(ch rune) {
2021-04-02 14:52:44 -04:00
if e.cursor.Y == e.document.RowCount() {
e.document.AppendRow("")
2021-04-01 16:17:13 -04:00
}
2021-04-02 15:36:43 -04:00
e.document.InsertChar(e.cursor, ch)
2021-04-02 14:52:44 -04:00
e.cursor.X += 1
2021-04-01 16:17:13 -04:00
}
2021-04-02 10:48:51 -04:00
func (e *editor) delChar() {
2021-04-02 14:52:44 -04:00
if e.cursor.Y == e.document.RowCount() {
2021-04-02 10:48:51 -04:00
return
}
if e.cursor.X == 0 && e.cursor.Y == 0 {
return
}
2021-04-02 14:52:44 -04:00
if e.cursor.X > 0 {
2021-04-02 15:36:43 -04:00
at := e.cursor
at.X -= 1
e.document.DelChar(at)
} else {
// Move cursor to the current end of the previous line
2021-04-02 19:24:20 -04:00
e.cursor.X = e.document.GetRow(e.cursor.Y - 1).Size()
// Move the contents of the current row to the previous
2021-04-02 19:24:58 -04:00
e.document.MergeRows(e.cursor.Y-1, e.cursor.Y)
e.cursor.Y -= 1
2021-04-02 10:48:51 -04:00
}
}