-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave-load.go
72 lines (61 loc) · 1.3 KB
/
save-load.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/charmbracelet/bubbles/textinput"
"gopkg.in/yaml.v3"
)
func Save(m *model) {
yamlData, err := yaml.Marshal(&m)
if err != nil {
fmt.Println("Error:", err)
return
}
dirPath := filepath.Dir(filename)
err = os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
fmt.Println("Error creating directory:", err)
return
}
writenData := strings.ReplaceAll(string(yamlData), "\n -", "\n\n -")
writenData = strings.ReplaceAll(writenData, ":\n", ":")
err = os.WriteFile(filename, []byte(writenData), 0o644)
if err != nil {
fmt.Println("Error:", err)
return
}
}
func Load() model {
ti := textinput.New()
ti.Placeholder = "To Do"
ti.Focus()
ti.CharLimit = 200
ti.Width = 35
if _, err := os.Stat(filename); os.IsNotExist(err) {
return model{
TaskList: []task{
{"Do this", true, 0},
{"Do that", false, 1},
{"this is a never ending cycle", false, 2},
},
textInput: ti,
state: 1,
err: nil,
}
}
loadedData, err := os.ReadFile(filename)
if err != nil {
fmt.Println("Error:", err)
return model{}
}
var loadedModel model
err = yaml.Unmarshal(loadedData, &loadedModel)
if err != nil {
fmt.Println("Error:", err)
}
loadedModel.textInput = ti
loadedModel.state = 1
return loadedModel
}