Open first line of a file

This commit is contained in:
Timothy Warren 2021-03-30 18:29:23 -04:00
parent b34c146aba
commit 067b38c26a
2 changed files with 27 additions and 3 deletions

View File

@ -1,6 +1,9 @@
package editor
import (
"bufio"
"log"
"os"
"timshome.page/gilo/terminal"
)
@ -23,12 +26,28 @@ func New() *editor {
screen := terminal.Size()
cursor := &cursor { 0, 0 }
var rows []*row
rows = append(rows, NewRow(""))
return &editor{screen, cursor, rows }
}
func (e *editor) Open() {
e.rows = append(e.rows, NewRow("Hello, world!"))
func (e *editor) Open(filename string) {
file, err := os.Open(filename)
if err != nil {
log.Fatalf("failed to open file")
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
e.rows = append(e.rows, NewRow(lines[0]))
}
func (e *editor) ProcessKeypress() bool {

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"golang.org/x/term"
"os"
"timshome.page/gilo/editor"
"timshome.page/gilo/terminal"
)
@ -22,7 +23,11 @@ func main() {
defer cleanup(oldState)
e := editor.New()
e.Open()
// If there is an argument passed, open it as a file to edit
if len(os.Args) >= 2 && os.Args[1] != "" {
e.Open(os.Args[1])
}
// The input loop
for {