-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorize.go
101 lines (85 loc) · 1.92 KB
/
colorize.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
92
93
94
95
96
97
98
99
100
101
package colordocker
import (
"fmt"
"os"
"strings"
"github.com/docker/docker/api/types"
"github.com/dustin/go-humanize"
"github.com/olekukonko/tablewriter"
)
var (
id string
names string
repos string
tag string
image string
created string
status string
color string
size string
data [][]string
)
const (
evenLineColor = "32m"
oddLineColor = "36m"
)
func Container(containers []types.Container) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "NAMES", "IMAGE", "STATUS"})
for i, container := range containers {
if i%2 == 0 {
color = evenLineColor
} else {
color = oddLineColor
}
id = fmt.Sprintf("\x1b[%s%s\x1b[0m", color, container.ID[:10])
names = fmt.Sprintf(
"\x1b[%s%s\x1b[0m",
color,
strings.Replace(strings.Replace(container.Names[0], " ", "", -1), "/", "", -1))
image = fmt.Sprintf(
"\x1b[%s%s\x1b[0m",
color,
container.Image)
status = fmt.Sprintf(
"\x1b[%s%v\x1b[0m",
color,
strings.Replace(container.Status, " ", "-", -1))
data = append(data, []string{id, names, image, status})
}
table.AppendBulk(data)
table.Render()
}
func Image(images []types.ImageSummary) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"REPOSITORY", "TAG", "IMAGE ID", "SIZE"})
for i, image := range images {
if i%2 == 0 {
color = evenLineColor
} else {
color = oddLineColor
}
repoTags := strings.Split(strings.Replace(image.RepoTags[0], " ", "", -1), ":")
repos = fmt.Sprintf(
"\x1b[%s%s\x1b[0m",
color,
repoTags[0],
)
tag = fmt.Sprintf(
"\x1b[%s%s\x1b[0m",
color,
repoTags[1],
)
id = fmt.Sprintf(
"\x1b[%s%s\x1b[0m",
color,
image.ID[7:19])
size = fmt.Sprintf(
"\x1b[%s%s\x1b[0m",
color,
strings.Replace(humanize.Bytes(uint64(image.Size)), " ", "", -1))
data = append(data, []string{repos, tag, id, size})
}
table.AppendBulk(data)
table.Render()
}