completeSessionIDs offers the persisted session ids for the current repo (or --repo, when already typed) as shell completions, newest first, with a short summary as the completion description. It completes one positional; a command with two session ids (compare) resets args per positional.
(cmd *cobra.Command, args []string, toComplete string)
| 99 | // short summary as the completion description. It completes one positional; |
| 100 | // a command with two session ids (compare) resets args per positional. |
| 101 | func completeSessionIDs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 102 | if len(args) != 0 { |
| 103 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 104 | } |
| 105 | repo, _ := cmd.Flags().GetString("repo") |
| 106 | resolvedRepo, err := resolveWorkingDirForSession(repo) |
| 107 | if err != nil { |
| 108 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 109 | } |
| 110 | summaries, err := session.ListSessions(resolvedRepo) |
| 111 | if err != nil { |
| 112 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 113 | } |
| 114 | completions := make([]string, 0, len(summaries)) |
| 115 | for _, s := range summaries { |
| 116 | if !strings.HasPrefix(s.SessionID, toComplete) { |
| 117 | continue |
| 118 | } |
| 119 | desc := fmt.Sprintf("%s · %d comments · %s", describeStart(s), s.TotalComments, describeStatus(s)) |
| 120 | completions = append(completions, s.SessionID+"\t"+desc) |
| 121 | } |
| 122 | return completions, cobra.ShellCompDirectiveNoFileComp |
| 123 | } |
| 124 | |
| 125 | func init() { |
| 126 | sessionListCmd.Flags().StringVar(&sessionListRepoDir, "repo", "", "root directory of the git repository (default: current dir)") |