-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
53 lines (40 loc) · 878 Bytes
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"os"
)
type File struct {
path string
}
func NewFile(path string) *File {
//if file doesnt exist create file
if _, err := os.Stat(path); os.IsNotExist(err) {
file, err := os.Create(path)
if err != nil {
panic(err)
}
file.Close()
}
file := File{path: path}
return &file
}
func (f *File) ReadFile() []byte {
data, err := os.ReadFile(f.path)
if err != nil {
panic(err)
}
return (data)
}
func (f *File) Save(tb *TabBuffer) {
// read through all the lines in the tab buffer and add thier runes to a buffer array
// then write the buffer array to the file
validLines := tb.GetValidLines()
var buffer []byte
for i, line := range validLines {
runes := line.GetRunes()
buffer = append(buffer, []byte(runes)...)
if i < len(validLines) - 1 {
buffer = append(buffer, 10)
}
}
os.WriteFile(f.path, buffer, 0644)
}