Skip to content

Commit 49378fb

Browse files
authored
Handle Ctrl-C in repo garden
1 parent 02f4946 commit 49378fb

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

pkg/cmd/repo/garden/garden.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"io"
88
"math/rand"
99
"net/http"
10+
"os"
1011
"os/exec"
12+
"os/signal"
1113
"runtime"
1214
"strconv"
1315
"strings"
16+
"syscall"
1417

1518
"github.com/cli/cli/api"
1619
"github.com/cli/cli/internal/ghinstance"
@@ -216,6 +219,22 @@ func gardenRun(opts *GardenOptions) error {
216219
_ = exec.Command("stty", sttyFileArg, "/dev/tty", "cbreak", "min", "1").Run()
217220
_ = exec.Command("stty", sttyFileArg, "/dev/tty", "-echo").Run()
218221

222+
walkAway := func() {
223+
clear(opts.IO)
224+
fmt.Fprint(out, "\033[?25h")
225+
_ = exec.Command("stty", sttyFileArg, "/dev/tty", strings.TrimSpace(string(oldTTYSettings))).Run()
226+
fmt.Fprintln(out)
227+
fmt.Fprintln(out, utils.Bold("You turn and walk away from the wildflower garden..."))
228+
}
229+
230+
c := make(chan os.Signal)
231+
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
232+
go func(){
233+
<-c
234+
walkAway()
235+
os.Exit(0)
236+
}()
237+
219238
var b []byte = make([]byte, 3)
220239
for {
221240
_, _ = opts.IO.In.Read(b)
@@ -296,12 +315,7 @@ func gardenRun(opts *GardenOptions) error {
296315
fmt.Fprint(out, utils.Bold(sl))
297316
}
298317

299-
clear(opts.IO)
300-
fmt.Fprint(out, "\033[?25h")
301-
_ = exec.Command("stty", sttyFileArg, "/dev/tty", strings.TrimSpace(string(oldTTYSettings))).Run()
302-
fmt.Fprintln(out)
303-
fmt.Fprintln(out, utils.Bold("You turn and walk away from the wildflower garden..."))
304-
318+
walkAway()
305319
return nil
306320
}
307321

0 commit comments

Comments
 (0)