forked from jyggen/intro-detection-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
91 lines (72 loc) · 1.79 KB
/
output.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"encoding/csv"
"github.com/mattn/go-colorable"
"github.com/olekukonko/tablewriter"
"os"
"strconv"
)
var formats = []string{"csv", "ascii"}
func isValidFormat(format string) bool {
for _, allowed := range formats {
if allowed == format {
return true
}
}
return false
}
func outputAsAscii(results []*Result) error {
var table *tablewriter.Table
if colors {
table = tablewriter.NewWriter(colorable.NewColorableStdout())
} else {
table = tablewriter.NewWriter(colorable.NewNonColorable(os.Stdout))
}
table.SetAutoMergeCellsByColumnIndex([]int{0})
table.SetHeader([]string{"Show", "Season", "Detected", "Comment"})
table.SetRowLine(true)
for _, result := range results {
var color int
if result.DetectedEpisodesCount == 0 {
color = tablewriter.FgRedColor
} else if result.DetectedEpisodesCount == result.TotalEpisodesCount {
color = tablewriter.FgGreenColor
} else {
color = tablewriter.FgYellowColor
}
table.Rich(
[]string{
result.Show.Title(),
strconv.Itoa(result.Season.Number()),
seasonStatusString(result),
missingEpisodeString(result.MissingEpisodesList),
},
[]tablewriter.Colors{{}, {}, {tablewriter.Normal, color}},
)
}
table.Render()
return nil
}
func outputAsCsv(results []*Result) error {
rows := make([][]string, len(results)+1)
rows[0] = []string{"Show", "Season", "Detected", "Comment"}
for index, result := range results {
rows[index+1] = []string{
result.Show.Title(),
strconv.Itoa(result.Season.Number()),
seasonStatusString(result),
missingEpisodeString(result.MissingEpisodesList),
}
}
w := csv.NewWriter(os.Stdout)
for _, row := range rows {
if err := w.Write(row); err != nil {
return err
}
}
w.Flush()
if err := w.Error(); err != nil {
return err
}
return nil
}