add SaveToFile(string) api

This commit is contained in:
lionsoul2014 2026-04-24 09:41:02 +08:00
parent 69d267d85d
commit c314ef54ca
1 changed files with 8 additions and 8 deletions

View File

@ -270,10 +270,6 @@ func (e *Editor) PutSegment(seg *Segment, cb func(newSeg *Segment, oldList []*Se
}) })
} }
// check and merge the sList
// for all the continuous segments with the same region
sList = MergeSegments(sList)
// print for debug // print for debug
// for i, s := range sList { // for i, s := range sList {
// fmt.Printf("replace -> %d: %s\n", i, s) // fmt.Printf("replace -> %d: %s\n", i, s)
@ -371,16 +367,22 @@ func (e *Editor) PutFile(src string, cb func(newSeg *Segment, oldList []*Segment
return oldRows, newRows, nil return oldRows, newRows, nil
} }
// save the changes to the source file.
func (e *Editor) Save() error { func (e *Editor) Save() error {
return e.SaveToFile(e.srcPath)
}
func (e *Editor) SaveToFile(dstFile string) error {
// check the to-save flag // check the to-save flag
if !e.toSave { if !e.toSave {
return fmt.Errorf("nothing changed") return fmt.Errorf("nothing changed")
} }
dstHandle, err := os.OpenFile(e.srcPath, os.O_WRONLY|os.O_TRUNC, 0644) dstHandle, err := os.OpenFile(dstFile, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil { if err != nil {
return err return err
} }
defer dstHandle.Close()
// loop and flush all the segments to the dstHandle // loop and flush all the segments to the dstHandle
var next *list.Element var next *list.Element
@ -405,9 +407,7 @@ func (e *Editor) Save() error {
} }
} }
// close the handle // close the to-save flag
// and close the to-save flag
_ = dstHandle.Close()
e.toSave = false e.toSave = false
return nil return nil