From d169fe2f265abcc86963ed0ed1cebe5c3a57a224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 1 May 2021 16:10:12 +0300 Subject: [PATCH] Improve shell detection SHELL in environment is not necessarily the current shell; POSIX specifies it as the user's preferred one. Use parent process name primarily instead, if available. https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html --- powerline.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/powerline.go b/powerline.go index 0b640ac1..d8e7485a 100644 --- a/powerline.go +++ b/powerline.go @@ -12,6 +12,7 @@ import ( pwl "github.com/justjanne/powerline-go/powerline" "github.com/mattn/go-runewidth" + "github.com/shirou/gopsutil/process" "golang.org/x/crypto/ssh/terminal" "golang.org/x/text/width" ) @@ -81,7 +82,15 @@ func newPowerline(cfg Config, cwd string, align alignment) *powerline { p.theme = cfg.Themes[cfg.Theme] if cfg.Shell == "autodetect" { - cfg.Shell = detectShell(os.Getenv("SHELL")) + var shellExe string + proc, err := process.NewProcess(int32(os.Getppid())) + if err == nil { + shellExe, _ = proc.Exe() + } + if shellExe == "" { + shellExe = os.Getenv("SHELL") + } + cfg.Shell = detectShell(shellExe) } p.shell = cfg.Shells[cfg.Shell] p.reset = fmt.Sprintf(p.shell.ColorTemplate, "[0m") @@ -117,12 +126,12 @@ func newPowerline(cfg Config, cwd string, align alignment) *powerline { return p } -func detectShell(envShell string) string { +func detectShell(shellExe string) string { var shell string - envShell = path.Base(envShell) - if strings.Contains(envShell, "bash") { + shellExe = path.Base(shellExe) + if strings.Contains(shellExe, "bash") { shell = "bash" - } else if strings.Contains(envShell, "zsh") { + } else if strings.Contains(shellExe, "zsh") { shell = "zsh" } else { shell = "bare" @@ -460,4 +469,3 @@ func (p *powerline) supportsRightModules() bool { func (p *powerline) isRightPrompt() bool { return p.align == alignRight && p.supportsRightModules() } -