-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextblock.go
129 lines (113 loc) · 2.66 KB
/
textblock.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/rivo/tview"
)
type BlockType int
const (
Math BlockType = iota
Text
Code
)
type TextBlock struct {
Type BlockType
Text string
}
func (b BlockType) String() string {
switch b {
case Math:
return "Math"
case Text:
return "Text"
case Code:
return "Code"
default:
return "Unknown"
}
}
// const fileName = "textblocks.json"
var (
namespace = flag.String("namespace", "default-gonoterm", "Namespace for the textblocks")
local = flag.Bool("local", false, "Use local storage")
)
// Save textBlocks to a file
func saveToFile(textBlocks []TextBlock) error {
data, err := json.MarshalIndent(textBlocks, "", " ")
if err != nil {
return err
}
fileName := getFileName()
dir := filepath.Dir(fileName)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
log.Fatalf("Error creating directory: %v", err)
}
}
err = os.WriteFile(fileName, data, 0644)
log.Println("Saved to file: ", fileName)
if err != nil {
log.Fatalf("Error writing to file: %v", err)
}
return nil
}
// Load textBlocks from a file
func loadFromFile() ([]TextBlock, error) {
fileName := getFileName()
data, err := ioutil.ReadFile(fileName)
if err != nil {
if os.IsNotExist(err) {
return []TextBlock{}, nil
}
return nil, err
}
var textBlocks []TextBlock
err = json.Unmarshal(data, &textBlocks)
return textBlocks, err
}
func getFileName() string {
if *local {
return "gonoterm.json"
}
// Use a universal file path
// return "/universal/path/" + *namespace + ".json"
// get a path in home directory config
configDir, err := os.UserConfigDir()
if err != nil {
log.Fatalf("Error getting config directory: %v", err)
}
path := filepath.Join(configDir, "gonoterm", *namespace+".json")
log.Println("Path: ", path)
return path
}
func handleTypeSelection(app *tview.Application, pages *tview.Pages, textBlocks []TextBlock, inputFields *[]*tview.TextArea,
textAreaGrid *tview.Grid) {
currentFocus := app.GetFocus()
for i, inputField := range *inputFields {
if currentFocus == inputField {
modal := tview.NewModal().
SetText("Select the type of the block").
AddButtons([]string{"Text", "Code", "Math"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
switch buttonLabel {
case "Text":
textBlocks[i].Type = Text
case "Code":
textBlocks[i].Type = Code
case "Math":
textBlocks[i].Type = Math
}
updateTextBlocks(textAreaGrid, textBlocks, inputFields)
pages.RemovePage("modal")
})
pages.AddPage("modal", modal, true, true)
app.SetFocus(modal)
break
}
}
}