Skip to content

Commit a893df8

Browse files
committed
cmd/asyncpi: Handle interrupts properly.
Ctrl+C should be able to interrupt whenever, not just after a single prompt cycle is finished.
1 parent 14afe7b commit a893df8

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

cmd/asyncpi/main.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,28 @@ func (r *REPL) Prompt() {
114114
for {
115115
var command string
116116
fmt.Fprint(r.out, PromptInit)
117-
select {
118-
case <-r.Interrupted:
119-
r.Cmd["exit"].Run()
120-
default:
117+
waitPrompt := make(chan struct{})
118+
go func(prompt chan struct{}) {
119+
defer func() { close(prompt) }()
121120
// read first space-delimited string (the command).
122121
_, err := fmt.Fscanf(r.in, "%s", &command)
123122
if err != nil {
124123
if err == io.EOF {
125-
command = "exit"
124+
command = CmdExit
126125
}
127126
}
128127
command = strings.TrimSpace(command)
129128
if cmd, ok := r.Cmd[command]; ok {
130129
cmd.Run()
131130
} else {
132131
r.Errorf("Unrecognised command: %s\n", command)
133-
r.Cmd["help"].Run()
132+
r.Cmd[CmdHelp].Run()
134133
}
134+
}(waitPrompt)
135+
select {
136+
case <-r.Interrupted: // Wait for Ctrl+C
137+
r.Cmd[CmdExit].Run()
138+
case <-waitPrompt: // Wait for prompt to finish
135139
}
136140
}
137141
}

0 commit comments

Comments
 (0)