2021-03-26 16:18:03 -04:00
|
|
|
// Editor methods involved in drawing to the console
|
|
|
|
package editor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-04-01 13:25:59 -04:00
|
|
|
"time"
|
2021-04-02 13:11:54 -04:00
|
|
|
"timshome.page/gilo/internal/buffer"
|
2021-04-02 14:52:44 -04:00
|
|
|
"timshome.page/gilo/internal/terminal"
|
2021-03-26 16:18:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Editor Methods
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (e *editor) RefreshScreen() {
|
2021-03-31 09:43:47 -04:00
|
|
|
e.scroll()
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab := buffer.NewBuffer()
|
2021-03-26 16:18:03 -04:00
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(terminal.HideCursor)
|
|
|
|
ab.Append(terminal.ResetCursor)
|
2021-03-26 16:18:03 -04:00
|
|
|
|
|
|
|
e.drawRows(ab)
|
2021-04-01 09:41:16 -04:00
|
|
|
e.drawStatusBar(ab)
|
2021-04-01 13:25:59 -04:00
|
|
|
e.drawMessageBar(ab)
|
2021-04-01 09:41:16 -04:00
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
ab.Append(terminal.MoveCursor(e.renderX-e.offset.X, e.cursor.Y-e.offset.Y))
|
2021-03-26 16:18:03 -04:00
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(terminal.ShowCursor)
|
2021-03-26 16:18:03 -04:00
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
terminal.Write(ab.ToString())
|
2021-03-26 16:18:03 -04:00
|
|
|
}
|
|
|
|
|
2021-03-31 09:43:47 -04:00
|
|
|
func (e *editor) scroll() {
|
2021-03-31 14:56:46 -04:00
|
|
|
e.renderX = 0
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.Y < e.document.RowCount() {
|
|
|
|
e.renderX = e.document.Row(e.cursor.Y).cursorXToRenderX(e.cursor.X)
|
2021-03-31 14:56:46 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.Y < e.offset.Y {
|
|
|
|
e.offset.Y = e.cursor.Y
|
2021-03-31 09:43:47 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.Y >= e.offset.Y+e.screen.Rows {
|
|
|
|
e.offset.Y = e.cursor.Y - e.screen.Rows + 1
|
2021-03-31 09:43:47 -04:00
|
|
|
}
|
2021-03-31 11:12:09 -04:00
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.renderX < e.offset.X {
|
|
|
|
e.offset.X = e.renderX
|
2021-03-31 11:12:09 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.renderX >= e.offset.X+e.screen.Cols {
|
|
|
|
e.offset.X = e.renderX - e.screen.Cols
|
2021-03-31 11:12:09 -04:00
|
|
|
}
|
2021-03-31 09:43:47 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
func (e *editor) drawRows(ab *buffer.Buffer) {
|
2021-04-01 16:17:13 -04:00
|
|
|
for y := 0; y < e.screen.Rows; y++ {
|
2021-04-02 14:52:44 -04:00
|
|
|
fileRow := y + e.offset.Y
|
2021-03-31 09:43:47 -04:00
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
if fileRow >= e.document.RowCount() {
|
2021-03-31 09:28:39 -04:00
|
|
|
e.drawPlaceholderRow(y, ab)
|
2021-03-26 16:18:03 -04:00
|
|
|
} else {
|
2021-04-02 14:52:44 -04:00
|
|
|
rawRow := e.document.Row(fileRow)
|
2021-03-31 11:12:09 -04:00
|
|
|
|
|
|
|
// If the column offset is greater than the length of the row,
|
|
|
|
// just display an empty row
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.offset.X > rawRow.Size() {
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append("")
|
2021-03-31 11:12:09 -04:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
rowLen := e.document.Row(fileRow).RenderSize() - e.offset.X
|
|
|
|
outputRow := truncate(string(e.document.Row(fileRow).render[e.offset.X:]), rowLen)
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(outputRow)
|
2021-03-26 16:18:03 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.AppendLn(terminal.ClearLine)
|
2021-03-26 16:18:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
func (e *editor) drawPlaceholderRow(y int, ab *buffer.Buffer) {
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.document.RowCount() == 0 && y == e.screen.Rows/3 {
|
2021-03-31 09:28:39 -04:00
|
|
|
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
|
|
|
|
if len(welcome) > e.screen.Cols {
|
2021-04-01 16:17:13 -04:00
|
|
|
welcome = truncate(welcome, e.screen.Cols)
|
2021-03-31 09:28:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
padding := (e.screen.Cols - len(welcome)) / 2
|
|
|
|
if padding > 0 {
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.AppendRune('~')
|
2021-03-31 09:28:39 -04:00
|
|
|
padding--
|
|
|
|
}
|
|
|
|
|
|
|
|
for padding > 0 {
|
|
|
|
padding--
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.AppendRune(' ')
|
2021-03-31 09:28:39 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(welcome)
|
2021-03-31 09:28:39 -04:00
|
|
|
} else {
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.AppendRune('~')
|
2021-03-31 09:28:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
func (e *editor) drawStatusBar(ab *buffer.Buffer) {
|
2021-04-01 12:37:28 -04:00
|
|
|
cols := e.screen.Cols
|
2021-04-01 16:17:13 -04:00
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(terminal.InvertColor)
|
2021-04-01 09:41:16 -04:00
|
|
|
|
2021-04-01 12:37:28 -04:00
|
|
|
fileName := "[No Name]"
|
|
|
|
if e.document.filename != "" {
|
|
|
|
fileName = e.document.filename
|
|
|
|
}
|
2021-04-02 10:07:37 -04:00
|
|
|
modified := ""
|
|
|
|
if e.document.dirty {
|
|
|
|
modified = "(modified)"
|
|
|
|
}
|
2021-04-01 12:37:28 -04:00
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
leftStatus := fmt.Sprintf("%.20s - %d lines %s", fileName, e.document.RowCount(), modified)
|
2021-04-01 12:49:55 -04:00
|
|
|
length := len(leftStatus)
|
|
|
|
if length > cols {
|
2021-04-01 16:17:13 -04:00
|
|
|
leftStatus = truncate(leftStatus, cols)
|
2021-04-01 12:49:55 -04:00
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(leftStatus)
|
|
|
|
ab.Append(terminal.ResetColor)
|
2021-04-01 12:49:55 -04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-04-01 16:17:13 -04:00
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
rightStatus := fmt.Sprintf("%d/%d", e.cursor.Y+1, e.document.RowCount())
|
2021-04-01 12:49:55 -04:00
|
|
|
rlength := len(rightStatus)
|
|
|
|
statusLength := length + rlength
|
|
|
|
|
|
|
|
if statusLength <= cols {
|
|
|
|
paddingLength := cols - statusLength
|
|
|
|
padding := strings.Repeat(" ", paddingLength)
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(leftStatus)
|
|
|
|
ab.Append(padding)
|
|
|
|
ab.Append(rightStatus)
|
2021-04-01 12:37:28 -04:00
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(terminal.ResetColor)
|
2021-04-01 12:37:28 -04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(leftStatus)
|
2021-04-01 12:37:28 -04:00
|
|
|
|
|
|
|
// Pad the rest of the status line
|
2021-04-01 16:17:13 -04:00
|
|
|
padding := strings.Repeat(" ", cols-length)
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(padding)
|
2021-04-01 12:37:28 -04:00
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(terminal.ResetColor)
|
2021-04-01 09:41:16 -04:00
|
|
|
}
|
2021-04-01 13:25:59 -04:00
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
func (e *editor) drawMessageBar(ab *buffer.Buffer) {
|
|
|
|
ab.Append("\r\n")
|
|
|
|
ab.Append(terminal.ClearLine)
|
2021-04-01 13:25:59 -04:00
|
|
|
|
2021-04-01 16:17:13 -04:00
|
|
|
msg := truncate(e.status.message, e.screen.Cols)
|
2021-04-01 13:25:59 -04:00
|
|
|
if len(msg) > 0 && time.Since(e.status.created).Seconds() < 5.0 {
|
2021-04-02 13:11:54 -04:00
|
|
|
ab.Append(msg)
|
2021-04-01 13:25:59 -04:00
|
|
|
}
|
|
|
|
}
|