Skip to content

Commit

Permalink
Merge pull request #17 from RasmusLindroth/auto
Browse files Browse the repository at this point in the history
autodetect wm
  • Loading branch information
RasmusLindroth authored Feb 24, 2020
2 parents 42c5a0c + a2039a7 commit 52021c7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 28 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# i3keys
A program for the tiling window manager [i3](https://i3wm.org/).
It also supports [Sway](https://swaywm.org/) but then you need to use the flag `-s`.
It also supports [Sway](https://swaywm.org/).

This is program lists all the keys that are bound to some action in i3 or sway, and
all keys that are not bound to any actions. Now you don't have to search
Expand Down Expand Up @@ -65,10 +65,6 @@ i3keys web
//Run web interface on port 8080
i3keys web 8080
//sway usage
i3keys -s web
i3keys -s web 8080
//or output text to the terminal
i3keys text ISO
Expand All @@ -86,6 +82,12 @@ If you still having problems see the
[installation guide for Go](https://golang.org/doc/install#install) or open
up an issue.

### You don't want autodetection of WM

You can use the following flags to disable autodetection, `-i` for i3 and `-s` for Sway.
This can be useful if you're running both at the same time, or if the
autodetection doesn't work. But then you should file an issue.

#### You have started i3keys

For web output you will need to start your browser and head over to the URL
Expand Down Expand Up @@ -134,8 +136,8 @@ Control_L, Super_L, Alt_L, ISO_Level3_Shift, Super_R, Menu, Control_R, KP_Insert
```
Usage:
i3keys [-s] <command> [arguments]
Add the flag -s for sway
i3keys [-i|-s] <command> [arguments]
Add the flag -i for i3 and -s for Sway if you don't want autodetection
The commands are:
Expand Down
28 changes: 16 additions & 12 deletions i3keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/RasmusLindroth/i3keys/internal/web"
)

const version string = "0.0.11"
const version string = "0.0.12"

func helpText(exitCode int) {
fmt.Print("Usage:\n\n\ti3keys [-s] <command> [arguments]\n")
fmt.Print("\tAdd the flag -s for sway\n\n")
fmt.Print("Usage:\n\n\ti3keys [-i|-s] <command> [arguments]\n")
fmt.Print("\tAdd the flag -i for i3 and -s for Sway if you don't want autodetection\n\n")
fmt.Print("The commands are:\n\n")
fmt.Print("\tweb [port]\n\t\tstart the web ui and listen on random port or [port]\n\n")
fmt.Print("\ttext <layout> [mods]\n\t\toutput available keybindings in the terminal\n\n")
Expand All @@ -33,9 +33,13 @@ func main() {
}

sIndex := 1
sway := false
if os.Args[1] == "-s" {
sway = true
wm := ""
switch os.Args[1] {
case "-i":
wm = "i3"
sIndex = 2
case "-s":
wm = "sway"
sIndex = 2
}
cmd := os.Args[sIndex]
Expand All @@ -60,20 +64,20 @@ func main() {

switch cmd {
case "web":
web.Output(sway, port)
web.Output(wm, port)
case "text":
if len(os.Args) < 3+sIndex {
text.Output(sway, os.Args[1+sIndex], "")
text.Output(wm, os.Args[1+sIndex], "")
} else {
text.Output(sway, os.Args[1+sIndex], os.Args[2+sIndex])
text.Output(wm, os.Args[1+sIndex], os.Args[2+sIndex])
}
case "svg":
if len(os.Args) < 3+sIndex {
svg.Output(sway, os.Args[1+sIndex], "", "")
svg.Output(wm, os.Args[1+sIndex], "", "")
} else if len(os.Args) < 4+sIndex {
svg.Output(sway, os.Args[1+sIndex], os.Args[2+sIndex], "")
svg.Output(wm, os.Args[1+sIndex], os.Args[2+sIndex], "")
} else {
svg.Output(sway, os.Args[1+sIndex], os.Args[2+sIndex], os.Args[3+sIndex])
svg.Output(wm, os.Args[1+sIndex], os.Args[2+sIndex], os.Args[3+sIndex])
}
case "version":
fmt.Printf("i3keys version %s by Rasmus Lindroth\n", version)
Expand Down
38 changes: 38 additions & 0 deletions internal/i3parse/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package i3parse

import (
"errors"
"fmt"
"io/ioutil"
"log"
Expand All @@ -11,6 +12,43 @@ import (
"go.i3wm.org/i3/v4"
)

type wmType uint

const (
wmNil wmType = iota
wmi3
wmSway
)

func testWM(program string) bool {
_, err := exec.Command(program, "--get-socketpath").Output()
return err == nil
}

func getWM() wmType {
if testWM("i3") {
return wmi3
}
if testWM("sway") {
return wmSway
}
return wmNil
}

func getAutoWM() (*strings.Reader, error) {
wm := getWM()
switch wm {
case wmi3:
return getConfigFromRunningi3()
case wmSway:
return getConfigFromRunningSway()
default:
err := errors.New("couldn't determine if you're running i3 or Sway. Test the -i or -s flag")
return nil, err
}

}

//getConfigFromRunningi3 returns the i3 config file
func getConfigFromRunningi3() (*strings.Reader, error) {
conf, err := i3.GetConfig()
Expand Down
10 changes: 7 additions & 3 deletions internal/i3parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ type Group string
*/

//ParseFromRunning loads config from the running i3 instance
func ParseFromRunning(sway bool) ([]Mode, []Binding, error) {
if sway {
func ParseFromRunning(wm string) ([]Mode, []Binding, error) {
switch wm {
case "i3":
return parse(getConfigFromRunningi3())
case "sway":
return parse(getConfigFromRunningSway())
default:
return parse(getAutoWM())
}
return parse(getConfigFromRunningi3())
}

//ParseFromFile loads config from path
Expand Down
4 changes: 2 additions & 2 deletions internal/svg/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func createGroup(layout string, dest string, group i3parse.ModifierGroup, modifi
}

//Output generates svg-files of the keyboards at the desired location
func Output(sway bool, layout string, dest string, filter string) {
modes, keys, err := i3parse.ParseFromRunning(sway)
func Output(wm string, layout string, dest string, filter string) {
modes, keys, err := i3parse.ParseFromRunning(wm)

if err != nil {
log.Fatalln(err)
Expand Down
4 changes: 2 additions & 2 deletions internal/text/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func printKeyboards(keyboards []keyboard.Keyboard, groups []i3parse.ModifierGrou
}

//Output prints the keyboards to os.Stdout
func Output(sway bool, layout string, filter string) {
modes, keys, err := i3parse.ParseFromRunning(sway)
func Output(wm string, layout string, filter string) {
modes, keys, err := i3parse.ParseFromRunning(wm)

if err != nil {
log.Fatalln(err)
Expand Down
4 changes: 2 additions & 2 deletions internal/web/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type modeKeyboards struct {
}

//Output starts the server at desired port
func Output(sway bool, port string) {
modes, keys, err := i3parse.ParseFromRunning(sway)
func Output(wm string, port string) {
modes, keys, err := i3parse.ParseFromRunning(wm)

if err != nil {
log.Fatalln(err)
Expand Down

0 comments on commit 52021c7

Please sign in to comment.