completion.go

  1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
  2//
  3// SPDX-License-Identifier: AGPL-3.0-or-later
  4
  5// Package completion provides shell completion helpers for CLI commands.
  6package completion
  7
  8import (
  9	"git.secluded.site/go-lunatask"
 10	"git.secluded.site/lune/internal/config"
 11	"github.com/spf13/cobra"
 12)
 13
 14// Areas returns area keys from config for shell completion.
 15func Areas(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
 16	cfg, err := config.Load()
 17	if err != nil {
 18		return nil, cobra.ShellCompDirectiveError
 19	}
 20
 21	keys := make([]string, len(cfg.Areas))
 22	for i, a := range cfg.Areas {
 23		keys[i] = a.Key
 24	}
 25
 26	return keys, cobra.ShellCompDirectiveNoFileComp
 27}
 28
 29// Goals returns all goal keys across all areas for shell completion.
 30func Goals(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
 31	cfg, err := config.Load()
 32	if err != nil {
 33		return nil, cobra.ShellCompDirectiveError
 34	}
 35
 36	totalGoals := 0
 37	for _, a := range cfg.Areas {
 38		totalGoals += len(a.Goals)
 39	}
 40
 41	keys := make([]string, 0, totalGoals)
 42
 43	for _, a := range cfg.Areas {
 44		for _, g := range a.Goals {
 45			keys = append(keys, g.Key)
 46		}
 47	}
 48
 49	return keys, cobra.ShellCompDirectiveNoFileComp
 50}
 51
 52// Notebooks returns notebook keys from config for shell completion.
 53func Notebooks(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
 54	cfg, err := config.Load()
 55	if err != nil {
 56		return nil, cobra.ShellCompDirectiveError
 57	}
 58
 59	keys := make([]string, len(cfg.Notebooks))
 60	for i, n := range cfg.Notebooks {
 61		keys[i] = n.Key
 62	}
 63
 64	return keys, cobra.ShellCompDirectiveNoFileComp
 65}
 66
 67// Habits returns habit keys from config for shell completion.
 68func Habits(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
 69	cfg, err := config.Load()
 70	if err != nil {
 71		return nil, cobra.ShellCompDirectiveError
 72	}
 73
 74	keys := make([]string, len(cfg.Habits))
 75	for i, h := range cfg.Habits {
 76		keys[i] = h.Key
 77	}
 78
 79	return keys, cobra.ShellCompDirectiveNoFileComp
 80}
 81
 82// Relationships returns relationship strength options for shell completion.
 83func Relationships(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
 84	rs := lunatask.AllRelationshipStrengths()
 85	keys := make([]string, len(rs))
 86
 87	for i, r := range rs {
 88		keys[i] = string(r)
 89	}
 90
 91	return keys, cobra.ShellCompDirectiveNoFileComp
 92}
 93
 94// Workflows returns workflow options for shell completion.
 95func Workflows(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
 96	workflows := lunatask.Workflows()
 97	keys := make([]string, len(workflows))
 98
 99	for i, w := range workflows {
100		keys[i] = string(w)
101	}
102
103	return keys, cobra.ShellCompDirectiveNoFileComp
104}
105
106// TaskStatuses returns task status options for shell completion.
107func TaskStatuses(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
108	statuses := lunatask.AllTaskStatuses()
109	keys := make([]string, len(statuses))
110
111	for i, s := range statuses {
112		keys[i] = string(s)
113	}
114
115	return keys, cobra.ShellCompDirectiveNoFileComp
116}
117
118// Priorities returns priority options for shell completion.
119func Priorities(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
120	priorities := lunatask.AllPriorities()
121	keys := make([]string, len(priorities))
122
123	for i, p := range priorities {
124		keys[i] = p.String()
125	}
126
127	return keys, cobra.ShellCompDirectiveNoFileComp
128}
129
130// Motivations returns motivation options for shell completion.
131func Motivations(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
132	motivations := lunatask.AllMotivations()
133	keys := make([]string, len(motivations))
134
135	for i, m := range motivations {
136		keys[i] = string(m)
137	}
138
139	return keys, cobra.ShellCompDirectiveNoFileComp
140}