2021-04-01 12:02:17 -04:00
|
|
|
package editor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type document struct {
|
|
|
|
filename string
|
2021-04-01 16:47:32 -04:00
|
|
|
rows []*row
|
2021-04-01 12:02:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-04-01 16:17:13 -04:00
|
|
|
}
|