From 117381813511b0a2e1affe56b12faf9f3c5b163e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 24 Mar 2021 15:52:35 -0400 Subject: [PATCH] Display centered welcome message --- fn/fn.go | 23 +++++++++++++++++++++++ internal/editor/editor.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 fn/fn.go diff --git a/fn/fn.go b/fn/fn.go new file mode 100644 index 0000000..43bef01 --- /dev/null +++ b/fn/fn.go @@ -0,0 +1,23 @@ +package fn + +import "strings" + +func TruncateString(s string, length int) string { + if length < 1 { + return "" + } + + var buf strings.Builder + count := 0 + + for _, char := range s { + if count == length { + break + } + + buf.WriteRune(char) + count++ + } + + return buf.String() +} \ No newline at end of file diff --git a/internal/editor/editor.go b/internal/editor/editor.go index b22ebdc..cc1b46e 100644 --- a/internal/editor/editor.go +++ b/internal/editor/editor.go @@ -3,11 +3,14 @@ package editor import ( "fmt" "strings" + "timshome.page/gilo/fn" "timshome.page/gilo/internal/ansi" "timshome.page/gilo/internal/char" "timshome.page/gilo/internal/terminal" ) +const KiloVersion = "0.0.1" + // ---------------------------------------------------------------------------- // !Editor // ---------------------------------------------------------------------------- @@ -52,7 +55,28 @@ func (e *editor) ProcessKeypress() bool { func (e *editor) drawRows(ab *buffer) { for y :=0; y < e.rows; y += 1 { - ab.appendRune('~') + if y == e.rows / 3 { + welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) + if len(welcome) > e.cols { + welcome = fn.TruncateString(welcome, e.cols) + } + + padding := (e.cols - len(welcome)) / 2 + if padding > 0 { + ab.appendRune('~') + padding-- + } + + for padding > 0 { + padding-- + ab.appendRune(' ') + } + + ab.append(welcome) + } else { + ab.appendRune('~') + } + ab.append(ansi.ClearLine) if y < (e.rows - 1) { @@ -99,4 +123,4 @@ func (b *buffer) appendLn(s string) int { func (b *buffer) toString() string { return b.buf.String() -} +} \ No newline at end of file