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 package editor
import ( import (
"bufio"
"log"
"os"
"timshome.page/gilo/terminal" "timshome.page/gilo/terminal"
) )
@ -23,12 +26,28 @@ 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 }
} }
func (e *editor) Open() { func (e *editor) Open(filename string) {
e.rows = append(e.rows, NewRow("Hello, world!")) 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 { func (e *editor) ProcessKeypress() bool {

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"golang.org/x/term" "golang.org/x/term"
"os"
"timshome.page/gilo/editor" "timshome.page/gilo/editor"
"timshome.page/gilo/terminal" "timshome.page/gilo/terminal"
) )
@ -22,7 +23,11 @@ func main() {
defer cleanup(oldState) defer cleanup(oldState)
e := editor.New() 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 // The input loop
for { for {