Timothy J. Warren
1cc35c95a7
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good
169 lines
3.5 KiB
Go
169 lines
3.5 KiB
Go
// Editor methods involved in drawing to the console
|
|
package editor
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
"timshome.page/gilo/internal/buffer"
|
|
"timshome.page/gilo/terminal"
|
|
)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// !Editor Methods
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func (e *editor) RefreshScreen() {
|
|
e.scroll()
|
|
|
|
ab := buffer.NewBuffer()
|
|
|
|
ab.Append(terminal.HideCursor)
|
|
ab.Append(terminal.ResetCursor)
|
|
|
|
e.drawRows(ab)
|
|
e.drawStatusBar(ab)
|
|
e.drawMessageBar(ab)
|
|
|
|
ab.Append(terminal.MoveCursor(e.renderX-e.offset.x, e.cursor.y-e.offset.y))
|
|
|
|
ab.Append(terminal.ShowCursor)
|
|
|
|
terminal.Write(ab.ToString())
|
|
}
|
|
|
|
func (e *editor) scroll() {
|
|
e.renderX = 0
|
|
|
|
if e.cursor.y < e.document.rowCount() {
|
|
e.renderX = e.document.rows[e.cursor.y].cursorXToRenderX(e.cursor.x)
|
|
}
|
|
|
|
if e.cursor.y < e.offset.y {
|
|
e.offset.y = e.cursor.y
|
|
}
|
|
|
|
if e.cursor.y >= e.offset.y+e.screen.Rows {
|
|
e.offset.y = e.cursor.y - e.screen.Rows + 1
|
|
}
|
|
|
|
if e.renderX < e.offset.x {
|
|
e.offset.x = e.renderX
|
|
}
|
|
|
|
if e.renderX >= e.offset.x+e.screen.Cols {
|
|
e.offset.x = e.renderX - e.screen.Cols
|
|
}
|
|
}
|
|
|
|
func (e *editor) drawRows(ab *buffer.Buffer) {
|
|
for y := 0; y < e.screen.Rows; y++ {
|
|
fileRow := y + e.offset.y
|
|
|
|
if fileRow >= e.document.rowCount() {
|
|
e.drawPlaceholderRow(y, ab)
|
|
} else {
|
|
rawRow := e.document.rows[fileRow]
|
|
|
|
// If the column offset is greater than the length of the row,
|
|
// just display an empty row
|
|
if e.offset.x > rawRow.size() {
|
|
ab.Append("")
|
|
|
|
continue
|
|
}
|
|
|
|
rowLen := e.document.rows[fileRow].rSize() - e.offset.x
|
|
outputRow := truncate(string(e.document.rows[fileRow].render[e.offset.x:]), rowLen)
|
|
ab.Append(outputRow)
|
|
}
|
|
|
|
ab.AppendLn(terminal.ClearLine)
|
|
}
|
|
}
|
|
|
|
func (e *editor) drawPlaceholderRow(y int, ab *buffer.Buffer) {
|
|
if e.document.rowCount() == 0 && y == e.screen.Rows/3 {
|
|
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
|
|
if len(welcome) > e.screen.Cols {
|
|
welcome = truncate(welcome, e.screen.Cols)
|
|
}
|
|
|
|
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('~')
|
|
}
|
|
}
|
|
|
|
func (e *editor) drawStatusBar(ab *buffer.Buffer) {
|
|
cols := e.screen.Cols
|
|
|
|
ab.Append(terminal.InvertColor)
|
|
|
|
fileName := "[No Name]"
|
|
if e.document.filename != "" {
|
|
fileName = e.document.filename
|
|
}
|
|
modified := ""
|
|
if e.document.dirty {
|
|
modified = "(modified)"
|
|
}
|
|
|
|
leftStatus := fmt.Sprintf("%.20s - %d lines %s", fileName, e.document.rowCount(), modified)
|
|
length := len(leftStatus)
|
|
if length > cols {
|
|
leftStatus = truncate(leftStatus, cols)
|
|
|
|
ab.Append(leftStatus)
|
|
ab.Append(terminal.ResetColor)
|
|
|
|
return
|
|
}
|
|
|
|
rightStatus := fmt.Sprintf("%d/%d", e.cursor.y+1, 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)
|
|
|
|
ab.Append(terminal.ResetColor)
|
|
|
|
return
|
|
}
|
|
|
|
ab.Append(leftStatus)
|
|
|
|
// Pad the rest of the status line
|
|
padding := strings.Repeat(" ", cols-length)
|
|
ab.Append(padding)
|
|
|
|
ab.Append(terminal.ResetColor)
|
|
}
|
|
|
|
func (e *editor) drawMessageBar(ab *buffer.Buffer) {
|
|
ab.Append("\r\n")
|
|
ab.Append(terminal.ClearLine)
|
|
|
|
msg := truncate(e.status.message, e.screen.Cols)
|
|
if len(msg) > 0 && time.Since(e.status.created).Seconds() < 5.0 {
|
|
ab.Append(msg)
|
|
}
|
|
}
|