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	return []string{
 85		"family",
 86		"intimate-friends",
 87		"close-friends",
 88		"casual-friends",
 89		"acquaintances",
 90		"business-contacts",
 91		"almost-strangers",
 92	}, cobra.ShellCompDirectiveNoFileComp
 93}
 94
 95// Workflows returns workflow options for shell completion.
 96func Workflows(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
 97	workflows := lunatask.Workflows()
 98	keys := make([]string, len(workflows))
 99
100	for i, w := range workflows {
101		keys[i] = string(w)
102	}
103
104	return keys, cobra.ShellCompDirectiveNoFileComp
105}