-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
214 lines (193 loc) · 4.11 KB
/
functions.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
package main
import (
"fmt"
"strings"
"github.com/jroimartin/gocui"
)
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func cursorDown(g *gocui.Gui, v *gocui.View, n int) error {
if v != nil {
cx, cy := v.Cursor()
if cy > n-2 {
return nil
}
if err := v.SetCursor(cx, cy+1); err != nil {
ox, oy := v.Origin()
printfLog("O:(%v,%v),C:(%v,%v) \n", ox, oy, cx, cy)
//显示最后项目后停止向下滚动
if oy > n-cy-2 {
return nil
}
if err := v.SetOrigin(ox, oy+1); err != nil {
return err
}
}
}
return nil
}
func cursorUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
ox, oy := v.Origin()
cx, cy := v.Cursor()
printfLog("O:(%v,%v),C:(%v,%v) \n", ox, oy, cx, cy)
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
if err := v.SetOrigin(ox, oy-1); err != nil {
return err
}
}
}
return nil
}
func PageUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
_, maxY := g.Size()
ox, oy := v.Origin()
cx, cy := v.Cursor()
printfLog("O:(%v,%v),C:(%v,%v) \n", ox, oy, cx, cy)
// 配置光标到第一行
if err := v.SetCursor(cx, 0); err != nil {
printfLog("%v\n", err)
}
// 向上滚动
if oy > 0 {
dy := oy - maxY*4/10 + 1
if dy < 0 {
dy = 0
}
if err := v.SetOrigin(ox, dy); err != nil {
return err
}
}
}
return nil
}
func PageDown(g *gocui.Gui, v *gocui.View, n int) error {
if v != nil {
_, maxY := g.Size()
cx, _ := v.Cursor()
ox, oy := v.Origin()
// printfLog("O:(%v,%v),C:(%v,%v) \n", ox, oy, cx, cy)
dy := maxY*4/10 - 1
if dy+oy >= n {
if err := v.SetOrigin(ox, n-dy); err != nil {
return err
}
if err := v.SetCursor(cx, dy-1); err != nil {
return err
}
} else {
if err := v.SetOrigin(ox, dy+oy); err != nil {
return err
}
}
}
return nil
}
func PageHome(g *gocui.Gui, v *gocui.View) error {
if v != nil {
if err := v.SetOrigin(0, 0); err != nil {
return err
}
if err := v.SetCursor(0, 0); err != nil {
return err
}
}
return nil
}
func PageEnd(g *gocui.Gui, v *gocui.View, n int) error {
if v != nil {
_, maxY := g.Size()
cy := maxY*4/10 - 2
oy := n - maxY*4/10 + 1
// printfLog("cy %v,oy %v\n", cy, oy)
if err := v.SetOrigin(0, oy); err != nil {
// printfLog("ori %v\n", err)
return err
}
if err := v.SetCursor(0, cy); err != nil {
// printfLog("Cur %v\n", err)
return err
}
}
return nil
}
func nextView(g *gocui.Gui, v *gocui.View) error {
nextIndex := (active + 1) % len(viewArr)
name := viewArr[nextIndex]
printfLog("Going from view " + v.Name() + " to " + name + "\n")
if _, err := g.SetCurrentView(name); err != nil {
return err
}
if nextIndex == 0 {
g.Cursor = true
} else {
g.Cursor = false
}
active = nextIndex
return nil
}
// Item is the selected item
var Item string
func dialogItem(g *gocui.Gui, v *gocui.View) error {
var l string
var err error
_, cy := v.Cursor()
if l, err = v.Line(cy); err != nil {
l = ""
}
switch l {
case "Status":
itemChan <- Item
v, err := g.View("v4")
if err != nil {
printfLog("%v\n", err)
}
v.Clear()
fmt.Fprintf(v, "%v\n", getServiceFiles(Item))
goto Delete
case "Start or Restart":
goto Delete
case "Stop":
goto Delete
case "Close":
}
Delete:
if err := g.DeleteView("dialog"); err != nil {
return err
}
if _, err = g.SetCurrentView("v2"); err != nil {
return err
}
return nil
}
func itemSelect(g *gocui.Gui, v *gocui.View) error {
var l string
var err error
_, cy := v.Cursor()
if l, err = v.Line(cy); err != nil {
l = ""
}
printfLog("%v\n", strings.Split(l, " ")[0])
Item = strings.Split(l, " ")[0]
maxX, maxY := g.Size()
if v, err := g.SetView("dialog", maxX/2-30, maxY/2, maxX/2+30, maxY/2+5); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Dialog"
v.Wrap = true
v.Highlight = true
v.SelBgColor = gocui.ColorBlack
v.SelFgColor = gocui.ColorYellow
fmt.Fprintf(v, "%s\n", "Status")
fmt.Fprintf(v, "%s\n", "Start or Restart")
fmt.Fprintf(v, "%s\n", "Stop")
fmt.Fprintf(v, "%s\n", "Close")
if _, err := g.SetCurrentView("dialog"); err != nil {
return err
}
}
return nil
}