From ca81c5a8cfa08d40213932ab589fc6d5ed486974 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 25 Mar 2021 12:27:48 -0400 Subject: [PATCH] Add structs for cursor position and size --- editor/editor.go | 26 ++++++++++++++++---------- terminal/size.go | 15 ++++++++++----- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/editor/editor.go b/editor/editor.go index 7171c23..3289f67 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -12,15 +12,21 @@ const KiloVersion = "0.0.1" // !Editor // ---------------------------------------------------------------------------- +type cursor struct { + x int + y int +} + type editor struct { - rows int - cols int + screen *terminal.Screen + cursor *cursor } func New() *editor { - rows, cols := terminal.Size() + screen := terminal.Size() + cursor := &cursor { 0, 0 } - return &editor{rows, cols} + return &editor{screen, cursor } } func (e *editor) RefreshScreen() { @@ -51,14 +57,14 @@ func (e *editor) ProcessKeypress() bool { } func (e *editor) drawRows(ab *buffer) { - for y :=0; y < e.rows; y += 1 { - if y == e.rows / 3 { + 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.cols { - welcome = fn.TruncateString(welcome, e.cols) + if len(welcome) > e.screen.Cols { + welcome = fn.TruncateString(welcome, e.screen.Cols) } - padding := (e.cols - len(welcome)) / 2 + padding := (e.screen.Cols - len(welcome)) / 2 if padding > 0 { ab.appendRune('~') padding-- @@ -76,7 +82,7 @@ func (e *editor) drawRows(ab *buffer) { ab.append(terminal.ClearLine) - if y < (e.rows - 1) { + if y < (e.screen.Rows - 1) { ab.append("\r\n") } } diff --git a/terminal/size.go b/terminal/size.go index 89c765a..982421a 100644 --- a/terminal/size.go +++ b/terminal/size.go @@ -6,23 +6,28 @@ import ( "os" ) +type Screen struct { + Rows int + Cols int +} + // Get the size of the terminal in rows and columns -func Size () (rows int, cols int) { +func Size () *Screen { check() - cols = 80 - rows = 24 + cols := 80 + rows := 24 // Try the syscall first cols, rows, err := term.GetSize(int(os.Stdin.Fd())) if err == nil { - return rows, cols + return &Screen {rows, cols } } // Figure out the size the hard way rows, cols = sizeTrick() - return rows, cols + return &Screen{ rows, cols } } func sizeTrick () (rows int, cols int) {