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-03-26 16:18:03 -04:00
|
|
|
"timshome.page/gilo/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Editor Methods
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (e *editor) RefreshScreen() {
|
2021-03-31 09:43:47 -04:00
|
|
|
e.scroll()
|
|
|
|
|
2021-03-26 16:18:03 -04:00
|
|
|
ab := newBuffer()
|
|
|
|
|
|
|
|
ab.append(terminal.HideCursor)
|
|
|
|
ab.append(terminal.ResetCursor)
|
|
|
|
|
|
|
|
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-01 16:17:13 -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
|
|
|
|
|
|
|
ab.append(terminal.ShowCursor)
|
|
|
|
|
|
|
|
terminal.Write(ab.toString())
|
|
|
|
}
|
|
|
|
|
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-01 12:02:17 -04:00
|
|
|
if e.cursor.y < e.document.rowCount() {
|
|
|
|
e.renderX = e.document.rows[e.cursor.y].cursorXToRenderX(e.cursor.x)
|
2021-03-31 14:56:46 -04:00
|
|
|
}
|
|
|
|
|
2021-03-31 09:43:47 -04:00
|
|
|
if e.cursor.y < e.offset.y {
|
|
|
|
e.offset.y = e.cursor.y
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:17:13 -04:00
|
|
|
if e.cursor.y >= e.offset.y+e.screen.Rows {
|
2021-03-31 09:43:47 -04:00
|
|
|
e.offset.y = e.cursor.y - e.screen.Rows + 1
|
|
|
|
}
|
2021-03-31 11:12:09 -04:00
|
|
|
|
2021-03-31 14:56:46 -04:00
|
|
|
if e.renderX < e.offset.x {
|
|
|
|
e.offset.x = e.renderX
|
2021-03-31 11:12:09 -04:00
|
|
|
}
|
|
|
|
|
2021-04-01 16:17:13 -04:00
|
|
|
if e.renderX >= e.offset.x+e.screen.Cols {
|
2021-03-31 14:56:46 -04:00
|
|
|
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-03-26 16:18:03 -04:00
|
|
|
func (e *editor) drawRows(ab *buffer) {
|
2021-04-01 16:17:13 -04:00
|
|
|
for y := 0; y < e.screen.Rows; y++ {
|
2021-03-31 09:43:47 -04:00
|
|
|
fileRow := y + e.offset.y
|
|
|
|
|
2021-04-01 12:02:17 -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-01 12:02:17 -04:00
|
|
|
rawRow := e.document.rows[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-03-31 14:32:43 -04:00
|
|
|
if e.offset.x > rawRow.size() {
|
2021-03-31 11:12:09 -04:00
|
|
|
ab.append("")
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-01 12:02:17 -04:00
|
|
|
rowLen := e.document.rows[fileRow].rSize() - e.offset.x
|
2021-04-01 16:17:13 -04:00
|
|
|
outputRow := truncate(string(e.document.rows[fileRow].render[e.offset.x:]), rowLen)
|
2021-03-31 14:32:43 -04:00
|
|
|
ab.append(outputRow)
|
2021-03-26 16:18:03 -04:00
|
|
|
}
|
|
|
|
|
2021-04-01 16:17:13 -04:00
|
|
|
ab.appendLn(terminal.ClearLine)
|
2021-03-26 16:18:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 09:28:39 -04:00
|
|
|
func (e *editor) drawPlaceholderRow(y int, ab *buffer) {
|
2021-04-01 16:17:13 -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 {
|
|
|
|
ab.appendRune('~')
|
|
|
|
padding--
|
|
|
|
}
|
|
|
|
|
|
|
|
for padding > 0 {
|
|
|
|
padding--
|
|
|
|
ab.appendRune(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
ab.append(welcome)
|
|
|
|
} else {
|
|
|
|
ab.appendRune('~')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 09:41:16 -04:00
|
|
|
func (e *editor) drawStatusBar(ab *buffer) {
|
2021-04-01 12:37:28 -04:00
|
|
|
cols := e.screen.Cols
|
2021-04-01 16:17:13 -04:00
|
|
|
|
2021-04-01 09:41:16 -04:00
|
|
|
ab.append(terminal.InvertColor)
|
|
|
|
|
2021-04-01 12:37:28 -04:00
|
|
|
fileName := "[No Name]"
|
|
|
|
if e.document.filename != "" {
|
|
|
|
fileName = e.document.filename
|
|
|
|
}
|
|
|
|
|
2021-04-01 12:49:55 -04:00
|
|
|
leftStatus := fmt.Sprintf("%.20s - %d lines", fileName, e.document.rowCount())
|
|
|
|
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
|
|
|
|
|
|
|
ab.append(leftStatus)
|
|
|
|
ab.append(terminal.ResetColor)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-04-01 16:17:13 -04:00
|
|
|
|
2021-04-01 12:49:55 -04:00
|
|
|
rightStatus := fmt.Sprintf("%d/%d", e.cursor.y, e.document.rowCount())
|
|
|
|
rlength := len(rightStatus)
|
|
|
|
statusLength := length + rlength
|
|
|
|
|
|
|
|
if statusLength <= cols {
|
|
|
|
paddingLength := cols - statusLength
|
|
|
|
padding := strings.Repeat(" ", paddingLength)
|
|
|
|
|
|
|
|
ab.append(leftStatus)
|
|
|
|
ab.append(padding)
|
|
|
|
ab.append(rightStatus)
|
2021-04-01 12:37:28 -04:00
|
|
|
|
|
|
|
ab.append(terminal.ResetColor)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-01 12:49:55 -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-01 12:37:28 -04:00
|
|
|
ab.append(padding)
|
|
|
|
|
2021-04-01 09:41:16 -04:00
|
|
|
ab.append(terminal.ResetColor)
|
|
|
|
}
|
2021-04-01 13:25:59 -04:00
|
|
|
|
|
|
|
func (e *editor) drawMessageBar(ab *buffer) {
|
|
|
|
ab.append("\r\n")
|
|
|
|
ab.append(terminal.ClearLine)
|
|
|
|
|
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 {
|
|
|
|
ab.append(msg)
|
|
|
|
}
|
|
|
|
}
|