diff --git a/editor/draw.go b/editor/draw.go index 7ac31dd..648425e 100644 --- a/editor/draw.go +++ b/editor/draw.go @@ -26,9 +26,9 @@ func (e *editor) RefreshScreen() { } func (e *editor) drawRows(ab *buffer) { - for y :=0; y < e.screen.Rows; y += 1 { - if y >= e.screen.Rows { - if y == e.screen.Rows / 3 { + for y :=0; y < e.screen.Rows; y++ { + if y >= len(e.rows) { + if len(e.rows) == 0 && 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) @@ -50,10 +50,10 @@ func (e *editor) drawRows(ab *buffer) { ab.appendRune('~') } } else { - ab.append(string(e.rows[0].chars)) + row := truncateString(string(e.rows[y].chars), e.screen.Cols) + ab.append(row) } - ab.append(terminal.ClearLine) if y < (e.screen.Rows - 1) { diff --git a/editor/editor.go b/editor/editor.go index 883571b..c6f0c93 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -26,7 +26,6 @@ func New() *editor { screen := terminal.Size() cursor := &cursor { 0, 0 } var rows []*row - rows = append(rows, NewRow("")) return &editor{screen, cursor, rows } } @@ -41,13 +40,9 @@ func (e *editor) Open(filename string) { scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanLines) - var lines []string - for scanner.Scan() { - lines = append(lines, scanner.Text()) + e.appendRow(scanner.Text()) } - - e.rows = append(e.rows, NewRow(lines[0])) } func (e *editor) ProcessKeypress() bool { @@ -173,4 +168,9 @@ func parseEscapeSequence () string { } return string('\x1b') +} + +func (e *editor) appendRow(s string) { + newRow := NewRow(s) + e.rows = append(e.rows, newRow) } \ No newline at end of file diff --git a/editor/fn.go b/editor/fn.go index 2ba29e4..9ab445f 100644 --- a/editor/fn.go +++ b/editor/fn.go @@ -9,6 +9,10 @@ func truncateString(s string, length int) string { return "" } + if len(s) < length { + return s + } + var buf strings.Builder count := 0