1
0
Fork 0

Finish kilo chapter 4, text viewer
timw4mail/gilo/pipeline/head This commit looks good Details

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

View File

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