2021-04-01 12:02:17 -04:00
|
|
|
package editor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-04-02 13:11:54 -04:00
|
|
|
"timshome.page/gilo/internal/buffer"
|
2021-04-02 14:52:44 -04:00
|
|
|
"timshome.page/gilo/internal/gilo"
|
2021-04-01 12:02:17 -04:00
|
|
|
)
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
type Document struct {
|
2021-04-01 20:26:40 -04:00
|
|
|
dirty bool
|
2021-04-01 12:02:17 -04:00
|
|
|
filename string
|
2021-04-02 14:52:44 -04:00
|
|
|
rows []*Row
|
2021-04-01 12:02:17 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
func NewDocument() *Document {
|
|
|
|
var rows []*Row
|
2021-04-01 12:02:17 -04:00
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
return &Document{
|
2021-04-01 20:26:40 -04:00
|
|
|
false,
|
2021-04-01 12:02:17 -04:00
|
|
|
"",
|
|
|
|
rows,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
func (d *Document) Open(filename string) {
|
2021-04-01 12:02:17 -04:00
|
|
|
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() {
|
2021-04-02 14:52:44 -04:00
|
|
|
d.AppendRow(scanner.Text())
|
2021-04-01 12:02:17 -04:00
|
|
|
}
|
2021-04-02 10:07:37 -04:00
|
|
|
|
|
|
|
d.dirty = false
|
2021-04-01 12:02:17 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
func (d *Document) Save() int {
|
2021-04-01 20:23:51 -04:00
|
|
|
if d.filename == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:26:40 -04:00
|
|
|
file, err := os.OpenFile(d.filename, os.O_RDWR|os.O_CREATE, 0644)
|
2021-04-01 20:23:51 -04:00
|
|
|
if err != nil {
|
|
|
|
panic("failed to open/create file for saving")
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
fileStr := d.ToString()
|
2021-04-01 20:23:51 -04:00
|
|
|
fileLen := len(fileStr)
|
|
|
|
|
|
|
|
err = file.Truncate(int64(fileLen))
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to truncate file")
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := file.WriteString(fileStr)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-02 14:52:44 -04:00
|
|
|
panic("failed to save Document to file")
|
2021-04-01 20:23:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if fileLen == size {
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:07:37 -04:00
|
|
|
d.dirty = false
|
|
|
|
|
2021-04-01 20:23:51 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
func (d *Document) Row(at int) *Row {
|
|
|
|
return d.rows[at]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Document) AppendRow(s string) {
|
2021-04-01 12:02:17 -04:00
|
|
|
newRow := newRow(s)
|
|
|
|
newRow.update()
|
|
|
|
d.rows = append(d.rows, newRow)
|
2021-04-02 10:07:37 -04:00
|
|
|
|
|
|
|
d.dirty = true
|
2021-04-01 12:02:17 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
func (d *Document) ToString() string {
|
2021-04-02 13:11:54 -04:00
|
|
|
buf := buffer.NewBuffer()
|
2021-04-01 20:23:51 -04:00
|
|
|
|
|
|
|
for _, row := range d.rows {
|
2021-04-02 13:11:54 -04:00
|
|
|
buf.Append(row.toString())
|
|
|
|
buf.AppendRune('\n')
|
2021-04-01 20:23:51 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 13:11:54 -04:00
|
|
|
return buf.ToString()
|
2021-04-01 20:23:51 -04:00
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
func (d *Document) RowCount() int {
|
2021-04-01 12:02:17 -04:00
|
|
|
return len(d.rows)
|
2021-04-01 16:17:13 -04:00
|
|
|
}
|
2021-04-02 14:52:44 -04:00
|
|
|
|
|
|
|
func (d *Document) InsertChar(at *gilo.Point, ch rune) {
|
|
|
|
d.rows[at.Y].insertRune(ch, at.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Document) DelChar(at *gilo.Point) {
|
|
|
|
d.rows[at.Y].deleteRune(at.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Document) IsDirty() bool {
|
|
|
|
return d.dirty
|
|
|
|
}
|