Skip to content

Commit 5c56afb

Browse files
Merge pull request #6 from RasmusLindroth/modes
Added support for modes
2 parents 349b7d1 + d3cc5e9 commit 5c56afb

File tree

10 files changed

+435
-253
lines changed

10 files changed

+435
-253
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ through your configuration file or going down the track of trial and error
77
anymore.
88

99
The program will output one separate keyboard for all of your different modifier
10-
combinations. And the keyboard will look like your keymap. I hope :)
10+
combinations. It will also output each combination for all of your modes.
11+
The keyboard should look like your keymap if everything works as expected :)
1112

1213
You can see the results in three ways. By opening a local web page,
1314
output the keyboards as text in the terminal or generate SVG files.
@@ -85,6 +86,10 @@ feature-rich option. Because here you can see the command bound to each key.
8586

8687
If you have selected to out SVG files. Open them with a program that supports
8788
SVG files. For example your program for viewing images, InkScape or a browser.
89+
Note that each mode will be located in a folder named mode\<int\>-name-of-mode.
90+
Inside that folder you will find a file named `mode-full-name` where you can see
91+
the real name of the mode. Because this program removes some characters and
92+
shortens the name of the folder.
8893

8994
If you have selected text output, you should already have seen the result in
9095
your terminal.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ require (
66
github.com/gorilla/handlers v1.4.0
77
github.com/gorilla/mux v1.7.1
88
go.i3wm.org/i3 v0.0.0-20181105220049-e2468ef5e1cd
9+
golang.org/x/net v0.0.0-20190322120337-addf6b3196f6 // indirect
910
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ github.com/gorilla/mux v1.7.1 h1:Dw4jY2nghMMRsh1ol8dv1axHkDwMQK2DHerMNJsIpJU=
1010
github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
1111
go.i3wm.org/i3 v0.0.0-20181105220049-e2468ef5e1cd h1:PVrHRo3MP4x/+tbG01rmFnp1sJ1GC7laHJ56OedXYp0=
1212
go.i3wm.org/i3 v0.0.0-20181105220049-e2468ef5e1cd/go.mod h1:7w17+r1F28Yxb1pHu8SxARJo+RGvJCAGOEf+GLw+2aQ=
13+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1314
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc h1:ZMCWScCvS2fUVFw8LOpxyUUW5qiviqr4Dg5NdjLeiLU=
1415
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
16+
golang.org/x/net v0.0.0-20190322120337-addf6b3196f6 h1:78jEq2G3J16aXneH23HSnTQQTCwMHoyO8VEiUH+bpPM=
17+
golang.org/x/net v0.0.0-20190322120337-addf6b3196f6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1518
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
1619
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
20+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
21+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
22+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

i3keys.go

Lines changed: 6 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,17 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"flag"
65
"fmt"
7-
"log"
86
"os"
9-
"path/filepath"
107
"strings"
118

12-
"github.com/RasmusLindroth/i3keys/internal/i3parse"
13-
"github.com/RasmusLindroth/i3keys/internal/keyboard"
149
"github.com/RasmusLindroth/i3keys/internal/svg"
10+
"github.com/RasmusLindroth/i3keys/internal/text"
1511
"github.com/RasmusLindroth/i3keys/internal/web"
16-
"github.com/RasmusLindroth/i3keys/internal/xlib"
1712
)
1813

19-
const version string = "0.0.5"
20-
21-
func keysToSymbol(keys []i3parse.Binding) []i3parse.Binding {
22-
for key, item := range keys {
23-
if item.Type == i3parse.CodeBinding {
24-
res, err := i3parse.CodeToSymbol(item)
25-
if err == nil {
26-
keys[key] = res
27-
}
28-
}
29-
}
30-
return keys
31-
}
32-
33-
func webOutput(port string) {
34-
_, keys, err := i3parse.ParseFromRunning()
35-
36-
if err != nil {
37-
log.Fatalln(err)
38-
}
39-
40-
keys = keysToSymbol(keys)
41-
42-
modifiers := xlib.GetModifiers()
43-
groups := i3parse.GetModifierGroups(keys)
44-
45-
var isoKeyboards []keyboard.Keyboard
46-
var ansiKeyboards []keyboard.Keyboard
47-
for _, group := range groups {
48-
kbISO, err := keyboard.MapKeyboard("ISO", group, modifiers)
49-
if err != nil {
50-
log.Fatalln(err)
51-
}
52-
isoKeyboards = append(isoKeyboards, kbISO)
53-
54-
kbANSI, err := keyboard.MapKeyboard("ANSI", group, modifiers)
55-
if err != nil {
56-
log.Fatalln(err)
57-
}
58-
ansiKeyboards = append(ansiKeyboards, kbANSI)
59-
}
60-
61-
ISOkeyboardJSON, _ := json.Marshal(isoKeyboards)
62-
ANSIkeyboardJSON, _ := json.Marshal(ansiKeyboards)
63-
64-
js := fmt.Sprintf("let generatedISO = %s;\nlet generatedANSI = %s;", ISOkeyboardJSON, ANSIkeyboardJSON)
65-
66-
handler := web.New(js)
67-
68-
fmt.Printf("Starting server at http://localhost:%s\nGo there "+
69-
"to see all of your available keys.\n\n", port)
70-
err = handler.Start(port)
71-
if err != nil {
72-
log.Fatalln(err)
73-
}
74-
}
75-
76-
func textOutput(layout string) {
77-
_, keys, err := i3parse.ParseFromRunning()
78-
79-
if err != nil {
80-
log.Fatalln(err)
81-
}
82-
83-
layout = strings.ToUpper(layout)
84-
85-
keys = keysToSymbol(keys)
86-
87-
groups := i3parse.GetModifierGroups(keys)
88-
modifiers := xlib.GetModifiers()
89-
90-
var keyboards []keyboard.Keyboard
91-
for _, group := range groups {
92-
kb, err := keyboard.MapKeyboard(layout, group, modifiers)
93-
if err == nil {
94-
keyboards = append(keyboards, kb)
95-
}
96-
}
97-
98-
fmt.Printf("Available keybindings per modifier group\n\n")
99-
for kbIndex, kb := range keyboards {
100-
dots := "-"
101-
for i := 0; i < len(kb.Name); i++ {
102-
dots = dots + "-"
103-
}
104-
105-
fmt.Printf("%s:\n%s\n", kb.Name, dots)
106-
107-
for _, keyRow := range kb.Keys {
108-
var unused []string
109-
for _, key := range keyRow {
110-
if key.InUse == false {
111-
unused = append(unused, key.Symbol)
112-
}
113-
}
114-
unusedStr := strings.Join(unused, ", ")
115-
fmt.Printf("%s\n", unusedStr)
116-
}
117-
if kbIndex+1 < len(groups) {
118-
fmt.Printf("\n\n")
119-
}
120-
}
121-
}
122-
123-
func svgOutput(layout string, dest string) {
124-
_, keys, err := i3parse.ParseFromRunning()
125-
126-
if err != nil {
127-
log.Fatalln(err)
128-
}
129-
130-
keys = keysToSymbol(keys)
131-
132-
layout = strings.ToUpper(layout)
133-
if dest == "" {
134-
dest = filepath.Join("./")
135-
}
136-
137-
groups := i3parse.GetModifierGroups(keys)
138-
modifiers := xlib.GetModifiers()
139-
140-
for _, group := range groups {
141-
kb, err := keyboard.MapKeyboard(layout, group, modifiers)
142-
143-
if err != nil {
144-
log.Fatalln(err)
145-
}
146-
147-
data := svg.Generate(layout, kb)
148-
149-
fname := "no-modifiers"
150-
if len(group.Modifiers) > 0 {
151-
fname = strings.Join(group.Modifiers, "-")
152-
}
153-
fname = fname + "-" + layout + ".svg"
154-
155-
file, err := os.Create(filepath.Join(dest, fname))
156-
157-
if err != nil {
158-
log.Fatalln(err)
159-
}
160-
161-
file.Write(data)
162-
}
163-
}
14+
const version string = "0.0.6"
16415

16516
func helpText(exitCode int) {
16617
fmt.Printf("Usage:\n\n\ti3keys <command> [arguments]\n\n")
@@ -202,14 +53,14 @@ func main() {
20253

20354
switch cmd {
20455
case "web":
205-
webOutput(os.Args[2])
56+
web.Output(os.Args[2])
20657
case "text":
207-
textOutput(os.Args[2])
58+
text.Output(os.Args[2])
20859
case "svg":
20960
if len(os.Args) < 4 {
210-
svgOutput(os.Args[2], "")
61+
svg.Output(os.Args[2], "")
21162
} else {
212-
svgOutput(os.Args[2], os.Args[3])
63+
svg.Output(os.Args[2], os.Args[3])
21364
}
21465
case "version":
21566
fmt.Printf("i3keys version %s by Rasmus Lindroth\n", version)

internal/i3parse/helpers.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
package i3parse
3+
4+
//KeysToSymbol converts code bindings to symbol bindings
5+
func KeysToSymbol(keys []Binding) []Binding {
6+
for key, item := range keys {
7+
if item.Type == CodeBinding {
8+
res, err := CodeToSymbol(item)
9+
if err == nil {
10+
keys[key] = res
11+
}
12+
}
13+
}
14+
return keys
15+
}

internal/svg/output.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package svg
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
"unicode"
10+
11+
"github.com/RasmusLindroth/i3keys/internal/i3parse"
12+
"github.com/RasmusLindroth/i3keys/internal/keyboard"
13+
"github.com/RasmusLindroth/i3keys/internal/xlib"
14+
)
15+
16+
func sanitizeDirName(s string) string {
17+
r := ""
18+
prevChar := '-'
19+
for _, c := range s {
20+
switch {
21+
case unicode.IsSpace(c) || unicode.IsPunct(c):
22+
if prevChar != '-' {
23+
r += "-"
24+
prevChar = '-'
25+
}
26+
case unicode.IsDigit(c) || unicode.IsLetter(c):
27+
r += string(c)
28+
prevChar = c
29+
}
30+
}
31+
r = filepath.Base(filepath.Clean(r))
32+
return r
33+
}
34+
func createGroup(layout string, dest string, group i3parse.ModifierGroup, modifiers map[string][]string) {
35+
kb, err := keyboard.MapKeyboard(layout, group, modifiers)
36+
37+
if err != nil {
38+
log.Fatalln(err)
39+
}
40+
41+
data := Generate(layout, kb)
42+
43+
fname := "no-modifiers"
44+
if len(group.Modifiers) > 0 {
45+
fname = strings.Join(group.Modifiers, "-")
46+
}
47+
fname = fname + "-" + layout + ".svg"
48+
49+
file, err := os.Create(filepath.Join(dest, fname))
50+
defer file.Close()
51+
52+
if err != nil {
53+
log.Fatalln(err)
54+
}
55+
56+
file.Write(data)
57+
}
58+
59+
//Output generates svg-files of the keyboards at the desired location
60+
func Output(layout string, dest string) {
61+
modes, keys, err := i3parse.ParseFromRunning()
62+
63+
if err != nil {
64+
log.Fatalln(err)
65+
}
66+
67+
keys = i3parse.KeysToSymbol(keys)
68+
69+
layout = strings.ToUpper(layout)
70+
if dest == "" {
71+
dest = filepath.Join("./")
72+
}
73+
74+
groups := i3parse.GetModifierGroups(keys)
75+
modifiers := xlib.GetModifiers()
76+
77+
err = os.MkdirAll(dest, os.ModePerm)
78+
if err != nil {
79+
log.Fatalln(err)
80+
}
81+
82+
for _, group := range groups {
83+
createGroup(layout, dest, group, modifiers)
84+
}
85+
86+
for i, mode := range modes {
87+
keys := i3parse.KeysToSymbol(mode.Bindings)
88+
groups := i3parse.GetModifierGroups(keys)
89+
90+
modeDir := fmt.Sprintf("mode%d-", i)
91+
modeDir += sanitizeDirName(mode.Name)
92+
93+
if len(modeDir) > 50 {
94+
modeDir = modeDir[0:50]
95+
}
96+
97+
mDest := filepath.Join(dest, modeDir)
98+
err = os.MkdirAll(mDest, os.ModePerm)
99+
if err != nil {
100+
log.Fatalln(err)
101+
}
102+
103+
file, err := os.Create(filepath.Join(mDest, "mode-full-name.txt"))
104+
105+
if err != nil {
106+
log.Fatalln(err)
107+
}
108+
109+
file.Write([]byte(mode.Name))
110+
file.Close()
111+
112+
for _, group := range groups {
113+
createGroup(layout, mDest, group, modifiers)
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)