-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyprlang_parser.go
147 lines (131 loc) · 4.55 KB
/
hyprlang_parser.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package hyprlang_parser
import (
"errors"
)
type configFile struct {
path string // Path of the file
content []string // Content, line by line, of the file
changed bool // Store if config has changed
}
type Config struct {
configFiles []configFile // List of config files (sources included)
indentation int // Indentation of the config file, based on the first section of the entry file
}
// LoadConfig loads config at {configPath} and returns a Config struct
// It also adds the sourced configuration files
func LoadConfig(configPath string) (config Config, err error) {
err = config.addConfigFile(configPath)
config.indentation = getIndentation(&config.configFiles[0].content)
return
}
// WriteConfig writes/saves changed configs
func (c *Config) WriteConfig() error {
for i, configFile := range c.configFiles {
if configFile.changed {
err := configFile.writeFile()
if err != nil {
return err
}
c.configFiles[i].changed = false
}
}
return nil
}
// GetFirst returns the first value for {variable} at {section}
func (c *Config) GetFirst(section, variable string) string {
var value string
for _, configFile := range c.configFiles {
values := getVariables(configFile.content, parseSectionString(section), variable, -1)
if len(values) > 0 {
return values[0]
}
}
return value
}
// GetAll returns all the values for {variable} at {section}
func (c *Config) GetAll(section, variable string) []string {
var values []string
for _, configFile := range c.configFiles {
values = append(values, getVariables(configFile.content, parseSectionString(section), variable, 0)...)
}
return values
}
// EditFirst changes the value of the first {variable} at {section} to {value}
// It returns an error if the variable was not found
func (c *Config) EditFirst(section, variable, value string) error {
var isEdited bool
for i, configFile := range c.configFiles {
isEdited = editVariableN(&configFile.content, parseSectionString(section), variable, value, 0, c.indentation)
if isEdited {
c.configFiles[i].changed = true
return nil
}
}
return errors.New("Variable not found (Not edited).")
}
// EditN changes the value of the {n} {variable} at {section} to {value}
// It returns an error if the variable was not found
func (c *Config) EditN(section, variable, value string, n int) error {
var isEdited bool
for i, configFile := range c.configFiles {
isEdited = editVariableN(&configFile.content, parseSectionString(section), variable, value, n, c.indentation)
if isEdited {
c.configFiles[i].changed = true
return nil
}
}
return errors.New("Variable not found (Not edited).")
}
// Add creates a {variable} at {section} with the {value} value
// It creates the sections if they don't exist
// If sections are not found, it will add the sections and the variable to the first config file
func (c *Config) Add(section, variable, value string) {
var whereSectionExist int
var exist bool = false
for i, configFile := range c.configFiles {
exist = doesSectionExist(&configFile.content, parseSectionString(section))
if exist {
whereSectionExist = i
break
}
}
if exist {
addVariable(&c.configFiles[whereSectionExist].content, parseSectionString(section), variable, value, c.indentation)
c.configFiles[whereSectionExist].changed = true
} else {
parsedSection := parseSectionString(section)
for i := 0; i < len(parsedSection); i++ {
if !doesSectionExist(&c.configFiles[0].content, parsedSection[:i+1]) {
addSection(&c.configFiles[0].content, parsedSection[:i], parsedSection[i], c.indentation)
}
}
addVariable(&c.configFiles[0].content, parsedSection, variable, value, c.indentation)
c.configFiles[0].changed = true
}
}
// RemoveFirst removes the first {variable} at {section}
// It returns an error if the variable is not found
func (c *Config) RemoveFirst(section, variable string) error {
var isRemoved bool
for i, configFile := range c.configFiles {
isRemoved = removeVariableN(&configFile.content, parseSectionString(section), variable, 0)
if isRemoved {
c.configFiles[i].changed = true
return nil
}
}
return errors.New("Variable not found (Not removed).")
}
// RemoveN removes the {n} {variable} at {section}
// It returns an error if the variable is not found
func (c *Config) RemoveN(section, variable string, n int) error {
var isRemoved bool
for i, configFile := range c.configFiles {
isRemoved = removeVariableN(&configFile.content, parseSectionString(section), variable, n)
if isRemoved {
c.configFiles[i].changed = true
return nil
}
}
return errors.New("Variable not found (Not removed).")
}