From bd9a07feed7d7c744a246d34ca25347aaa3d4c0a Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 1 Apr 2021 13:25:59 -0400 Subject: [PATCH] Finish kilo chapter 4, text viewer --- editor/draw.go | 12 ++++++++++++ editor/editor.go | 26 +++++++++++++++++++++++--- gilo.go | 2 ++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/editor/draw.go b/editor/draw.go index 34adcb3..dcc9970 100644 --- a/editor/draw.go +++ b/editor/draw.go @@ -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) + } +} diff --git a/editor/editor.go b/editor/editor.go index 16c6db2..f1664a8 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -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: diff --git a/gilo.go b/gilo.go index dfc8531..9260e88 100644 --- a/gilo.go +++ b/gilo.go @@ -29,6 +29,8 @@ func main() { e.Open(os.Args[1]) } + e.SetStatusMessage("HELP: Ctrl-Q = quit"); + // The input loop for { e.RefreshScreen()