-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
keybord.go
54 lines (45 loc) · 1014 Bytes
/
keybord.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
package main
import (
"fmt"
"log"
"os/exec"
"regexp"
"strings"
"time"
)
type Keyboard struct {
val string
}
func (k *Keyboard) value() string {
return k.val
}
func keyboard() element {
e := &Keyboard{}
go func() {
for {
if val, err := e.layout(); err == nil {
e.val = val
} else {
log.Printf("could not read keyboard layout: %v", err)
}
time.Sleep(time.Second)
}
}()
return e
}
func (k *Keyboard) layout() (string, error) {
data, err := exec.Command("setxkbmap", "-print").Output()
if err != nil {
return "", fmt.Errorf("'setxkbmap -print' command: %s", err)
}
r := regexp.MustCompile(`xkb_symbols[^"]+"([^"]+)`)
m := r.FindStringSubmatch(string(data))
if len(m) != 2 {
return "", fmt.Errorf("could not extract keybord layout from %s", string(data))
}
parts := strings.Split(m[1], "+")
if len(parts) < 2 {
return "", fmt.Errorf("expected at least two elements in keyboard details: %s", m[1])
}
return fmt.Sprintf("^fg(white) %s^fg()", parts[1]), nil
}