-
Notifications
You must be signed in to change notification settings - Fork 0
/
contests.go
67 lines (61 loc) · 1.41 KB
/
contests.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
package cmd
import (
"io"
"os"
"strings"
"github.com/fatih/color"
"github.com/lcian/cf-tool/client"
"github.com/olekukonko/tablewriter"
)
// Contests command
func Contests() error {
cln := client.Instance
contests, err := cln.GetContests()
if err != nil {
if err = loginAgain(cln, err); err == nil {
contests, err = cln.GetContests()
}
}
if err != nil {
return err
}
output := io.Writer(os.Stdout)
table := tablewriter.NewWriter(output)
table.SetHeader([]string{"ID", "Name", "Start", "Length", "State", "Registration"})
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.SetRowLine(true)
table.SetRowSeparator("─")
table.SetColumnSeparator("│")
table.SetCenterSeparator("┼")
table.SetAutoWrapText(false)
colorCell := func(s string, doColor bool) string {
if doColor {
return colorText(s, color.GreenString)
}
return s
}
for _, contest := range contests {
ok := contest.Registered
table.Append([]string{
colorCell(contest.ID, ok),
colorCell(contest.Name, ok),
colorCell(contest.Start, ok),
colorCell(contest.Length, ok),
colorCell(contest.State, ok),
colorCell(contest.Registration, ok),
})
}
table.Render()
return nil
}
func colorText(text string, f func(s string, a ...interface{}) string) string {
lines := strings.Split(text, "\n")
out := ""
for i := 0; i < len(lines); i++ {
out += f(lines[i])
if i != len(lines)-1 {
out += "\n"
}
}
return out
}