Use Buffer.WriteTo method for writing buffer to file

This commit is contained in:
Luke I. Wilson 2021-03-23 18:13:55 -05:00
parent 40e1b40672
commit 3be12bdff1
2 changed files with 10 additions and 6 deletions

14
main.go
View File

@ -129,12 +129,16 @@ func main() {
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx()) tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
te := tab.Child.(*ui.TextEdit) te := tab.Child.(*ui.TextEdit)
if len(te.FilePath) > 0 { if len(te.FilePath) > 0 {
// Write the contents into the file, creating one if it does f, err := os.OpenFile(te.FilePath, os.O_WRONLY|os.O_CREATE, fs.ModePerm)
// not exist.
err := ioutil.WriteFile(te.FilePath, te.Buffer.Bytes(), fs.ModePerm)
if err != nil { if err != nil {
panic("Could not write file at path " + te.FilePath) panic(err)
} // TODO: Replace with io.Writer method }
defer f.Close()
_, err = te.Buffer.WriteTo(f) // TODO: check count
if err != nil {
panic(fmt.Sprintf("Error occurred while writing buffer to file: %v", err))
}
te.Dirty = false te.Dirty = false
} }

View File

@ -357,7 +357,7 @@ func (t *TextEdit) Draw(s tcell.Screen) {
selEndIdx := len(lineRunes) - t.scrollx - 1 // used inclusively selEndIdx := len(lineRunes) - t.scrollx - 1 // used inclusively
if line == t.selection.EndLine { // If the selection ends somewhere in the line... if line == t.selection.EndLine { // If the selection ends somewhere in the line...
tabCount := t.getTabCountInLineAtCol(line, t.selection.EndCol) tabCount := t.getTabCountInLineAtCol(line, t.selection.EndCol)
selEndIdx = t.selection.EndCol - 1 + tabCount*(t.TabSize-1) - t.scrollx selEndIdx = t.selection.EndCol + tabCount*(t.TabSize-1) - t.scrollx
} }
// NOTE: a special draw function just for selections. Should combine this with ordinary draw // NOTE: a special draw function just for selections. Should combine this with ordinary draw