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

Support multiple keys for key bindings #505

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 24 additions & 3 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var validate *validator.Validate

type ViewType string

type KeyList []string

const (
PRsView ViewType = "prs"
IssuesView ViewType = "issues"
Expand Down Expand Up @@ -111,7 +113,7 @@ type RepoConfig struct {
}

type Keybinding struct {
Key string `yaml:"key"`
Key KeyList `yaml:"key"`
Command string `yaml:"command"`
Builtin string `yaml:"builtin"`
}
Expand All @@ -123,8 +125,8 @@ func (kb Keybinding) NewBinding(previous *key.Binding) key.Binding {
}

return key.NewBinding(
key.WithKeys(kb.Key),
key.WithHelp(kb.Key, helpDesc),
key.WithKeys(kb.Key...),
key.WithHelp(strings.Join(kb.Key, "/"), helpDesc),
)
}

Expand Down Expand Up @@ -486,3 +488,22 @@ func ParseConfig(path string, repoPath *string) (Config, error) {

return config, nil
}

func (kl *KeyList) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Try unmarshaling into a []string first
var list []string
if err := unmarshal(&list); err == nil {
*kl = list
return nil
}

// If that fails, try unmarshaling into a single string
var single string
if err := unmarshal(&single); err == nil {
*kl = []string{single}
return nil
}

// If both attempts fail, return an error
return fmt.Errorf("key must be either a string or a list of strings")
}