/
procstate_sorter.go
164 lines (151 loc) · 4.04 KB
/
procstate_sorter.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package tui
import (
"fmt"
"github.com/f1bonacc1/process-compose/src/types"
"sort"
"strings"
)
type ColumnID int
const (
ProcessStateUndefined ColumnID = -1
ProcessStateIcon ColumnID = 0
ProcessStatePid ColumnID = 1
ProcessStateName ColumnID = 2
ProcessStateNamespace ColumnID = 3
ProcessStateStatus ColumnID = 4
ProcessStateAge ColumnID = 5
ProcessStateHealth ColumnID = 6
ProcessStateRestarts ColumnID = 7
ProcessStateExit ColumnID = 8
)
var columnNames = map[ColumnID]string{
ProcessStateUndefined: "",
ProcessStatePid: "PID",
ProcessStateName: "NAME",
ProcessStateNamespace: "NAMESPACE",
ProcessStateStatus: "STATUS",
ProcessStateAge: "AGE",
ProcessStateHealth: "HEALTH",
ProcessStateRestarts: "RESTARTS",
ProcessStateExit: "EXIT",
}
var columnIDs = map[string]ColumnID{
"": ProcessStateUndefined,
"PID": ProcessStatePid,
"NAME": ProcessStateName,
"NAMESPACE": ProcessStateNamespace,
"STATUS": ProcessStateStatus,
"AGE": ProcessStateAge,
"HEALTH": ProcessStateHealth,
"RESTARTS": ProcessStateRestarts,
"EXIT": ProcessStateExit,
}
func (c ColumnID) String() string {
return columnNames[c]
}
func StringToColumnID(s string) (ColumnID, error) {
id, ok := columnIDs[strings.ToUpper(s)]
if !ok {
return ProcessStateUndefined, fmt.Errorf("unknown column name: %s", s)
}
return id, nil
}
func ColumnNames() []string {
var names []string
for _, name := range columnNames {
if name != "" {
names = append(names, name)
}
}
sort.Strings(names)
return names
}
type StateSorter struct {
sortByColumn ColumnID
isAsc bool
}
type sortFn func(i, j int) bool
func sortProcessesState(sortBy ColumnID, asc bool, states *types.ProcessesState) error {
if states == nil {
return fmt.Errorf("empty states")
}
sorter := getSorter(sortBy, states)
if !asc {
sorter = reverse(sorter)
}
sort.Slice(states.States, sorter)
return nil
}
func getSorter(sortBy ColumnID, states *types.ProcessesState) sortFn {
switch sortBy {
case ProcessStatePid:
return func(i, j int) bool {
if states.States[i].Pid == states.States[j].Pid {
return states.States[i].Name < states.States[j].Name
} else {
return states.States[i].Pid < states.States[j].Pid
}
}
case ProcessStateNamespace:
return func(i, j int) bool {
if states.States[i].Namespace == states.States[j].Namespace {
return states.States[i].Name < states.States[j].Name
} else {
return states.States[i].Namespace < states.States[j].Namespace
}
}
case ProcessStateStatus:
return func(i, j int) bool {
if states.States[i].Status == states.States[j].Status {
return states.States[i].Name < states.States[j].Name
} else {
return states.States[i].Status < states.States[j].Status
}
}
case ProcessStateAge:
return func(i, j int) bool {
if states.States[i].Age == states.States[j].Age {
return states.States[i].Name < states.States[j].Name
} else {
return states.States[i].Age < states.States[j].Age
}
}
case ProcessStateHealth:
return func(i, j int) bool {
if states.States[i].Health == states.States[j].Health {
return states.States[i].Name < states.States[j].Name
} else {
return states.States[i].Health < states.States[j].Health
}
}
case ProcessStateRestarts:
return func(i, j int) bool {
if states.States[i].Restarts == states.States[j].Restarts {
return states.States[i].Name < states.States[j].Name
} else {
return states.States[i].Restarts < states.States[j].Restarts
}
}
case ProcessStateExit:
return func(i, j int) bool {
eci := getStrForExitCode(states.States[i])
ecj := getStrForExitCode(states.States[j])
if eci == ecj {
return states.States[i].Name < states.States[j].Name
} else {
return eci < ecj
}
}
case ProcessStateName:
fallthrough
default:
return func(i, j int) bool {
return states.States[i].Name < states.States[j].Name
}
}
}
func reverse(less func(i, j int) bool) func(i, j int) bool {
return func(i, j int) bool {
return !less(i, j)
}
}