Skip to content

Commit db29267

Browse files
committed
fix: terminal not restored after quit (c-bata/go-prompt#228)
The patch workaround issue [c-bata/go-prompt#228], make the terminal running ts-cli works correctly after quit, exit, ControlC or ControlD. Signed-off-by: JUN JIE NAN <[email protected]>
1 parent e8f316b commit db29267

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

app/ts-cli/geminicli/cli.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"net/http"
2424
"net/url"
2525
"os"
26+
"os/exec"
2627
"os/signal"
28+
"runtime"
2729
"sort"
2830
"strings"
2931
"syscall"
@@ -182,13 +184,22 @@ func (c *CommandLine) elapse() {
182184

183185
}
184186

187+
func (c *CommandLine) tearDown(_ *prompt.Buffer) {
188+
if runtime.GOOS != "windows" {
189+
reset := exec.Command("stty", "-raw", "echo")
190+
reset.Stdin = os.Stdin
191+
_ = reset.Run()
192+
}
193+
os.Exit(0)
194+
}
195+
185196
func (c *CommandLine) Execute(s string) error {
186197
var err error
187198

188199
if s == "" {
189200
return nil
190201
} else if s == "quit" || s == "exit" {
191-
os.Exit(0)
202+
c.tearDown(nil)
192203
}
193204

194205
ast := &geminiql.QLAst{}
@@ -510,8 +521,14 @@ func (c *CommandLine) Run() error {
510521
Key: prompt.ShiftRight,
511522
Fn: prompt.GoRightWord,
512523
},
524+
prompt.KeyBind{
525+
Key: prompt.ControlC,
526+
Fn: c.tearDown,
527+
},
513528
),
514529
)
530+
// Make sure key bind ControlD reset stty correctly
531+
defer c.tearDown(nil)
515532
p.Run()
516533
return nil
517534
}

0 commit comments

Comments
 (0)