Skip to content

Commit

Permalink
Merge pull request #11 from RasmusLindroth/filter
Browse files Browse the repository at this point in the history
Add filter and TOC
  • Loading branch information
RasmusLindroth authored Jan 19, 2020
2 parents 4f4e39f + 4ae552a commit f63dc0a
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 106 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Rasmus Lindroth
Copyright (c) 2020 Rasmus Lindroth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
47 changes: 34 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@ git clone https://github.com/RasmusLindroth/i3keys.git
//Install
go install
//Run web interface on port 8080
i3keys web 8080
//or output text to the terminal
i3keys text ISO
//or output SVG to the current directory
i3keys svg ISO ./
/
//If starting doesn't work try running it from $HOME/go/bin
```
Expand All @@ -62,16 +55,25 @@ cd $HOME/go/src/github.com/RasmusLindroth/i3keys
//Install
go install
//If starting doesn't work try running it from $HOME/go/bin
```

Example usage
```
//Run web interface on port 8080
i3keys web 8080
//or output text to the terminal
i3keys text ISO
//or filter text output
i3keys text ISO Mod4+Ctrl
//or output SVG to the current directory
i3keys svg ISO ./
//If starting doesn't work try running it from $HOME/go/bin
//or filter SVG
i3keys svg ISO ./ Mod4+Ctrl
```

If you still having problems see the
Expand Down Expand Up @@ -130,10 +132,29 @@ Usage:
The commands are:
web <port> start the web ui and listen on <port>
text <layout> output available keybindings in the terminal. <layout> can be ISO or ANSI
svg <layout> [dest] outputs one SVG file for each modifier group. <layout> can be ISO or ANSI, [dest] defaults to current directory
version print i3keys version
web <port>
start the web ui and listen on <port>
text <layout> [mods]
output available keybindings in the terminal
svg <layout> [dest] [mods]
outputs one SVG file for each modifier group
version
print i3keys version
Arguments:
<layout>
is required. Can be ISO or ANSI
[mods]
is optional. Can be a single modifier or a group of modifiers. Group them with a plus sign, e.g. Mod4+Ctrl
[dest]
is optional. Where to output files, defaults to the current directory
```

### Disclaimer
Expand Down
26 changes: 18 additions & 8 deletions i3keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ import (
"github.com/RasmusLindroth/i3keys/internal/web"
)

const version string = "0.0.7"
const version string = "0.0.8"

func helpText(exitCode int) {
fmt.Printf("Usage:\n\n\ti3keys <command> [arguments]\n\n")
fmt.Printf("The commands are:\n\n")
fmt.Println("\tweb <port> start the web ui and listen on <port>")
fmt.Println("\ttext <layout> output available keybindings in the terminal. <layout> can be ISO or ANSI")
fmt.Println("\tsvg <layout> [dest] outputs one SVG file for each modifier group. <layout> can be ISO or ANSI, [dest] defaults to current directory")
fmt.Println("\tversion print i3keys version")
fmt.Print("\tweb <port>\n\t\tstart the web ui and listen on <port>\n\n")
fmt.Print("\ttext <layout> [mods]\n\t\toutput available keybindings in the terminal\n\n")
fmt.Print("\tsvg <layout> [dest] [mods]\n\t\toutputs one SVG file for each modifier group\n\n")
fmt.Print("\tversion\n\t\tprint i3keys version\n\n")
fmt.Printf("Arguments:\n\n")
fmt.Print("\t<layout>\n\t\tis required. Can be ISO or ANSI\n\n")
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")
fmt.Print("\t[dest]\n\t\tis optional. Where to output files, defaults to the current directory\n\n")
os.Exit(exitCode)
}

Expand Down Expand Up @@ -55,12 +59,18 @@ func main() {
case "web":
web.Output(os.Args[2])
case "text":
text.Output(os.Args[2])
if len(os.Args) < 4 {
text.Output(os.Args[2], "")
} else {
text.Output(os.Args[2], os.Args[3])
}
case "svg":
if len(os.Args) < 4 {
svg.Output(os.Args[2], "")
svg.Output(os.Args[2], "", "")
} else if len(os.Args) < 5 {
svg.Output(os.Args[2], os.Args[3], "")
} else {
svg.Output(os.Args[2], os.Args[3])
svg.Output(os.Args[2], os.Args[3], os.Args[4])
}
case "version":
fmt.Printf("i3keys version %s by Rasmus Lindroth\n", version)
Expand Down
35 changes: 35 additions & 0 deletions internal/helpers/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helpers

import (
"sort"
"strings"
"unicode"
)
Expand Down Expand Up @@ -40,3 +41,37 @@ func SplitBySpace(s string) []string {

return parts
}

//HandleFilterArgs sorts and splits strings like Mod4+Shift
func HandleFilterArgs(s string) []string {
var a []string
var b []string

parts := strings.Split(s, "+")
for _, item := range parts {
p := strings.Title(item)
if len(p) > 2 && p[:3] == "Mod" {
a = append(a, p)
continue
}
b = append(b, p)
}
sort.Strings(a)
sort.Strings(b)
return append(a, b...)
}

//CompareSlices compares if two slices are equal
func CompareSlices(a []string, b []string) bool {
if len(a) != len(b) {
return false
}

for i, val := range a {
if val != b[i] {
return false
}
}

return true
}
18 changes: 8 additions & 10 deletions internal/i3parse/codes.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package i3parse

import (
"errors"
"fmt"
"strconv"

"github.com/RasmusLindroth/i3keys/internal/xlib"
)

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

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

hex := xlib.KeyCodeToHex(i)
if name, ok := xlib.KeySyms[hex]; ok {
code.Key = name
code.Type = SymbolBinding

return code, nil
name, ok := xlib.KeySyms[hex]
if !ok {
return "", fmt.Errorf("Keycode %s not in keysymdef.h", key)
}

return Binding{}, errors.New("Keycode not in keysymdef.h")
return name, nil
}
28 changes: 8 additions & 20 deletions internal/i3parse/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,14 @@ package i3parse

import "sort"

func compareSlices(a []string, b []string) bool {
if len(a) != len(b) {
return false
}

for i, val := range a {
if val != b[i] {
return false
}
}

return true
}
import "github.com/RasmusLindroth/i3keys/internal/helpers"

type sortByNumBindings []ModifierGroup
type sortByNumModifiers []ModifierGroup

func (a sortByNumBindings) Len() int { return len(a) }
func (a sortByNumBindings) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortByNumBindings) Less(i, j int) bool {
return len(a[i].Bindings) > len(a[j].Bindings)
func (a sortByNumModifiers) Len() int { return len(a) }
func (a sortByNumModifiers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortByNumModifiers) Less(i, j int) bool {
return len(a[i].Modifiers) < len(a[j].Modifiers)
}

//GetModifierGroups groups bindings that have the same modifiers
Expand All @@ -30,7 +18,7 @@ func GetModifierGroups(bindings []Binding) []ModifierGroup {
for _, binding := range bindings {
match := false
for gKey, group := range groups {
if compareSlices(binding.Modifiers, group.Modifiers) {
if helpers.CompareSlices(binding.Modifiers, group.Modifiers) {
groups[gKey].Bindings = append(groups[gKey].Bindings, binding)
match = true
}
Expand All @@ -43,7 +31,7 @@ func GetModifierGroups(bindings []Binding) []ModifierGroup {
})
}
}
sort.Sort(sortByNumBindings(groups))
sort.Sort(sortByNumModifiers(groups))

return groups
}
15 changes: 0 additions & 15 deletions internal/i3parse/helpers.go

This file was deleted.

Loading

0 comments on commit f63dc0a

Please sign in to comment.