/
view.go
229 lines (210 loc) · 5.27 KB
/
view.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
package main
import (
"fmt"
"github.com/nsf/termbox-go"
"github.com/tSU-RooT/diskinfo/atasmart"
)
type view struct {
disks []*atasmart.SkDisk
page int
scroll int
scrollable bool
exitCh chan struct{}
}
func initView() view {
v := view{
disks: nil,
page: 0,
exitCh: make(chan struct{}, 5),
}
return v
}
func (v *view) paintScreen() {
w, h := getTermSize()
// draw frame
drawFrame(0, 0, w, 4, termbox.ColorBlue, termbox.ColorDefault)
drawFrame(0, 3, w, 5, termbox.ColorBlue, termbox.ColorDefault)
drawFrame(0, 7, w, h-7, termbox.ColorBlue, termbox.ColorDefault)
v.paintSelecter()
v.paintData(v.disks[v.page])
v.paintAttr(v.disks[v.page])
}
func (v *view) paintSelecter() {
for i, d := range v.disks {
x := 2 + i*12
fg := termbox.ColorWhite
bg := termbox.ColorDefault
if v, err := d.GetSize(); err == nil {
var unit float64
unit = (float64)(v / 1024.0)
c := 0
u := []string{"KB", "MB", "GB", "TB", "PB"}
for unit >= 1000 && c < len(u)-1 {
unit /= 1024.0
c++
}
t := fmt.Sprintf("%.2f[%s]", unit, u[c])
drawText(t, x, 1, fg, bg)
}
if i == v.page {
bg = termbox.ColorCyan
}
p := fmt.Sprintf("%- 10s", d.Path)
drawText(p, x, 2, fg, bg)
}
}
func (v *view) paintData(disk *atasmart.SkDisk) {
y := 4
text := fmt.Sprintf("Name: %s", disk.Data.Model)
drawText(text, 2, y, termbox.ColorWhite, termbox.ColorDefault)
y++
text = fmt.Sprintf("Firmware: %s", disk.Data.Firmware)
drawText(text, 2, y, termbox.ColorWhite, termbox.ColorDefault)
y++
if !masking {
text = fmt.Sprintf("Serial: %s", disk.Data.Serial)
} else {
text = "Serial: "
for i := 0; i < len(disk.Data.Serial); i++ {
text += "*"
}
}
drawText(text, 2, y, termbox.ColorWhite, termbox.ColorDefault)
y = 4
if v, err := disk.GetSmartPowerCycle(); err == nil {
text = fmt.Sprintf("Power On Count: %d", v)
drawText(text, 42, y, termbox.ColorWhite, termbox.ColorDefault)
y++
}
if v, err := disk.GetSmartTemperature(); err == nil {
t := (float64)((v - 273150) / 1000.0)
text = fmt.Sprintf("Temperature: %.1f", t)
drawText(text, 42, y, termbox.ColorWhite, termbox.ColorDefault)
}
}
func (v *view) paintAttr(disk *atasmart.SkDisk) {
w, h := getTermSize()
attrs := disk.Attributes
y := 8
drawBgLine(1, y, w-2, termbox.ColorGreen)
drawText("Status", 1, y, termbox.ColorWhite, termbox.ColorGreen)
drawText("ID", 9, y, termbox.ColorWhite, termbox.ColorGreen)
drawText("Attribute", 13, y, termbox.ColorWhite, termbox.ColorGreen)
drawText("Current", 41, y, termbox.ColorWhite, termbox.ColorGreen)
drawText("Worst", 49, y, termbox.ColorWhite, termbox.ColorGreen)
drawText("Threshold", 56, y, termbox.ColorWhite, termbox.ColorGreen)
drawText("Raw", 66, y, termbox.ColorWhite, termbox.ColorGreen)
y++
i := v.scroll
for {
if i >= len(attrs) {
break
} else if y >= h-1 {
// clear
drawText(" ", 1, h-2, termbox.ColorBlack, termbox.ColorWhite)
drawText("↓", 1, h-2, termbox.ColorBlack, termbox.ColorWhite)
break
}
at := attrs[i]
bg := termbox.ColorDefault
st := "Good"
if at.Threshold != 0 && at.Current < at.Threshold {
bg = termbox.ColorRed
st = "Bad"
}
drawBgLine(1, y, w-2, bg)
drawText(st, 3, y, termbox.ColorWhite, bg)
text := fmt.Sprintf("%02X", at.ID)
drawText(text, 9, y, termbox.ColorWhite, bg)
drawText(at.Name, 13, y, termbox.ColorWhite, bg)
text = fmt.Sprintf("%03d", at.Current)
drawText(text, 44, y, termbox.ColorWhite, bg)
text = fmt.Sprintf("%03d", at.Worst)
drawText(text, 50, y, termbox.ColorWhite, bg)
text = fmt.Sprintf("% 3d", at.Threshold)
drawText(text, 62, y, termbox.ColorWhite, bg)
text = fmt.Sprintf("%012X", at.Raw)
drawText(text, 66, y, termbox.ColorWhite, bg)
i++
y++
}
if v.scrollable && v.scroll > 0 {
drawText(" ", 1, 9, termbox.ColorBlack, termbox.ColorWhite)
drawText("↑", 1, 9, termbox.ColorBlack, termbox.ColorWhite)
}
}
func (v *view) update() {
v.paintScreen()
termbox.Flush()
}
func (v *view) updateAttributes() {
v.paintAttr(v.disks[v.page])
termbox.Flush()
}
func (v *view) Loop() error {
v.update()
v.updateScrollable()
evCh := make(chan termbox.Event)
go func() {
for {
evCh <- termbox.PollEvent()
}
}()
loop:
for {
select {
case ev := <-evCh:
if ev.Type == termbox.EventResize {
setTermSize(ev.Width, ev.Height)
v.scroll = 0
v.updateScrollable()
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
v.update()
continue
}
v.handleKey(ev.Key)
case <-v.exitCh:
break loop
}
}
// terminate
for _, d := range v.disks {
d.Free()
}
v.disks = nil
return nil
}
const occupiedspace = 10
func (v *view) updateScrollable() {
_, h := getTermSize()
v.scrollable = len(v.disks[v.page].Attributes) > (h - occupiedspace)
}
func (v *view) handleKey(key termbox.Key) {
switch key {
case termbox.KeyArrowLeft:
if v.page >= 1 {
v.page--
v.scroll = 0
v.update()
}
case termbox.KeyArrowRight:
if v.page < len(v.disks)-1 {
v.page++
v.scroll = 0
v.update()
}
case termbox.KeyArrowUp:
if v.scrollable && v.scroll > 0 {
v.scroll--
v.updateAttributes()
}
case termbox.KeyArrowDown:
_, h := getTermSize()
if v.scrollable && v.scroll < len(v.disks[v.page].Attributes)-(h-occupiedspace) {
v.scroll++
v.updateAttributes()
}
case termbox.KeyCtrlQ:
v.exitCh <- struct{}{}
}
}