Skip to content

Commit

Permalink
feat: support completion for multiple services
Browse files Browse the repository at this point in the history
  • Loading branch information
xx4h committed Oct 13, 2024
1 parent 76bf311 commit 169ec0c
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func noMoreArgsComp() ([]string, cobra.ShellCompDirective) {
}

// support function for completion
func compListStates(_ string, ignoredStates []string, service string, state string, h *pkg.Hctl) ([]string, cobra.ShellCompDirective) {
func compListStates(_ string, ignoredStates []string, serviceCaps []string, state string, h *pkg.Hctl) ([]string, cobra.ShellCompDirective) {
states, err := h.GetStates()
if err != nil {
log.Debug().Caller().Msgf("Error: %+v", err)
Expand All @@ -127,7 +127,7 @@ func compListStates(_ string, ignoredStates []string, service string, state stri
}

filteredStates := filterStates(states, ignoredStates)
filteredStates = filterCapable(filteredStates, services, service, state)
filteredStates = filterCapable(filteredStates, services, serviceCaps, state)

var choices []string
for _, rel := range filteredStates {
Expand Down
5 changes: 3 additions & 2 deletions cmd/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cmd

import (
"slices"
"strings"

"github.com/xx4h/hctl/pkg/rest"
Expand Down Expand Up @@ -43,7 +44,7 @@ func filterStates(states []rest.HassState, ignoredStates []string) []rest.HassSt
}

// Filter states with given service capability and state
func filterCapable(states []rest.HassState, services []rest.HassService, service string, state string) []rest.HassState {
func filterCapable(states []rest.HassState, services []rest.HassService, serviceCaps []string, state string) []rest.HassState {
// get all service domains that have "turn_on" as domain service
// split state.EntryId domain=[0] entity=[1]
// create list of states that are in a domain having "turn_on" as domain service
Expand All @@ -52,7 +53,7 @@ func filterCapable(states []rest.HassState, services []rest.HassService, service
var filteredStates []rest.HassState
for _, rel := range services {
for name := range rel.Services {
if name == service {
if slices.Contains(serviceCaps, name) {
capableServices = append(capableServices, rel)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/off.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func newOffCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
if len(args) != 0 {
return noMoreArgsComp()
}
return compListStates(toComplete, args, "turn_off", "on", h)
return compListStates(toComplete, args, []string{"turn_off"}, "on", h)
},
Run: func(_ *cobra.Command, args []string) {
c := h.GetRest()
Expand Down
2 changes: 1 addition & 1 deletion cmd/on.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func newOnCmd(h *pkg.Hctl) *cobra.Command {
if len(args) != 0 {
return noMoreArgsComp()
}
return compListStates(toComplete, args, "turn_on", "off", h)
return compListStates(toComplete, args, []string{"turn_on"}, "off", h)
},
Run: func(_ *cobra.Command, args []string) {
c := h.GetRest()
Expand Down
2 changes: 1 addition & 1 deletion cmd/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newPlayCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveDefault
}
return compListStates(toComplete, args, "play_media", "", h)
return compListStates(toComplete, args, []string{"play_media"}, "", h)
},
Run: func(_ *cobra.Command, args []string) {
h.PlayMusic(args[0], args[1])
Expand Down
2 changes: 1 addition & 1 deletion cmd/toggle.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newToggleCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
if len(args) != 0 {
return noMoreArgsComp()
}
return compListStates(toComplete, args, "toggle", "", h)
return compListStates(toComplete, args, []string{"toggle"}, "", h)
},
Run: func(_ *cobra.Command, args []string) {
c := h.GetRest()
Expand Down
2 changes: 1 addition & 1 deletion cmd/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newVolumeCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
Args: cobra.MatchAll(cobra.ExactArgs(2)),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return compListStates(toComplete, args, "volume_set", "", h)
return compListStates(toComplete, args, []string{"volume_set"}, "", h)
} else if len(args) == 1 {
return volRange, cobra.ShellCompDirectiveNoFileComp
}
Expand Down

0 comments on commit 169ec0c

Please sign in to comment.