summaryrefslogtreecommitdiff
path: root/internal/askcli/render_task_list.go
blob: 177bd818e9c4a226acd21befc8ca673117a2c2da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package askcli

import (
	"encoding/json"
	"fmt"
	"io"
)

var taskListAliasLoader = ensureTaskAliases

func renderTaskList(tasks []TaskExport, stdout, stderr io.Writer, jsonOutput bool) (int, error) {
	aliases, err := taskListAliasLoader(tasks)
	if err != nil {
		fmt.Fprintf(stderr, "error: failed to load task aliases: %v\n", err)
		return 1, nil
	}

	if jsonOutput {
		data, err := json.Marshal(withTaskIDs(tasks, aliases))
		if err != nil {
			fmt.Fprintf(stderr, "error: failed to marshal JSON: %v\n", err)
			return 1, nil
		}
		_, _ = stdout.Write(data)
		_, _ = io.WriteString(stdout, "\n")
		return 0, nil
	}

	_, _ = io.WriteString(stdout, FormatTaskListForWidth(tasks, aliases, detectTaskListTerminalWidth(stdout)))
	return 0, nil
}