Skip to content

Commit

Permalink
feat(custom command)!: support multiple contexts within one command
Browse files Browse the repository at this point in the history
  • Loading branch information
yam-liu committed Jul 28, 2024
1 parent f598da0 commit c5d846b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pkg/gui/services/custom_commands/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func (self *Client) GetCustomCommandKeybindings() ([]*types.Binding, error) {
bindings := []*types.Binding{}
for _, customCommand := range self.customCommands {
handler := self.handlerCreator.call(customCommand)
binding, err := self.keybindingCreator.call(customCommand, handler)
compoundBindings, err := self.keybindingCreator.call(customCommand, handler)
if err != nil {
return nil, err
}
bindings = append(bindings, binding)
bindings = append(bindings, compoundBindings...)
}

return bindings, nil
Expand Down
45 changes: 29 additions & 16 deletions pkg/gui/services/custom_commands/keybinding_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func NewKeybindingCreator(c *helpers.HelperCommon) *KeybindingCreator {
}
}

func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler func() error) (*types.Binding, error) {
func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler func() error) ([]*types.Binding, error) {
if customCommand.Context == "" {
return nil, formatContextNotProvidedError(customCommand)
}

viewName, err := self.getViewNameAndContexts(customCommand)
viewNames, err := self.getViewNamesAndContexts(customCommand)
if err != nil {
return nil, err
}
Expand All @@ -39,27 +39,40 @@ func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler
description = customCommand.Command
}

return &types.Binding{
ViewName: viewName,
Key: keybindings.GetKey(customCommand.Key),
Modifier: gocui.ModNone,
Handler: handler,
Description: description,
}, nil
bindings := []*types.Binding{}
for _, viewName := range viewNames {
bindings = append(bindings, &types.Binding{
ViewName: viewName,
Key: keybindings.GetKey(customCommand.Key),
Modifier: gocui.ModNone,
Handler: handler,
Description: description,
})
}
return bindings, nil
}

func (self *KeybindingCreator) getViewNameAndContexts(customCommand config.CustomCommand) (string, error) {
func (self *KeybindingCreator) getViewNamesAndContexts(customCommand config.CustomCommand) ([]string, error) {
if customCommand.Context == "global" {
return "", nil
return []string{}, nil
}

ctx, ok := self.contextForContextKey(types.ContextKey(customCommand.Context))
if !ok {
return "", formatUnknownContextError(customCommand)
contexts := strings.Split(customCommand.Context, ",")
contexts = lo.Map(contexts, func(context string, _ int) string {
return strings.TrimSpace(context)
})

viewNames := []string{}
for _, context := range contexts {
ctx, ok := self.contextForContextKey(types.ContextKey(context))
if !ok {
return []string{}, formatUnknownContextError(customCommand)
}

viewNames = append(viewNames, ctx.GetViewName())
}

viewName := ctx.GetViewName()
return viewName, nil
return viewNames, nil
}

func (self *KeybindingCreator) contextForContextKey(contextKey types.ContextKey) (types.Context, bool) {
Expand Down

0 comments on commit c5d846b

Please sign in to comment.