-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathps.go
More file actions
78 lines (73 loc) · 1.48 KB
/
ps.go
File metadata and controls
78 lines (73 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Package ps provides shell determination by process name
package ps
import (
"os"
"strings"
"github.com/carapace-sh/carapace/third_party/github.com/mitchellh/go-ps"
)
// DetermineShell determines shell by parent process name.
func DetermineShell() string {
process, err := ps.FindProcess(os.Getpid())
if err != nil {
return ""
}
for {
if process, err = ps.FindProcess(process.PPid()); err != nil || process == nil {
return ""
}
executable := process.Executable()
switch strings.SplitN(strings.TrimSuffix(executable, ".exe"), "-", 2)[0] {
case "bash":
if isBLE() {
return "bash-ble"
}
return "bash"
case "cmd":
return "cmd-clink"
case "elvish":
return "elvish"
case "fish":
return "fish"
case "ion":
return "ion"
case "nu":
return "nushell"
case "oil":
return "oil"
case "osh":
return "oil"
case "powershell":
return "powershell"
case "pwsh":
return "powershell"
case "tcsh":
return "tcsh"
case "xonsh":
return "xonsh"
case "zsh":
return "zsh"
default:
if strings.Contains(executable, "xonsh-wrapped") { // nix packaged version
return "xonsh"
}
}
}
}
func isBLE() bool {
bleEnvs := []string{
"_bleopt_connect_tty",
"_ble_util_fdlist_cloexec",
"_ble_util_fd_null",
"_ble_util_fd_stderr",
"_ble_util_fd_stdin",
"_ble_util_fd_stdout",
"_ble_util_fdvars_export",
"_ble_util_fd_zero",
}
for _, e := range bleEnvs {
if _, ok := os.LookupEnv(e); ok {
return true
}
}
return false
}