Skip to content

Commit 42c5a0c

Browse files
Merge pull request #16 from RasmusLindroth/incl
fix relative includes
2 parents c0036d5 + ad5fe25 commit 42c5a0c

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

i3keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/RasmusLindroth/i3keys/internal/web"
1111
)
1212

13-
const version string = "0.0.10"
13+
const version string = "0.0.11"
1414

1515
func helpText(exitCode int) {
1616
fmt.Print("Usage:\n\n\ti3keys [-s] <command> [arguments]\n")

internal/helpers/include.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package helpers
22

33
import (
4+
"errors"
45
"log"
56
"os"
67
"os/exec"
78
"path/filepath"
89
"strings"
910
)
1011

12+
type Include struct {
13+
ParentPath string
14+
Path string
15+
}
16+
1117
//Equal to os/exec Expand but changed ${} to $()
1218
func replaceDollar(s string, mapping func(string) string) string {
1319
var buf []byte
@@ -138,8 +144,13 @@ func checkPath(s string) []string {
138144
return r
139145
}
140146

141-
func GetPaths(s string) ([]string, error) {
142-
s = ExpandCommand(s)
147+
func GetPaths(i Include) ([]string, error) {
148+
s := ExpandCommand(i.Path)
149+
s = os.ExpandEnv(s)
150+
if !filepath.IsAbs(s) {
151+
dir := filepath.Dir(i.ParentPath)
152+
s = filepath.Join(dir, s)
153+
}
143154
matches, err := filepath.Glob(s)
144155
if err != nil {
145156
return nil, err
@@ -150,3 +161,33 @@ func GetPaths(s string) ([]string, error) {
150161
}
151162
return paths, nil
152163
}
164+
165+
func GetSwayDefaultConfig() (string, error) {
166+
home, _ := os.LookupEnv("HOME")
167+
xdgConfig, exists := os.LookupEnv("XDG_CONFIG_HOME")
168+
if !exists {
169+
xdgConfig = home + "/.config"
170+
}
171+
configs := []string{
172+
home + "/.sway/config",
173+
xdgConfig + "/sway/config",
174+
home + "/.i3/config",
175+
xdgConfig + "/i3/config",
176+
"/etc/sway/config",
177+
"/etc/i3/config",
178+
}
179+
configPath := ""
180+
for _, c := range configs {
181+
_, err := os.Stat(c)
182+
if os.IsNotExist(err) {
183+
continue
184+
}
185+
configPath = c
186+
break
187+
}
188+
var e error
189+
if configPath == "" {
190+
e = errors.New("couldn't find a config file")
191+
}
192+
return configPath, e
193+
}

internal/i3parse/parse.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func readLine(reader *bufio.Reader, c context) (string, []string, lineType, erro
108108
return line, lineParts, lineType, err
109109
}
110110

111-
func parseConfig(confReader io.Reader, err error) ([]Mode, []Binding, []Variable, []string, error) {
111+
func parseConfig(confReader io.Reader, confPath string, err error) ([]Mode, []Binding, []Variable, []string, error) {
112112
if err != nil {
113113
return []Mode{}, []Binding{}, []Variable{}, []string{}, errors.New("Couldn't get the config file")
114114
}
@@ -118,7 +118,7 @@ func parseConfig(confReader io.Reader, err error) ([]Mode, []Binding, []Variable
118118
var modes []Mode
119119
var bindings []Binding
120120
var variables []Variable
121-
var includes []string
121+
var includes []helpers.Include
122122

123123
context := mainContext
124124
var readErr error
@@ -145,7 +145,11 @@ func parseConfig(confReader io.Reader, err error) ([]Mode, []Binding, []Variable
145145
modes = append(modes, Mode{Name: name})
146146
continue
147147
case includeLine:
148-
includes = append(includes, strings.Join(lineParts[1:], " "))
148+
inc := helpers.Include{
149+
ParentPath: confPath,
150+
Path: strings.Join(lineParts[1:], " "),
151+
}
152+
includes = append(includes, inc)
149153
continue
150154
case bindCodeBracket:
151155
if context == mainContext {
@@ -210,12 +214,14 @@ func parseConfig(confReader io.Reader, err error) ([]Mode, []Binding, []Variable
210214
}
211215

212216
func parse(confReader io.Reader, err error) ([]Mode, []Binding, error) {
213-
modes, bindings, variables, includes, err := parseConfig(confReader, err)
217+
configPath, _ := helpers.GetSwayDefaultConfig()
218+
modes, bindings, variables, includes, err := parseConfig(confReader, configPath, err)
214219
if err != nil {
215220
return []Mode{}, []Binding{}, errors.New("Couldn't get the config file")
216221
}
217222
var parsedIncludes []string
218-
for _, incl := range includes {
223+
for j := 0; j < len(includes); j++ {
224+
incl := includes[j]
219225
done := false
220226
for _, ap := range parsedIncludes {
221227
if ap == incl {
@@ -229,7 +235,7 @@ func parse(confReader io.Reader, err error) ([]Mode, []Binding, error) {
229235
if err != nil {
230236
log.Printf("couldn't open the included file %s, got err: %v\n", incl, ferr)
231237
}
232-
m, b, v, i, perr := parseConfig(f, err)
238+
m, b, v, i, perr := parseConfig(f, incl, err)
233239
if err != nil {
234240
log.Printf("couldn't parse the included file %s, got err: %v\n", incl, perr)
235241
}
@@ -356,10 +362,8 @@ func replaceVariablesInBindings(variables []Variable, bindings []Binding) []Bind
356362
for mkey := range bindings[key].Modifiers {
357363
bindings[key].Modifiers[mkey] = variableNameToValue(variables, bindings[key].Modifiers[mkey])
358364
}
359-
360365
nb = append(nb, bindings[key])
361366
}
362-
363367
return bindings
364368
}
365369

@@ -368,12 +372,10 @@ func replaceVariablesInModes(variables []Variable, modes []Mode) []Mode {
368372
modes[mkey].Name = variableNameToValue(variables, modes[mkey].Name)
369373
modes[mkey].Bindings = replaceVariablesInBindings(variables, mode.Bindings)
370374
}
371-
372375
return modes
373376
}
374377

375378
func sortModifiers(bindings []Binding) []Binding {
376-
377379
for key := range bindings {
378380
var a []string
379381
var b []string
@@ -388,6 +390,5 @@ func sortModifiers(bindings []Binding) []Binding {
388390
sort.Strings(b)
389391
bindings[key].Modifiers = append(a, b...)
390392
}
391-
392393
return bindings
393394
}

0 commit comments

Comments
 (0)