From 3be12bdff1e47b85ff289630b16d4ffc7c9ab8d0 Mon Sep 17 00:00:00 2001 From: "Luke I. Wilson" Date: Tue, 23 Mar 2021 18:13:55 -0500 Subject: [PATCH] Use Buffer.WriteTo method for writing buffer to file --- main.go | 14 +++++++++----- ui/textedit.go | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 5faa9b1..4d75595 100644 --- a/main.go +++ b/main.go @@ -129,12 +129,16 @@ func main() { tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx()) te := tab.Child.(*ui.TextEdit) if len(te.FilePath) > 0 { - // Write the contents into the file, creating one if it does - // not exist. - err := ioutil.WriteFile(te.FilePath, te.Buffer.Bytes(), fs.ModePerm) + f, err := os.OpenFile(te.FilePath, os.O_WRONLY|os.O_CREATE, fs.ModePerm) if err != nil { - panic("Could not write file at path " + te.FilePath) - } // TODO: Replace with io.Writer method + panic(err) + } + 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 } diff --git a/ui/textedit.go b/ui/textedit.go index c825fca..3eab94c 100644 --- a/ui/textedit.go +++ b/ui/textedit.go @@ -357,7 +357,7 @@ func (t *TextEdit) Draw(s tcell.Screen) { selEndIdx := len(lineRunes) - t.scrollx - 1 // used inclusively if line == t.selection.EndLine { // If the selection ends somewhere in the line... 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