48 lines
668 B
Go
48 lines
668 B
Go
package editor
|
|
|
|
import (
|
|
"bufio"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type document struct {
|
|
filename string
|
|
rows []*row
|
|
}
|
|
|
|
func newDocument() *document {
|
|
var rows []*row
|
|
|
|
return &document{
|
|
"",
|
|
rows,
|
|
}
|
|
}
|
|
|
|
func (d *document) open(filename string) {
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
log.Fatalf("failed to open file")
|
|
}
|
|
defer file.Close()
|
|
|
|
d.filename = filename
|
|
scanner := bufio.NewScanner(file)
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
d.appendRow(scanner.Text())
|
|
}
|
|
}
|
|
|
|
func (d *document) appendRow(s string) {
|
|
newRow := newRow(s)
|
|
newRow.update()
|
|
d.rows = append(d.rows, newRow)
|
|
}
|
|
|
|
func (d *document) rowCount() int {
|
|
return len(d.rows)
|
|
}
|