Skip to content

Commit 7e35ee3

Browse files
committed
Have run-shell use the first recognized parent shell by default
1 parent b02a1fb commit 7e35ee3

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

tools/cmd/run_shell/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ func EntryPoint(root *cli.Command) *cli.Command {
4747
Help: "Specify a value for the shell_integration option, overriding the one from kitty.conf.",
4848
})
4949
sc.Add(cli.OptionSpec{
50-
Name: "--shell",
51-
Help: "Specify the shell command to run. If not specified the value of the shell option from kitty.conf is used.",
50+
Name: "--shell",
51+
Default: ".",
52+
Help: "Specify the shell command to run. The default value of :code:`.` will use the parent shell if recognized, falling back to the value of the :code:`shell` option from kitty.conf.",
5253
})
5354
return sc
5455
}

tools/tui/run.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"runtime"
1212
"strings"
1313

14+
"github.com/shirou/gopsutil/v3/process"
1415
"golang.org/x/sys/unix"
1516

1617
"kitty/tools/config"
@@ -63,18 +64,49 @@ var relevant_kitty_opts = utils.Once(func() KittyOpts {
6364
return read_relevant_kitty_opts(filepath.Join(utils.ConfigDir(), "kitty.conf"))
6465
})
6566

66-
func ResolveShell(shell string) []string {
67-
if shell == "" {
68-
shell = relevant_kitty_opts().Shell
69-
if shell == "." {
70-
s, e := utils.LoginShellForCurrentUser()
71-
if e != nil {
72-
shell = "/bin/sh"
73-
} else {
74-
shell = s
67+
func get_shell_from_kitty_conf() (shell string) {
68+
shell = relevant_kitty_opts().Shell
69+
if shell == "." {
70+
s, e := utils.LoginShellForCurrentUser()
71+
if e != nil {
72+
shell = "/bin/sh"
73+
} else {
74+
shell = s
75+
}
76+
}
77+
return
78+
}
79+
80+
func find_shell_parent_process() string {
81+
var p *process.Process
82+
var err error
83+
for {
84+
if p == nil {
85+
p, err = process.NewProcess(int32(os.Getppid()))
86+
} else {
87+
p, err = p.Parent()
88+
}
89+
if err != nil {
90+
return ""
91+
}
92+
if cmdline, err := p.CmdlineSlice(); err == nil && len(cmdline) > 0 {
93+
exe := get_shell_name(filepath.Base(cmdline[0]))
94+
if shell_integration.IsSupportedShell(exe) {
95+
return exe
7596
}
7697
}
7798
}
99+
}
100+
101+
func ResolveShell(shell string) []string {
102+
switch shell {
103+
case "":
104+
shell = get_shell_from_kitty_conf()
105+
case ".":
106+
if shell = find_shell_parent_process(); shell == "" {
107+
shell = get_shell_from_kitty_conf()
108+
}
109+
}
78110
shell_cmd, err := shlex.Split(shell)
79111
if err != nil {
80112
shell_cmd = []string{shell}

0 commit comments

Comments
 (0)