-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (107 loc) · 2.68 KB
/
main.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
fyne "fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
var (
filesPath []string = parseArgs()
properties, keys = getProperties(filesPath)
)
func parseArgs() []string {
if len(os.Args) < 2 {
fmt.Println("No file path provided. Please drag and drop your .ltx file here:")
var filePath string
fmt.Scanln(&filePath)
filePath = strings.Trim(filePath, "\"")
return []string{filePath}
}
return os.Args[1:]
}
func main() {
app := app.New()
// Create a new window and set its default size
fileName := strings.Split(filesPath[0], "/")[len(strings.Split(filesPath[0], "/"))-1]
w := app.NewWindow("lke - .ltx Properties Viewer | " + fileName)
w.Resize(fyne.NewSize(600, 400))
container := container.NewVBox()
properties, keys := getProperties(filesPath)
for _, key := range keys {
value := properties[key]
entry := widget.NewEntry()
entry.SetText(value)
container.Add(widget.NewForm(&widget.FormItem{
Text: key,
Widget: entry,
}))
saveButton := widget.NewButton("Save", func() {
properties[key] = entry.Text
found := false
for _, k := range keys {
if k == key {
found = true
break
}
}
if !found {
keys = append(keys, key)
}
// Save changes to file
ltxFilePath := os.Args[1:]
// Overwrite file
file, err := os.OpenFile(ltxFilePath[0], os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
for _, key := range keys {
_, err := file.WriteString(key + "=" + properties[key] + "\n")
println(key + "=" + properties[key] + "\n")
if err != nil {
log.Fatal(err)
}
}
})
undoButton := widget.NewButton("Undo", func() {
entry.SetText(properties[key])
})
container.Add(saveButton)
container.Add(undoButton)
container.Add(widget.NewSeparator())
}
// footer
container.Add(widget.NewLabel("ltx key-value editor by @editor99 (github.com/gteditor99/ltx)"))
w.SetContent(container)
w.ShowAndRun()
}
// Modify getProperties to return a slice of keys in addition to the map
func getProperties(filesPath []string) (map[string]string, []string) {
properties := make(map[string]string)
var keys []string
for _, filePath := range filesPath {
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
split := strings.SplitN(line, "=", 2)
if len(split) == 2 {
properties[split[0]] = split[1]
keys = append(keys, split[0])
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
return properties, keys
}