Display a row of text

This commit is contained in:
Timothy Warren 2021-03-30 18:00:06 -04:00
parent c455562703
commit b34c146aba
5 changed files with 38 additions and 18 deletions

View File

@ -27,28 +27,33 @@ func (e *editor) RefreshScreen() {
func (e *editor) drawRows(ab *buffer) { func (e *editor) drawRows(ab *buffer) {
for y :=0; y < e.screen.Rows; y += 1 { for y :=0; y < e.screen.Rows; y += 1 {
if y == e.screen.Rows / 3 { if y >= e.screen.Rows {
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) if y == e.screen.Rows / 3 {
if len(welcome) > e.screen.Cols { welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
welcome = truncateString(welcome, e.screen.Cols) if len(welcome) > e.screen.Cols {
} welcome = truncateString(welcome, e.screen.Cols)
}
padding := (e.screen.Cols - len(welcome)) / 2 padding := (e.screen.Cols - len(welcome)) / 2
if padding > 0 { if padding > 0 {
ab.appendRune('~')
padding--
}
for padding > 0 {
padding--
ab.appendRune(' ')
}
ab.append(welcome)
} else {
ab.appendRune('~') ab.appendRune('~')
padding--
} }
for padding > 0 {
padding--
ab.appendRune(' ')
}
ab.append(welcome)
} else { } else {
ab.appendRune('~') ab.append(string(e.rows[0].chars))
} }
ab.append(terminal.ClearLine) ab.append(terminal.ClearLine)
if y < (e.screen.Rows - 1) { if y < (e.screen.Rows - 1) {

View File

@ -27,6 +27,10 @@ func New() *editor {
return &editor{screen, cursor, rows } return &editor{screen, cursor, rows }
} }
func (e *editor) Open() {
e.rows = append(e.rows, NewRow("Hello, world!"))
}
func (e *editor) ProcessKeypress() bool { func (e *editor) ProcessKeypress() bool {
var str string var str string

View File

@ -1,7 +1,9 @@
// Helper functions
package editor package editor
import "strings" import "strings"
// Truncate a string to a length
func truncateString(s string, length int) string { func truncateString(s string, length int) string {
if length < 1 { if length < 1 {
return "" return ""
@ -22,10 +24,12 @@ func truncateString(s string, length int) string {
return buf.String() return buf.String()
} }
// Is this an ASCII character?
func isAscii(char rune) bool { func isAscii(char rune) bool {
return char < 0x80 return char < 0x80
} }
// Is this an ASCII ctrl character?
func isCtrl(char rune) bool { func isCtrl(char rune) bool {
if !isAscii(char) { if !isAscii(char) {
return false return false

View File

@ -4,8 +4,14 @@ type row struct {
chars []rune chars []rune
} }
func NewRow() *row { func NewRow(s string) *row {
return &row {} var chars []rune
for _, ch := range s {
chars = append(chars, ch)
}
return &row {chars}
} }
func (r *row) Size() int { func (r *row) Size() int {

View File

@ -22,6 +22,7 @@ func main() {
defer cleanup(oldState) defer cleanup(oldState)
e := editor.New() e := editor.New()
e.Open()
// The input loop // The input loop
for { for {