Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,259 changes: 2,259 additions & 0 deletions cmd/myf5/coverage_test.go

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions cmd/myf5/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ type exitPanic struct {
code int
}

// exitFunc is the process termination function, replaced during interactive mode
// so failed commands return to the shell prompt instead of exiting the process.
var exitFunc = os.Exit
var (
exitFunc = os.Exit
isTerminalFunc = term.IsTerminal
makeRawFunc = term.MakeRaw
restoreTerminalFunc = term.Restore
interactiveIn io.Reader = os.Stdin
interactiveOut io.Writer = os.Stdout
)

// startInteractive launches the interactive CLI shell with command history
// and tab-completion.
func startInteractive() {
fd := int(os.Stdin.Fd())
if !term.IsTerminal(fd) {
if !isTerminalFunc(fd) {
fmt.Fprintln(os.Stderr, "[ERROR] Interactive mode requires a terminal TTY.")
exitFunc(1)
return
Expand All @@ -50,7 +55,7 @@ func startInteractive() {

for {
// Put terminal in raw mode for line editing, history, and tab completion
oldState, err := term.MakeRaw(fd)
oldState, err := makeRawFunc(fd)
if err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] Failed to set raw terminal: %v\n", err)
return
Expand All @@ -59,7 +64,7 @@ func startInteractive() {
terminal := term.NewTerminal(struct {
io.Reader
io.Writer
}{os.Stdin, os.Stdout}, "myf5> ")
}{interactiveIn, interactiveOut}, "myf5> ")

terminal.AutoCompleteCallback = func(line string, pos int, key rune) (string, int, bool) {
if key != '\t' {
Expand All @@ -69,7 +74,7 @@ func startInteractive() {
}

line, err := terminal.ReadLine()
_ = term.Restore(fd, oldState)
_ = restoreTerminalFunc(fd, oldState)

if err != nil {
// EOF (Ctrl-D) or terminal closed
Expand Down
Loading
Loading