Skip to content

Commit

Permalink
Merge pull request #2 from glvr182/feature/mustparsers
Browse files Browse the repository at this point in the history
Parser: Added parse functions
Example: Added examples
  • Loading branch information
glvr182 authored Jul 14, 2019
2 parents b11f46a + 8d0cf6b commit 0835533
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Binary file added examples/examples
Binary file not shown.
21 changes: 21 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/awesome-gocui/keybinding"
)

// show will call the ParseAll function
func show(keyStr string) {
keys, err := keybinding.ParseAll(keyStr)
if err != nil {
Expand All @@ -15,9 +16,29 @@ func show(keyStr string) {
}
}

// must will call the MustParseAll function
func must(keyStr string) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Error caught: ", r)
}
}()

keys := keybinding.MustParseAll(keyStr)
fmt.Println("Key: ", keyStr, "=", keys)
}

func main() {
fmt.Println("The show calls:")
show("ctrl+a")
show("ctrl+b")
show("ctrl+/, tab")
show("ctrl+ alt +/")
show("jibber+ jabber +/") // This will fail
fmt.Println("The must calls:")
must("ctrl+a")
must("ctrl+b")
must("ctrl+/, tab")
must("ctrl+ alt +/")
must("jibber+ jabber +/") // This will fail
}
17 changes: 17 additions & 0 deletions keybinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,23 @@ func ParseAll(input string) ([]Key, error) {
return ret, nil
}


func MustParse(input string) Key {
if key, err := Parse(input); err != nil {
panic(err)
} else {
return key
}
}

func MustParseAll(input string) []Key {
if key, err := ParseAll(input); err != nil {
panic(err)
} else {
return key
}
}

func (key Key) String() string {
displayTokens := make([]string, 0)
prefix := ""
Expand Down

0 comments on commit 0835533

Please sign in to comment.