-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
task_row.go
93 lines (81 loc) · 2.62 KB
/
task_row.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
package swarm
import (
"github.com/docker/docker/api/types/swarm"
termui "github.com/gizak/termui"
"github.com/moncho/dry/appui"
"github.com/moncho/dry/docker"
"github.com/moncho/dry/docker/formatter"
drytermui "github.com/moncho/dry/ui/termui"
)
//TaskRow is a Grid row showing runtime information about a task
type TaskRow struct {
task swarm.Task
ID *drytermui.ParColumn
Name *drytermui.ParColumn
Image *drytermui.ParColumn
Node *drytermui.ParColumn
DesiredState *drytermui.ParColumn
CurrentState *drytermui.ParColumn
Error *drytermui.ParColumn
Ports *drytermui.ParColumn
drytermui.Row
}
//NewTaskRow creats a new TaskRow widget
func NewTaskRow(swarmClient docker.SwarmAPI, task swarm.Task, table drytermui.Table) *TaskRow {
ts := formatter.NewTaskStringer(swarmClient, task, true)
row := &TaskRow{
task: task,
ID: drytermui.NewThemedParColumn(appui.DryTheme, ts.ID()),
Name: drytermui.NewThemedParColumn(appui.DryTheme, ts.Name()),
Image: drytermui.NewThemedParColumn(appui.DryTheme, ts.Image()),
Node: drytermui.NewThemedParColumn(appui.DryTheme, ts.NodeID()),
DesiredState: drytermui.NewThemedParColumn(appui.DryTheme, ts.DesiredState()),
CurrentState: drytermui.NewThemedParColumn(appui.DryTheme, ts.CurrentState()),
Error: drytermui.NewThemedParColumn(appui.DryTheme, ts.Error()),
Ports: drytermui.NewThemedParColumn(appui.DryTheme, ts.Ports()),
}
row.Height = 1
row.Table = table
//Columns are rendered following the slice order
row.Columns = []termui.GridBufferer{
row.ID,
row.Name,
row.Image,
row.Node,
row.DesiredState,
row.CurrentState,
row.Error,
row.Ports,
}
updateStateColumns(row)
return row
}
//Highlighted marks this rows as being highlighted
func (row *TaskRow) Highlighted() {
row.changeTextColor(
termui.Attribute(appui.DryTheme.Fg),
termui.Attribute(appui.DryTheme.CursorLineBg))
}
//NotHighlighted marks this rows as being not highlighted
func (row *TaskRow) NotHighlighted() {
row.changeTextColor(
termui.Attribute(appui.DryTheme.ListItem),
termui.Attribute(appui.DryTheme.Bg))
}
func (row *TaskRow) changeTextColor(fg, bg termui.Attribute) {
row.ID.TextFgColor = fg
row.ID.TextBgColor = bg
}
//updateStateColumns changes the color of state-related column depending
//on the task state
func updateStateColumns(row *TaskRow) {
var color termui.Attribute
if row.DesiredState.Text == "Running" {
color = appui.Running
} else {
color = appui.NotRunning
}
row.DesiredState.TextFgColor = color
row.CurrentState.TextFgColor = color
row.Error.TextFgColor = color
}