fix(spec): isolate __complete probe from the controlling terminal (#111)
run the `__complete` probe subprocess in its own session so that a cobra-incompatible binary that ignores `__complete` can't mess with the user's controlling terminal. tested on both macos and nixos. encountered the problem while trying to use [visidata](https://www.visidata.org/). --------- Co-authored-by: shemishtamesh <shemishtamail@gmail.com>
This commit is contained in:
co-authored by
shemishtamesh
parent
d669e97423
commit
0b77c9334b
+13
-1
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -90,6 +91,16 @@ func buildCobraCacheKey(binKey string, args []string, partial string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// newProbeCmd builds and isolates the `__complete` probe command.
|
||||
// starts the child in its own session so it has no controlling terminal
|
||||
// and therefore won't affect the user's tty in the case of programs that
|
||||
// don't respect `__complete`.
|
||||
func newProbeCmd(ctx context.Context, binName string, args []string) *exec.Cmd {
|
||||
cmd := exec.CommandContext(ctx, binName, args...)
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
||||
return cmd
|
||||
}
|
||||
|
||||
// QueryCobraComplete calls `binName __complete <args> <partial>` and returns
|
||||
// structured suggestions cached per binary mtime, args, and partial.
|
||||
// returns nil if the binary is not Cobra-based or times out.
|
||||
@@ -113,7 +124,8 @@ func QueryCobraComplete(binName string, args []string, partial string) []Suggest
|
||||
|
||||
cmdArgs := append([]string{"__complete"}, args...)
|
||||
cmdArgs = append(cmdArgs, partial)
|
||||
out, err := exec.CommandContext(ctx, binName, cmdArgs...).Output()
|
||||
probe := newProbeCmd(ctx, binName, cmdArgs)
|
||||
out, err := probe.Output()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user