Skip to content

Commit

Permalink
support completion for arg and option
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Sep 23, 2018
1 parent 0c4463b commit b1216e6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
59 changes: 59 additions & 0 deletions cmd/todoist/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,65 @@ import (
"os"
)

const (
bashCompletionFunc = `
__todoist_select_one() {
fzf
}
__todoist_select_multi() {
fzf -m
}
__todoist_filter_ids() {
COMPREPLY=( $(todoist filter list | __todoist_select_multi | awk '{print $1}' | tr '\n' ' ') )
}
__todoist_item_ids() {
COMPREPLY=( $(todoist item list | __todoist_select_multi | awk '{print $1}' | tr '\n' ' ') )
}
__todoist_label_id() {
COMPREPLY=( $(todoist label list | __todoist_select_one | awk '{print $1}') )
}
__todoist_labels_ids() {
COMPREPLY=( $(todoist label list | __todoist_select_multi | awk '{print $1}' | tr '\n' ' ') )
}
__todoist_project_id() {
COMPREPLY=( $(todoist project list | __todoist_select_one | awk '{print $1}') )
}
__todoist_project_ids() {
COMPREPLY=( $(todoist project list | __todoist_select_multi | awk '{print $1}' | tr '\n' ' ') )
}
__todoist_custom_func() {
case ${last_command} in
todoist_filter_update | todoist_filter_delete)
__todoist_filter_ids
return
;;
todoist_item_update | todoist_item_delete | todoist_item_move | todoist_item_complete | todoist_item_uncomplete)
__todoist_item_ids
return
;;
todoist_label_update | todoist_label_delete)
__todoist_label_ids
return
;;
todoist_project_update | todoist_project_delete | todoist_project_archive | todoist_project_unarchive)
__todoist_project_ids
return
;;
*)
;;
esac
}
`
)

// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion",
Expand Down
4 changes: 4 additions & 0 deletions cmd/todoist/cmd/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,20 @@ func init() {
RootCmd.AddCommand(itemCmd)
itemCmd.AddCommand(itemListCmd)
itemAddCmd.Flags().StringP("project", "p", "inbox", "project id or name")
itemAddCmd.Flag("project").Annotations = map[string][]string{cobra.BashCompCustom: {"__todoist_project_id"}}
itemAddCmd.Flags().StringP("label", "l", "", "label id or name(s) (delimiter: ,)")
itemAddCmd.Flag("label").Annotations = map[string][]string{cobra.BashCompCustom: {"__todoist_label_id"}}
itemAddCmd.Flags().StringP("due", "d", "", "due date")
itemAddCmd.Flags().Int("priority", 1, "priority")
itemCmd.AddCommand(itemAddCmd)
itemUpdateCmd.Flags().StringP("label", "l", "", "label id(s) or name(s) (delimiter: ,)")
itemUpdateCmd.Flag("label").Annotations = map[string][]string{cobra.BashCompCustom: {"__todoist_label_id"}}
itemUpdateCmd.Flags().StringP("due", "d", "", "due date")
itemUpdateCmd.Flags().Int("priority", 1, "priority")
itemCmd.AddCommand(itemUpdateCmd)
itemCmd.AddCommand(itemDeleteCmd)
itemMoveCmd.Flags().StringP("project", "p", "", "project")
itemMoveCmd.Flag("project").Annotations = map[string][]string{cobra.BashCompCustom: {"__todoist_project_id"}}
itemCmd.AddCommand(itemMoveCmd)
itemCmd.AddCommand(itemCompleteCmd)
itemCmd.AddCommand(itemUncompleteCmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/todoist/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var cfgFile string
var RootCmd = &cobra.Command{
Use: "todoist",
Short: "Command line tool for todoist.",
BashCompletionFunction: bashCompletionFunc,
}

// Execute adds all child commands to the root command sets flags appropriately.
Expand Down

0 comments on commit b1216e6

Please sign in to comment.