File tree Expand file tree Collapse file tree 3 files changed +33
-12
lines changed
Expand file tree Collapse file tree 3 files changed +33
-12
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import (
1414 "runtime"
1515 "strconv"
1616 "strings"
17+ "sync"
1718 "sync/atomic"
1819 "syscall"
1920 "time"
@@ -77,6 +78,8 @@ type escort struct {
7778 watcherErrors chan error
7879 sig chan os.Signal
7980
81+ wg sync.WaitGroup
82+
8083 binPath string
8184 bin * exec.Cmd
8285 stdoutPipe io.ReadCloser
@@ -108,14 +111,17 @@ func (e *escort) run() (err error) {
108111 _ = os .Remove (e .binPath )
109112 }()
110113
111- go e .runBin ()
112- go e .watchingBin ()
113- go e .watchingFiles ()
114+ e .wg .Add (3 )
115+ go func () { defer e .wg .Done (); e .runBin () }()
116+ go func () { defer e .wg .Done (); e .watchingBin () }()
117+ go func () { defer e .wg .Done (); e .watchingFiles () }()
114118
115119 signal .Notify (e .sig , syscall .SIGTERM , syscall .SIGINT , os .Interrupt )
116120 <- e .sig
117121
118122 e .terminate ()
123+ close (e .hitCh )
124+ e .wg .Wait ()
119125
120126 log .Println ("See you next time 👋" )
121127
@@ -153,7 +159,11 @@ func (e *escort) init() (err error) {
153159 e .binPath += ".exe"
154160 }
155161
156- e .hitFunc = e .runBin
162+ e .hitFunc = func () {
163+ e .wg .Add (1 )
164+ e .runBin ()
165+ e .wg .Done ()
166+ }
157167
158168 e .preRunCommands = parsePreRunCommands (c .preRun )
159169
@@ -214,15 +224,22 @@ func (e *escort) watchingFiles() {
214224
215225func (e * escort ) watchingBin () {
216226 var timer * time.Timer
217- for range e .hitCh {
218- // reset timer
219- if timer != nil && ! timer .Stop () {
220- select {
221- case <- timer .C :
222- default :
227+ for {
228+ select {
229+ case <- e .ctx .Done ():
230+ if timer != nil {
231+ timer .Stop ()
232+ }
233+ return
234+ case <- e .hitCh :
235+ if timer != nil && ! timer .Stop () {
236+ select {
237+ case <- timer .C :
238+ default :
239+ }
223240 }
241+ timer = time .AfterFunc (e .delay , e .hitFunc )
224242 }
225- timer = time .AfterFunc (e .delay , e .hitFunc )
226243 }
227244}
228245
Original file line number Diff line number Diff line change @@ -312,5 +312,7 @@ func getEscort() *escort {
312312 },
313313 ctx : c ,
314314 terminate : t ,
315+ hitCh : make (chan struct {}, 1 ),
316+ sig : make (chan os.Signal , 1 ),
315317 }
316318}
Original file line number Diff line number Diff line change @@ -2,9 +2,11 @@ package internal
22
33import (
44 "fmt"
5+ "os"
56
67 input "github.com/charmbracelet/bubbles/textinput"
78 tea "github.com/charmbracelet/bubbletea"
9+ "github.com/muesli/termenv"
810)
911
1012type errMsg error
@@ -27,7 +29,7 @@ func NewPrompt(title string, placeholder ...string) *Prompt {
2729 p .textInput .Placeholder = placeholder [0 ]
2830 }
2931
30- p .p = tea .NewProgram (p )
32+ p .p = tea .NewProgram (p , tea . WithOutput ( termenv . NewOutput ( os . Stdout )) )
3133
3234 return p
3335}
You can’t perform that action at this time.
0 commit comments