Non-scrolling file viewer

This commit is contained in:
Timothy Warren 2021-03-30 18:43:46 -04:00
parent 067b38c26a
commit 7d435d1556
3 changed files with 15 additions and 11 deletions

View File

@ -26,9 +26,9 @@ 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++ {
if y >= e.screen.Rows { if y >= len(e.rows) {
if y == e.screen.Rows / 3 { if len(e.rows) == 0 && y == e.screen.Rows / 3 {
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
if len(welcome) > e.screen.Cols { if len(welcome) > e.screen.Cols {
welcome = truncateString(welcome, e.screen.Cols) welcome = truncateString(welcome, e.screen.Cols)
@ -50,10 +50,10 @@ func (e *editor) drawRows(ab *buffer) {
ab.appendRune('~') ab.appendRune('~')
} }
} else { } 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) ab.append(terminal.ClearLine)
if y < (e.screen.Rows - 1) { if y < (e.screen.Rows - 1) {

View File

@ -26,7 +26,6 @@ func New() *editor {
screen := terminal.Size() screen := terminal.Size()
cursor := &cursor { 0, 0 } cursor := &cursor { 0, 0 }
var rows []*row var rows []*row
rows = append(rows, NewRow(""))
return &editor{screen, cursor, rows } return &editor{screen, cursor, rows }
} }
@ -41,13 +40,9 @@ func (e *editor) Open(filename string) {
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines) scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() { 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 { func (e *editor) ProcessKeypress() bool {
@ -173,4 +168,9 @@ func parseEscapeSequence () string {
} }
return string('\x1b') return string('\x1b')
}
func (e *editor) appendRow(s string) {
newRow := NewRow(s)
e.rows = append(e.rows, newRow)
} }

View File

@ -9,6 +9,10 @@ func truncateString(s string, length int) string {
return "" return ""
} }
if len(s) < length {
return s
}
var buf strings.Builder var buf strings.Builder
count := 0 count := 0