Finish kilo chapter 4, text viewer
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-01 13:25:59 -04:00
parent a2bfb5f945
commit bd9a07feed
3 changed files with 37 additions and 3 deletions

View File

@ -4,6 +4,7 @@ package editor
import (
"fmt"
"strings"
"time"
"timshome.page/gilo/terminal"
)
@ -21,6 +22,7 @@ func (e *editor) RefreshScreen() {
e.drawRows(ab)
e.drawStatusBar(ab)
e.drawMessageBar(ab)
ab.append(terminal.MoveCursor(e.renderX - e.offset.x, e.cursor.y - e.offset.y))
@ -151,3 +153,13 @@ func (e *editor) drawStatusBar(ab *buffer) {
ab.append(terminal.ResetColor)
}
func (e *editor) drawMessageBar(ab *buffer) {
ab.append("\r\n")
ab.append(terminal.ClearLine)
msg := truncateString(e.status.message, e.screen.Cols)
if len(msg) > 0 && time.Since(e.status.created).Seconds() < 5.0 {
ab.append(msg)
}
}

View File

@ -1,6 +1,8 @@
package editor
import (
"fmt"
"time"
"timshome.page/gilo/terminal"
)
@ -13,29 +15,40 @@ type point struct {
y int
}
type statusMsg struct {
message string
created time.Time
}
type editor struct {
screen *terminal.Screen
cursor *point
offset *point
document *document
status *statusMsg
renderX int
}
func New() *editor {
screen := terminal.Size()
// Subtract rows for status bar and prompt
screen.Rows -= 1
// Subtract rows for status bar and message bar/prompt
screen.Rows -= 2
cursor := &point { 0, 0 }
offset := &point { 0, 0 }
document := newDocument()
status := &statusMsg{
"",
time.Now(),
}
return &editor{
screen,
cursor,
offset,
document,
status,
0,
}
}
@ -44,6 +57,13 @@ func (e *editor) Open(filename string) {
e.document.open(filename)
}
func (e *editor) SetStatusMessage(template string, a ...interface{}) {
e.status = &statusMsg {
fmt.Sprintf(template, a...),
time.Now(),
}
}
func (e *editor) ProcessKeypress() bool {
var str string
@ -110,7 +130,7 @@ func (e *editor) moveCursor (key string) {
e.cursor.y -= 1
}
case keyDown:
if e.cursor.y < (e.document.rowCount() - 1) {
if e.cursor.y < e.document.rowCount() {
e.cursor.y += 1
}
case keyPageUp:

View File

@ -29,6 +29,8 @@ func main() {
e.Open(os.Args[1])
}
e.SetStatusMessage("HELP: Ctrl-Q = quit");
// The input loop
for {
e.RefreshScreen()