forked from skanehira/docui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
240 lines (197 loc) · 4.89 KB
/
common.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package common
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/signal"
"reflect"
"sort"
"strconv"
"strings"
"syscall"
"time"
"unsafe"
"github.com/docker/docker/api/types"
"github.com/jroimartin/gocui"
)
var cutNewlineReplacer = strings.NewReplacer("\r", "", "\n", "")
// StructToJSON convert struct to json.
func StructToJSON(i interface{}) string {
j, err := json.Marshal(i)
if err != nil {
return ""
}
out := new(bytes.Buffer)
json.Indent(out, j, "", " ")
return out.String()
}
// SortKeys sort keys.
func SortKeys(keys []string) []string {
sort.Strings(keys)
return keys
}
// GetOSenv get os environment.
func GetOSenv(env string) string {
keyval := strings.SplitN(env, "=", 2)
if keyval[1][:1] == "$" {
keyval[1] = os.Getenv(keyval[1][1:])
return strings.Join(keyval, "=")
}
return env
}
// OutputFormattedLine print formatted panel info.
func OutputFormattedLine(v *gocui.View, i interface{}) {
elem := reflect.ValueOf(i).Elem()
size := elem.NumField()
maxX, _ := v.Size()
// column width
cw := (maxX - 10)
// parse format string length
parseLength := func(cw int, str string) (int, int) {
minMax := strings.Split(str, " ")
if len(minMax) < 2 {
return -1, -1
}
min, _ := strconv.ParseFloat(strings.Split(minMax[0], ":")[1], 64)
max, _ := strconv.ParseFloat(strings.Split(minMax[1], ":")[1], 64)
return int(float64(cw) * min), int(float64(cw) * max)
}
for i := 0; i < size; i++ {
value, ok := elem.Field(i).Interface().(string)
if !ok {
continue
}
max, min := parseLength(cw, elem.Type().Field(i).Tag.Get("len"))
// skip if can not get the tag
if max == -1 && min == -1 {
continue
}
if max < min {
max = min
}
if len(value) > max {
if max-3 > 0 {
value = value[:max-3] + "..."
}
if max-3 < 1 {
value = value[:1]
}
}
fmt.Fprintf(v, "%-"+strconv.Itoa(max)+"s ", value)
}
fmt.Fprint(v, "\n")
}
// OutputFormattedHeader print formatted panel header.
func OutputFormattedHeader(v *gocui.View, i interface{}) {
elem := reflect.ValueOf(i).Elem()
size := elem.NumField()
for i := 0; i < size; i++ {
field := elem.Type().Field(i)
tag := field.Tag.Get("tag")
name := field.Name
if field.Type.Kind() == reflect.String {
elem.FieldByName(name).SetString(tag)
}
}
OutputFormattedLine(v, i)
}
// ParseDateToString parse date to string.
func ParseDateToString(unixtime int64) string {
t := time.Unix(unixtime, 0)
return t.Format("2006/01/02 15:04:05")
}
// ParseSizeToString parse size to string.
func ParseSizeToString(size int64) string {
mb := float64(size) / 1024 / 1024
return fmt.Sprintf("%.1fMB", mb)
}
// ParsePortToString parse port to string.
func ParsePortToString(ports []types.Port) string {
var port string
for _, p := range ports {
if p.PublicPort == 0 {
port += fmt.Sprintf("%d/%s ", p.PrivatePort, p.Type)
} else {
port += fmt.Sprintf("%s:%d->%d/%s ", p.IP, p.PublicPort, p.PrivatePort, p.Type)
}
}
return port
}
// ParseRepoTag parse image repo and tag.
func ParseRepoTag(repoTag string) (string, string) {
tmp := strings.SplitN(repoTag, ":", 2)
return tmp[0], tmp[1]
}
// ParseLabels parse image labels.
func ParseLabels(labels map[string]string) string {
if len(labels) < 1 {
return ""
}
var result string
for label, value := range labels {
result += fmt.Sprintf("%s=%s ", label, value)
}
return result
}
// DateNow return date time.
func DateNow() string {
return time.Now().Format("2006/01/02 15:04:05")
}
// CutNewline cut new line.
func CutNewline(i string) string {
return cutNewlineReplacer.Replace(i)
}
func getTermSize(fd uintptr) (int, int) {
var sz struct {
rows uint16
cols uint16
}
_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
fd, uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
return int(sz.cols), int(sz.rows)
}
// IsTerminalWindowSizeThanZero check terminal window size
func IsTerminalWindowSizeThanZero() bool {
out, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
Logger.Error(err)
return false
}
defer out.Close()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGWINCH, syscall.SIGINT)
for {
// check terminal window size
termw, termh := getTermSize(out.Fd())
if termw > 0 && termh > 0 {
return true
}
select {
case signal := <-signalCh:
switch signal {
// when the terminal window size is changed
case syscall.SIGWINCH:
continue
// use ctrl + c to cancel
case syscall.SIGINT:
return false
}
}
}
}
// IsValidPanelSize checks if a panel size is valid.
func IsValidPanelSize(x, y, w, h int) error {
if x >= w || y >= h {
return ErrSmallTerminalWindowSize
}
return nil
}
// SetViewWithValidPanelSize run SetView with a valid panel size.
func SetViewWithValidPanelSize(g *gocui.Gui, name string, x, y, w, h int) (*gocui.View, error) {
if err := IsValidPanelSize(x, y, w, h); err != nil {
panic(err)
}
v, err := g.SetView(name, x, y, w, h)
return v, err
}