Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser: Added parse functions #2

Merged
merged 2 commits into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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