4
4
"context"
5
5
"fmt"
6
6
"os"
7
- "os/exec"
8
7
"syscall"
9
8
"time"
10
9
@@ -30,40 +29,47 @@ type KeyboardWatch struct {
30
29
Ctx context.Context
31
30
Cancel context.CancelFunc
32
31
}
32
+ type KEYBOARD_LOG_LEVEL int
33
+
34
+ const (
35
+ NONE KEYBOARD_LOG_LEVEL = 0
36
+ INFO KEYBOARD_LOG_LEVEL = 1
37
+ DEBUG KEYBOARD_LOG_LEVEL = 2
38
+ )
39
+
33
40
type LogKeyboard struct {
34
- SignalChannel chan <- os.Signal
35
41
ErrorHandle KeyboardError
36
42
Watch KeyboardWatch
37
43
started bool
38
44
IsDockerDesktopActive bool
39
45
IsWatchConfigured bool
40
- shouldPrintInfo bool
41
- printerStop func ()
42
- printerStart func ()
46
+ logLevel KEYBOARD_LOG_LEVEL
47
+ SignalChannel chan <- os.Signal
48
+ // services []string
49
+ // printerStop func()
50
+ // printerStart func()
43
51
}
44
52
45
53
var KeyboardManager * LogKeyboard
46
54
var eg multierror.Group
47
55
var errorColor = "\x1b [1;33m"
48
56
49
- func NewKeyboardManager (isDockerDesktopActive , isWatchConfigured , startWatch bool , watchFn func (ctx context.Context , project * types.Project , services []string , options api.WatchOptions ) error , stop func (), start func (), sc chan <- os.Signal ) {
57
+ func NewKeyboardManager (isDockerDesktopActive , isWatchConfigured bool , sc chan <- os.Signal , watchFn func (ctx context.Context , project * types.Project , services []string , options api.WatchOptions ) error ) {
58
+ // func NewKeyboardManager(isDockerDesktopActive, isWatchConfigured, startWatch bool, watchFn func(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error, stop func(), start func(), sc chan<- os.Signal) {
50
59
km := LogKeyboard {}
51
60
km .IsDockerDesktopActive = isDockerDesktopActive
52
61
km .IsWatchConfigured = isWatchConfigured
53
- km .printerStart = start
54
- km .printerStop = stop
55
- km .shouldPrintInfo = true
62
+ // km.printerStart = start
63
+ // km.printerStop = stop
64
+ km .logLevel = INFO
56
65
// if up --watch and there is a watch config, we should start with watch running
57
- km .Watch .Watching = isWatchConfigured && startWatch
66
+ km .Watch .Watching = false
58
67
km .Watch .WatchFn = watchFn
59
68
km .SignalChannel = sc
60
69
KeyboardManager = & km
61
70
}
62
71
63
72
func (lk * LogKeyboard ) PrintKeyboardInfo (print func ()) {
64
- if ! lk .shouldPrintInfo {
65
- return
66
- }
67
73
fmt .Print ("\033 [?25l" ) // hide cursor
68
74
defer fmt .Printf ("\033 [?25h" ) // show cursor
69
75
@@ -73,8 +79,14 @@ func (lk *LogKeyboard) PrintKeyboardInfo(print func()) {
73
79
lk .started = true
74
80
}
75
81
print ()
76
- lk .createBuffer ()
77
- lk .printInfo ()
82
+ switch lk .logLevel {
83
+ case INFO :
84
+ lk .createBuffer (2 )
85
+ lk .printInfo ()
86
+ case DEBUG :
87
+ lk .createBuffer (3 )
88
+ // lk.printDebugOptions()
89
+ }
78
90
}
79
91
80
92
func (lk * LogKeyboard ) Error (prefix string , err error ) {
@@ -83,10 +95,12 @@ func (lk *LogKeyboard) Error(prefix string, err error) {
83
95
}
84
96
85
97
// This avoids incorrect printing at the end of the terminal
86
- func (lk * LogKeyboard ) createBuffer () {
87
- fmt .Print ("\012 " ) // new line
88
- fmt .Print ("\012 " )
89
- fmt .Print ("\033 [2A" ) // go back 3 lines
98
+ func (lk * LogKeyboard ) createBuffer (lines int ) {
99
+ for i := 0 ; i < lines ; i ++ {
100
+ fmt .Print ("\033 [K" ) // clear
101
+ fmt .Print ("\012 " ) // new line
102
+ }
103
+ fmt .Printf ("\033 [%dA" , lines ) // go back x lines
90
104
}
91
105
92
106
func (lk * LogKeyboard ) printError (height int ) {
@@ -97,13 +111,15 @@ func (lk *LogKeyboard) printError(height int) {
97
111
}
98
112
99
113
func (lk * LogKeyboard ) printInfo () {
100
- height := goterm .Height ()
101
- fmt .Print ("\033 7" ) // save cursor position
102
- lk .printError (height )
103
- fmt .Printf ("\033 [%d;0H" , height ) // Move to last line
104
- // clear line
105
- lk .infoMessage ()
106
- fmt .Print ("\033 8" ) // restore cursor position
114
+ if lk .logLevel == INFO {
115
+ height := goterm .Height ()
116
+ fmt .Print ("\033 7" ) // save cursor position
117
+ lk .printError (height )
118
+ fmt .Printf ("\033 [%d;0H" , height ) // Move to last line
119
+ // clear line
120
+ lk .infoMessage ()
121
+ fmt .Print ("\033 8" ) // restore cursor position
122
+ }
107
123
}
108
124
109
125
func (lk * LogKeyboard ) infoMessage () {
@@ -124,21 +140,23 @@ func (lk *LogKeyboard) infoMessage() {
124
140
}
125
141
126
142
func (lk * LogKeyboard ) clearInfo () {
127
- height := goterm .Height ()
128
- fmt .Print ("\033 7" ) // save cursor position
129
- if lk .ErrorHandle .err != nil {
130
- fmt .Printf ("\033 [%d;0H" , height - 1 )
131
- fmt .Print ("\033 [2K" ) // clear line
143
+ if lk .logLevel == INFO {
144
+ height := goterm .Height ()
145
+ fmt .Print ("\033 7" ) // save cursor position
146
+ if lk .ErrorHandle .err != nil {
147
+ fmt .Printf ("\033 [%d;0H" , height - 1 )
148
+ fmt .Print ("\033 [2K" ) // clear line
149
+ }
150
+ fmt .Printf ("\033 [%d;0H" , height ) // Move to last line
151
+ fmt .Print ("\033 [2K" ) // clear line
152
+ fmt .Print ("\033 8" ) // restore cursor position
153
+ }
154
+ if lk .logLevel == DEBUG {
155
+
132
156
}
133
- fmt .Printf ("\033 [%d;0H" , height ) // Move to last line
134
- fmt .Print ("\033 [2K" ) // clear line
135
- fmt .Print ("\033 8" ) // restore cursor position
136
157
}
137
158
138
159
func (lk * LogKeyboard ) PrintEnter () {
139
- if ! lk .shouldPrintInfo {
140
- return
141
- }
142
160
lk .clearInfo ()
143
161
lk .printInfo ()
144
162
}
@@ -176,6 +194,7 @@ func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, o
176
194
return
177
195
}
178
196
lk .switchWatching ()
197
+ fmt .Println ("lk.isWatching()" , lk .isWatching ())
179
198
if ! lk .isWatching () && lk .Watch .Cancel != nil {
180
199
lk .Watch .Cancel ()
181
200
} else {
@@ -193,48 +212,99 @@ func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, o
193
212
}
194
213
}
195
214
196
- func (lk * LogKeyboard ) debug () {
197
- cmd := exec .Command ("docker" , "debug" , "keep-not-latest-app-1" )
198
- cmd .Stdout = os .Stdout
199
- cmd .Stdin = os .Stdin
200
- cmd .Stderr = os .Stderr
215
+ // func (lk *LogKeyboard) printDebugOptions() {
216
+ // if len(lk.services) == 0 {
217
+ // lk.logLevel = INFO
218
+ // return
219
+ // }
220
+ // height := goterm.Height()
221
+ // //clear
222
+ // fmt.Print("\0337") // save cursor position
223
+ // fmt.Printf("\033[%d;0H\033[2K", height-2)
224
+ // fmt.Printf("\033[%d;0H\033[2K", height-1)
225
+ // fmt.Printf("\033[%d;0H\033[2K", height)
226
+ // //clear
201
227
202
- lk .printerStop ()
203
- if err := cmd .Start (); err != nil {
204
- lk .printerStart ()
205
- lk .Error ("Debug" , fmt .Errorf ("Could not start debug process." ))
206
- return
207
- }
208
- err := cmd .Wait ()
209
- lk .printerStart ()
210
- if err != nil {
211
- lk .Error ("Debug" , err )
212
- }
213
- }
228
+ // fmt.Printf("\033[%d;0H", height-2) // Move to last line
229
+ // fmt.Print("\033[2K[Debug] Select Service:")
230
+ // fmt.Printf("\033[%d;0H", height-1) // Move to last line
231
+ // serviceOpts := ""
232
+ // for i := 0; i < len(lk.services); i++ {
233
+ // serviceOpts = serviceOpts + fmt.Sprintf("(%d) %s ", i+1, lk.services[i])
234
+ // }
235
+ // fmt.Printf("\033[2K%s", serviceOpts)
236
+ // fmt.Print("\0338") // restore cursor position
237
+ // }
238
+
239
+ // func (lk *LogKeyboard) debug(project *types.Project) {
240
+ // lk.services = project.ServiceNames()
241
+ // // show list
242
+ // lk.clearInfo()
243
+ // lk.logLevel = DEBUG
244
+ // // select
245
+ // // clear line
246
+ // choice := make(chan []byte)
247
+ // eg.Go(func() error {
248
+ // for {
249
+ // data := make([]byte, 8)
250
+ // n, err := os.Stdin.Read(data)
251
+
252
+ // // error handling : the basic thing to do is "on error, return"
253
+ // if err != nil {
254
+ // // if os.Stdin got closed, .Read() will return 'io.EOF'
255
+ // if err == io.EOF {
256
+ // log.Printf("stdin closed, exiting")
257
+ // } else {
258
+ // log.Printf("stdin: %s", err)
259
+ // }
260
+ // return err
261
+ // }
262
+ // // a habit to have : truncate your read buffers to 'n' after a .Read()
263
+ // choice <- data[:n]
264
+ // // return nil
265
+ // }
266
+ // })
267
+ // out := <-choice
268
+ // fmt.Println(out)
269
+ //run
270
+ // cmd := exec.Command("docker", "debug", "keep-not-latest-app-1")
271
+ // cmd.Stdout = os.Stdout
272
+ // cmd.Stdin = os.Stdin
273
+ // cmd.Stderr = os.Stderr
274
+
275
+ // lk.printerStop()
276
+ // if err := cmd.Start(); err != nil {
277
+ // lk.printerStart()
278
+ // lk.Error("Debug", fmt.Errorf("Could not start debug process."))
279
+ // return
280
+ // }
281
+ // err := cmd.Wait()
282
+ // lk.printerStart()
283
+ // lk.logLevel = INFO
284
+ // if err != nil {
285
+ // lk.Error("Debug", err)
286
+ // }
287
+ // }
214
288
215
289
func (lk * LogKeyboard ) HandleKeyEvents (event keyboard.KeyEvent , ctx context.Context , project * types.Project , options api.UpOptions ) {
216
290
switch kRune := event .Rune ; kRune {
217
291
case 'V' :
218
292
lk .openDockerDesktop (project )
219
293
case 'W' :
220
294
lk .StartWatch (ctx , project , options )
221
- case 'D' :
222
- lk .debug ()
295
+ // case 'D':
296
+ // lk.debug(project )
223
297
}
224
298
switch key := event .Key ; key {
225
299
case keyboard .KeyCtrlC :
226
300
keyboard .Close ()
227
301
lk .clearInfo ()
228
- lk .shouldPrintInfo = false
302
+ lk .logLevel = NONE
229
303
if lk .Watch .Watching && lk .Watch .Cancel != nil {
230
- // fmt.Println("canceling")
231
304
lk .Watch .Cancel ()
232
- // fmt.Println("canceling watch?")
233
- err := eg .Wait ().ErrorOrNil () // Need to print this ?
234
- fmt .Println ("done@" , err )
305
+ _ = eg .Wait ().ErrorOrNil () // Need to print this ?
235
306
}
236
307
// will notify main thread to kill and will handle gracefully
237
- fmt .Println ("tear down" )
238
308
lk .SignalChannel <- syscall .SIGINT
239
309
case keyboard .KeyEnter :
240
310
lk .PrintEnter ()
0 commit comments