1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5package area
6
7import (
8 "fmt"
9
10 "git.secluded.site/go-lunatask"
11 "git.secluded.site/lune/internal/client"
12 "git.secluded.site/lune/internal/completion"
13 "git.secluded.site/lune/internal/config"
14 "git.secluded.site/lune/internal/stats"
15 "git.secluded.site/lune/internal/ui"
16 "github.com/spf13/cobra"
17)
18
19// ShowCmd displays details for a specific area.
20var ShowCmd = &cobra.Command{
21 Use: "show KEY",
22 Short: "Show area details",
23 Long: `Show detailed information for a configured area.
24
25Displays the area's name, ID, deeplink, goals, and uncompleted task count.`,
26 Args: cobra.ExactArgs(1),
27 ValidArgsFunction: completion.Areas,
28 RunE: runShow,
29}
30
31func runShow(cmd *cobra.Command, args []string) error {
32 areaKey := args[0]
33
34 cfg, err := config.Load()
35 if err != nil {
36 return err
37 }
38
39 area := cfg.AreaByKey(areaKey)
40 if area == nil {
41 return fmt.Errorf("%w: %s", ErrUnknownArea, areaKey)
42 }
43
44 apiClient, err := client.New()
45 if err != nil {
46 return err
47 }
48
49 counter := stats.NewTaskCounter(apiClient)
50
51 return printAreaDetails(cmd, area, counter)
52}
53
54func printAreaDetails(cmd *cobra.Command, area *config.Area, counter *stats.TaskCounter) error {
55 link, _ := lunatask.BuildDeepLink(lunatask.ResourceArea, area.ID)
56
57 fmt.Fprintf(cmd.OutOrStdout(), "%s (%s)\n", ui.H1.Render(area.Name), area.Key)
58 fmt.Fprintf(cmd.OutOrStdout(), " ID: %s\n", area.ID)
59 fmt.Fprintf(cmd.OutOrStdout(), " Link: %s\n", link)
60
61 taskCount, err := counter.UncompletedInArea(cmd.Context(), area.ID)
62 if err != nil {
63 fmt.Fprintf(cmd.OutOrStdout(), " Tasks: %s\n", ui.Warning.Render("unable to fetch"))
64 } else {
65 fmt.Fprintf(cmd.OutOrStdout(), " Tasks: %d uncompleted\n", taskCount)
66 }
67
68 if len(area.Goals) == 0 {
69 fmt.Fprintln(cmd.OutOrStdout(), "\n No goals configured")
70
71 return nil
72 }
73
74 fmt.Fprintln(cmd.OutOrStdout())
75
76 for _, goal := range area.Goals {
77 if err := printGoalSummary(cmd, &goal, counter); err != nil {
78 return err
79 }
80 }
81
82 return nil
83}
84
85func printGoalSummary(cmd *cobra.Command, goal *config.Goal, counter *stats.TaskCounter) error {
86 goalLink, _ := lunatask.BuildDeepLink(lunatask.ResourceGoal, goal.ID)
87
88 fmt.Fprintf(cmd.OutOrStdout(), " %s (%s)\n", ui.H2.Render(goal.Name), goal.Key)
89 fmt.Fprintf(cmd.OutOrStdout(), " ID: %s\n", goal.ID)
90 fmt.Fprintf(cmd.OutOrStdout(), " Link: %s\n", goalLink)
91
92 taskCount, err := counter.UncompletedInGoal(cmd.Context(), goal.ID)
93 if err != nil {
94 fmt.Fprintf(cmd.OutOrStdout(), " Tasks: %s\n", ui.Warning.Render("unable to fetch"))
95 } else {
96 fmt.Fprintf(cmd.OutOrStdout(), " Tasks: %d uncompleted\n", taskCount)
97 }
98
99 return nil
100}