Display centered welcome message

This commit is contained in:
Timothy Warren 2021-03-24 15:52:35 -04:00
parent ca6e38fc51
commit 1173818135
2 changed files with 49 additions and 2 deletions

23
fn/fn.go Normal file
View File

@ -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()
}

View File

@ -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()
}
}