summaryrefslogtreecommitdiff
path: root/internal/askcli/render_task_list.go
blob: 65cae7175c133e69e82c703f1124c470bdf3f271 (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
32
33
34
35
package askcli

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

type taskListAliasLoader func([]TaskExport) (map[string]string, error)

func renderTaskList(tasks []TaskExport, stdout, stderr io.Writer, jsonOutput bool) (int, error) {
	return renderTaskListWithAliasLoader(tasks, stdout, stderr, jsonOutput, ensureTaskAliases)
}

func renderTaskListWithAliasLoader(tasks []TaskExport, stdout, stderr io.Writer, jsonOutput bool, loadAliases taskListAliasLoader) (int, error) {
	aliases, err := loadAliases(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
}