From b34c146aba7475836f181655734bb0f339cad41d Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 30 Mar 2021 18:00:06 -0400 Subject: [PATCH] Display a row of text --- editor/draw.go | 37 +++++++++++++++++++++---------------- editor/editor.go | 4 ++++ editor/fn.go | 4 ++++ editor/row.go | 10 ++++++++-- gilo.go | 1 + 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/editor/draw.go b/editor/draw.go index f1601d9..7ac31dd 100644 --- a/editor/draw.go +++ b/editor/draw.go @@ -27,28 +27,33 @@ func (e *editor) RefreshScreen() { func (e *editor) drawRows(ab *buffer) { for y :=0; y < e.screen.Rows; y += 1 { - if y == e.screen.Rows / 3 { - welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) - if len(welcome) > e.screen.Cols { - welcome = truncateString(welcome, e.screen.Cols) - } + if y >= e.screen.Rows { + if y == e.screen.Rows / 3 { + welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) + if len(welcome) > e.screen.Cols { + welcome = truncateString(welcome, e.screen.Cols) + } - padding := (e.screen.Cols - len(welcome)) / 2 - if padding > 0 { + 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('~') - padding-- } - - for padding > 0 { - padding-- - ab.appendRune(' ') - } - - ab.append(welcome) } else { - ab.appendRune('~') + ab.append(string(e.rows[0].chars)) } + ab.append(terminal.ClearLine) if y < (e.screen.Rows - 1) { diff --git a/editor/editor.go b/editor/editor.go index 7e401ea..05ca7e5 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -27,6 +27,10 @@ func New() *editor { return &editor{screen, cursor, rows } } +func (e *editor) Open() { + e.rows = append(e.rows, NewRow("Hello, world!")) +} + func (e *editor) ProcessKeypress() bool { var str string diff --git a/editor/fn.go b/editor/fn.go index 11faaf9..2ba29e4 100644 --- a/editor/fn.go +++ b/editor/fn.go @@ -1,7 +1,9 @@ +// Helper functions package editor import "strings" +// Truncate a string to a length func truncateString(s string, length int) string { if length < 1 { return "" @@ -22,10 +24,12 @@ func truncateString(s string, length int) string { return buf.String() } +// Is this an ASCII character? func isAscii(char rune) bool { return char < 0x80 } +// Is this an ASCII ctrl character? func isCtrl(char rune) bool { if !isAscii(char) { return false diff --git a/editor/row.go b/editor/row.go index d00b0c8..fb51558 100644 --- a/editor/row.go +++ b/editor/row.go @@ -4,8 +4,14 @@ type row struct { chars []rune } -func NewRow() *row { - return &row {} +func NewRow(s string) *row { + var chars []rune + + for _, ch := range s { + chars = append(chars, ch) + } + + return &row {chars} } func (r *row) Size() int { diff --git a/gilo.go b/gilo.go index 248949f..aa1d408 100644 --- a/gilo.go +++ b/gilo.go @@ -22,6 +22,7 @@ func main() { defer cleanup(oldState) e := editor.New() + e.Open() // The input loop for {