-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.go
More file actions
103 lines (81 loc) · 2.06 KB
/
terminal.go
File metadata and controls
103 lines (81 loc) · 2.06 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/shirou/gopsutil/process"
)
type Terminal struct {
Path string
}
var KnownEnvironments = map[string]string{
"WT_SESSION": "wt.exe",
"WEZTERM_PANE": "wezterm-gui.exe",
"ALACRITTY_LOG": "alacritty.exe",
"TABBY_PROFILE": "Tabby.exe",
"TERMINUS_PLUGINS": "Terminus.exe",
"ConEmuPID": "ConEmu64.exe",
"CMDER_ROOT": "Cmder.exe",
}
func (t *Terminal) Build(sh *Shell, cmd string, args []string) (string, []string) {
command := sh.Build(cmd, args)
if t.Path == "" {
return command[0], command[1:]
}
exe := strings.ToLower(filepath.Base(t.Path))
switch exe {
case "wt.exe", "windowsterminal.exe":
return "wt.exe", append([]string{"new-tab"}, command...)
case "rio.exe":
return t.Path, append([]string{"-e"}, command...)
case "tabby.exe":
return t.Path, append([]string{"run"}, command...)
case "alacritty.exe":
return t.Path, append([]string{"-e"}, command...)
case "wezterm-gui.exe", "wezterm.exe":
return t.Path, append([]string{"start", "--"}, command...)
case "conemu64.exe", "conemu.exe":
return t.Path, append([]string{"-run"}, command...)
}
return command[0], command[1:]
}
func GetCurrentTerminalAndShell() (*Terminal, *Shell, error) {
// resolve shell first
pid := int32(os.Getppid())
shellProc, err := process.NewProcess(pid)
if err != nil {
return nil, nil, err
}
path, err := shellProc.Exe()
if err != nil {
return nil, nil, err
}
shell := Shell{path}
// find terminal from env
var terminal Terminal
for env, exe := range KnownEnvironments {
if os.Getenv(env) == "" {
continue
}
path, err := exec.LookPath(exe)
if err != nil && exe == "ConEmu64.exe" {
path, _ = exec.LookPath("ConEmu.exe")
}
terminal.Path = path
break
}
// fallback to parent
if terminal.Path == "" {
terminalProc, err := shellProc.Parent()
if err != nil {
return &terminal, &shell, nil
}
path, err = terminalProc.Exe()
if err != nil {
return nil, nil, err
}
terminal.Path = path
}
return &terminal, &shell, nil
}