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/lune/internal/config"
10	"github.com/spf13/cobra"
11)
12
13// Areas returns area keys from config for shell completion.
14func Areas(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
15	cfg, err := config.Load()
16	if err != nil {
17		return nil, cobra.ShellCompDirectiveError
18	}
19
20	keys := make([]string, len(cfg.Areas))
21	for i, a := range cfg.Areas {
22		keys[i] = a.Key
23	}
24
25	return keys, cobra.ShellCompDirectiveNoFileComp
26}
27
28// Goals returns all goal keys across all areas for shell completion.
29func Goals(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
30	cfg, err := config.Load()
31	if err != nil {
32		return nil, cobra.ShellCompDirectiveError
33	}
34
35	var keys []string
36
37	for _, a := range cfg.Areas {
38		for _, g := range a.Goals {
39			keys = append(keys, g.Key)
40		}
41	}
42
43	return keys, cobra.ShellCompDirectiveNoFileComp
44}
45
46// Notebooks returns notebook keys from config for shell completion.
47func Notebooks(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
48	cfg, err := config.Load()
49	if err != nil {
50		return nil, cobra.ShellCompDirectiveError
51	}
52
53	keys := make([]string, len(cfg.Notebooks))
54	for i, n := range cfg.Notebooks {
55		keys[i] = n.Key
56	}
57
58	return keys, cobra.ShellCompDirectiveNoFileComp
59}
60
61// Habits returns habit keys from config for shell completion.
62func Habits(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
63	cfg, err := config.Load()
64	if err != nil {
65		return nil, cobra.ShellCompDirectiveError
66	}
67
68	keys := make([]string, len(cfg.Habits))
69	for i, h := range cfg.Habits {
70		keys[i] = h.Key
71	}
72
73	return keys, cobra.ShellCompDirectiveNoFileComp
74}
75
76// Relationships returns relationship strength options for shell completion.
77func Relationships(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
78	return []string{
79		"family",
80		"intimate-friends",
81		"close-friends",
82		"casual-friends",
83		"acquaintances",
84		"business-contacts",
85		"almost-strangers",
86	}, cobra.ShellCompDirectiveNoFileComp
87}