Skip to content

Commit f63dc0a

Browse files
Merge pull request #11 from RasmusLindroth/filter
Add filter and TOC
2 parents 4f4e39f + 4ae552a commit f63dc0a

File tree

13 files changed

+239
-106
lines changed

13 files changed

+239
-106
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Rasmus Lindroth
3+
Copyright (c) 2020 Rasmus Lindroth
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,7 @@ git clone https://github.com/RasmusLindroth/i3keys.git
3939
//Install
4040
go install
4141
42-
//Run web interface on port 8080
43-
i3keys web 8080
44-
45-
//or output text to the terminal
46-
i3keys text ISO
47-
48-
//or output SVG to the current directory
49-
i3keys svg ISO ./
42+
/
5043
5144
//If starting doesn't work try running it from $HOME/go/bin
5245
```
@@ -62,16 +55,25 @@ cd $HOME/go/src/github.com/RasmusLindroth/i3keys
6255
//Install
6356
go install
6457
58+
//If starting doesn't work try running it from $HOME/go/bin
59+
```
60+
61+
Example usage
62+
```
6563
//Run web interface on port 8080
6664
i3keys web 8080
6765
6866
//or output text to the terminal
6967
i3keys text ISO
7068
69+
//or filter text output
70+
i3keys text ISO Mod4+Ctrl
71+
7172
//or output SVG to the current directory
7273
i3keys svg ISO ./
7374
74-
//If starting doesn't work try running it from $HOME/go/bin
75+
//or filter SVG
76+
i3keys svg ISO ./ Mod4+Ctrl
7577
```
7678

7779
If you still having problems see the
@@ -130,10 +132,29 @@ Usage:
130132
131133
The commands are:
132134
133-
web <port> start the web ui and listen on <port>
134-
text <layout> output available keybindings in the terminal. <layout> can be ISO or ANSI
135-
svg <layout> [dest] outputs one SVG file for each modifier group. <layout> can be ISO or ANSI, [dest] defaults to current directory
136-
version print i3keys version
135+
web <port>
136+
start the web ui and listen on <port>
137+
138+
text <layout> [mods]
139+
output available keybindings in the terminal
140+
141+
svg <layout> [dest] [mods]
142+
outputs one SVG file for each modifier group
143+
144+
version
145+
print i3keys version
146+
147+
Arguments:
148+
149+
<layout>
150+
is required. Can be ISO or ANSI
151+
152+
[mods]
153+
is optional. Can be a single modifier or a group of modifiers. Group them with a plus sign, e.g. Mod4+Ctrl
154+
155+
[dest]
156+
is optional. Where to output files, defaults to the current directory
157+
137158
```
138159

139160
### Disclaimer

i3keys.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ import (
1111
"github.com/RasmusLindroth/i3keys/internal/web"
1212
)
1313

14-
const version string = "0.0.7"
14+
const version string = "0.0.8"
1515

1616
func helpText(exitCode int) {
1717
fmt.Printf("Usage:\n\n\ti3keys <command> [arguments]\n\n")
1818
fmt.Printf("The commands are:\n\n")
19-
fmt.Println("\tweb <port> start the web ui and listen on <port>")
20-
fmt.Println("\ttext <layout> output available keybindings in the terminal. <layout> can be ISO or ANSI")
21-
fmt.Println("\tsvg <layout> [dest] outputs one SVG file for each modifier group. <layout> can be ISO or ANSI, [dest] defaults to current directory")
22-
fmt.Println("\tversion print i3keys version")
19+
fmt.Print("\tweb <port>\n\t\tstart the web ui and listen on <port>\n\n")
20+
fmt.Print("\ttext <layout> [mods]\n\t\toutput available keybindings in the terminal\n\n")
21+
fmt.Print("\tsvg <layout> [dest] [mods]\n\t\toutputs one SVG file for each modifier group\n\n")
22+
fmt.Print("\tversion\n\t\tprint i3keys version\n\n")
23+
fmt.Printf("Arguments:\n\n")
24+
fmt.Print("\t<layout>\n\t\tis required. Can be ISO or ANSI\n\n")
25+
fmt.Print("\t[mods]\n\t\tis optional. Can be a single modifier or a group of modifiers. Group them with a plus sign, e.g. Mod4+Ctrl\n\n")
26+
fmt.Print("\t[dest]\n\t\tis optional. Where to output files, defaults to the current directory\n\n")
2327
os.Exit(exitCode)
2428
}
2529

@@ -55,12 +59,18 @@ func main() {
5559
case "web":
5660
web.Output(os.Args[2])
5761
case "text":
58-
text.Output(os.Args[2])
62+
if len(os.Args) < 4 {
63+
text.Output(os.Args[2], "")
64+
} else {
65+
text.Output(os.Args[2], os.Args[3])
66+
}
5967
case "svg":
6068
if len(os.Args) < 4 {
61-
svg.Output(os.Args[2], "")
69+
svg.Output(os.Args[2], "", "")
70+
} else if len(os.Args) < 5 {
71+
svg.Output(os.Args[2], os.Args[3], "")
6272
} else {
63-
svg.Output(os.Args[2], os.Args[3])
73+
svg.Output(os.Args[2], os.Args[3], os.Args[4])
6474
}
6575
case "version":
6676
fmt.Printf("i3keys version %s by Rasmus Lindroth\n", version)

internal/helpers/parse.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package helpers
22

33
import (
4+
"sort"
45
"strings"
56
"unicode"
67
)
@@ -40,3 +41,37 @@ func SplitBySpace(s string) []string {
4041

4142
return parts
4243
}
44+
45+
//HandleFilterArgs sorts and splits strings like Mod4+Shift
46+
func HandleFilterArgs(s string) []string {
47+
var a []string
48+
var b []string
49+
50+
parts := strings.Split(s, "+")
51+
for _, item := range parts {
52+
p := strings.Title(item)
53+
if len(p) > 2 && p[:3] == "Mod" {
54+
a = append(a, p)
55+
continue
56+
}
57+
b = append(b, p)
58+
}
59+
sort.Strings(a)
60+
sort.Strings(b)
61+
return append(a, b...)
62+
}
63+
64+
//CompareSlices compares if two slices are equal
65+
func CompareSlices(a []string, b []string) bool {
66+
if len(a) != len(b) {
67+
return false
68+
}
69+
70+
for i, val := range a {
71+
if val != b[i] {
72+
return false
73+
}
74+
}
75+
76+
return true
77+
}

internal/i3parse/codes.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package i3parse
22

33
import (
4-
"errors"
4+
"fmt"
55
"strconv"
66

77
"github.com/RasmusLindroth/i3keys/internal/xlib"
88
)
99

1010
//CodeToSymbol returns a code binding with the symbol equivalent
11-
func CodeToSymbol(code Binding) (Binding, error) {
12-
i, err := strconv.Atoi(code.Key)
11+
func CodeToSymbol(key string) (string, error) {
12+
i, err := strconv.Atoi(key)
1313

1414
if err != nil {
15-
return Binding{}, errors.New("Couldn't parse string to int")
15+
return "", fmt.Errorf("Couldn't parse string %s to int", key)
1616
}
1717

1818
hex := xlib.KeyCodeToHex(i)
19-
if name, ok := xlib.KeySyms[hex]; ok {
20-
code.Key = name
21-
code.Type = SymbolBinding
22-
23-
return code, nil
19+
name, ok := xlib.KeySyms[hex]
20+
if !ok {
21+
return "", fmt.Errorf("Keycode %s not in keysymdef.h", key)
2422
}
2523

26-
return Binding{}, errors.New("Keycode not in keysymdef.h")
24+
return name, nil
2725
}

internal/i3parse/group.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,14 @@ package i3parse
22

33
import "sort"
44

5-
func compareSlices(a []string, b []string) bool {
6-
if len(a) != len(b) {
7-
return false
8-
}
9-
10-
for i, val := range a {
11-
if val != b[i] {
12-
return false
13-
}
14-
}
15-
16-
return true
17-
}
5+
import "github.com/RasmusLindroth/i3keys/internal/helpers"
186

19-
type sortByNumBindings []ModifierGroup
7+
type sortByNumModifiers []ModifierGroup
208

21-
func (a sortByNumBindings) Len() int { return len(a) }
22-
func (a sortByNumBindings) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
23-
func (a sortByNumBindings) Less(i, j int) bool {
24-
return len(a[i].Bindings) > len(a[j].Bindings)
9+
func (a sortByNumModifiers) Len() int { return len(a) }
10+
func (a sortByNumModifiers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
11+
func (a sortByNumModifiers) Less(i, j int) bool {
12+
return len(a[i].Modifiers) < len(a[j].Modifiers)
2513
}
2614

2715
//GetModifierGroups groups bindings that have the same modifiers
@@ -30,7 +18,7 @@ func GetModifierGroups(bindings []Binding) []ModifierGroup {
3018
for _, binding := range bindings {
3119
match := false
3220
for gKey, group := range groups {
33-
if compareSlices(binding.Modifiers, group.Modifiers) {
21+
if helpers.CompareSlices(binding.Modifiers, group.Modifiers) {
3422
groups[gKey].Bindings = append(groups[gKey].Bindings, binding)
3523
match = true
3624
}
@@ -43,7 +31,7 @@ func GetModifierGroups(bindings []Binding) []ModifierGroup {
4331
})
4432
}
4533
}
46-
sort.Sort(sortByNumBindings(groups))
34+
sort.Sort(sortByNumModifiers(groups))
4735

4836
return groups
4937
}

internal/i3parse/helpers.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)