diff --git a/README.md b/README.md index 2edb75e0..0879950a 100644 --- a/README.md +++ b/README.md @@ -244,8 +244,8 @@ Usage of powerline-go: (default "root,cwd,user,host,ssh,perms,git-branch,git-status,hg,jobs,exit,cwd-path") -shell string Set this to your shell type - (valid choices: bare, bash, zsh) - (default "bash") + (valid choices: autodetect, bare, bash, zsh) + (default "autodetect") -shell-var string A shell variable to add to the segments. -shell-var-no-warn-empty diff --git a/main.go b/main.go index eb0c590f..4f91177a 100644 --- a/main.go +++ b/main.go @@ -201,9 +201,9 @@ func main() { "(valid choices: default, low-contrast)")), Shell: flag.String( "shell", - "bash", + "autodetect", commentsWithDefaults("Set this to your shell type", - "(valid choices: bare, bash, zsh)")), + "(valid choices: autodetect, bare, bash, zsh)")), Modules: flag.String( "modules", "venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root", diff --git a/powerline.go b/powerline.go index 023e0ab6..8a80d56c 100644 --- a/powerline.go +++ b/powerline.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/user" + "path" "strconv" "strings" "sync" @@ -37,6 +38,7 @@ type powerline struct { username string pathAliases map[string]string theme Theme + shell string shellInfo ShellInfo reset string symbolTemplates Symbols @@ -80,7 +82,12 @@ func newPowerline(args args, cwd string, priorities map[string]int, align alignm p.userIsAdmin = userIsAdmin() p.theme = themes[*args.Theme] - p.shellInfo = shellInfos[*args.Shell] + if *args.Shell == "autodetect" { + p.shell = detectShell(os.Getenv("SHELL")) + } else { + p.shell = *args.Shell + } + p.shellInfo = shellInfos[p.shell] p.reset = fmt.Sprintf(p.shellInfo.colorTemplate, "[0m") p.symbolTemplates = symbolTemplates[*args.Mode] p.priorities = priorities @@ -119,6 +126,19 @@ func newPowerline(args args, cwd string, priorities map[string]int, align alignm return p } +func detectShell(envShell string) string { + var shell string + envShell = path.Base(envShell) + if strings.Contains(envShell, "bash") { + shell = "bash" + } else if strings.Contains(envShell, "zsh") { + shell = "zsh" + } else { + shell = "bare" + } + return shell +} + func initSegments(p *powerline, mods []string) { orderedSegments := map[int][]pwl.Segment{} c := make(chan prioritizedSegments, len(mods)) diff --git a/powerline_test.go b/powerline_test.go new file mode 100644 index 00000000..fb3b7c0a --- /dev/null +++ b/powerline_test.go @@ -0,0 +1,34 @@ +package main + +import "testing" + +func Test_detectShell(t *testing.T) { + tests := []struct { + name string + want string + }{ + { + name: "", + want: "bare", + }, + { + name: "/bin/sh", + want: "bare", + }, { + name: "/bin/bash", + want: "bash", + }, { + name: "/usr/local/bin/bash5", + want: "bash", + }, { + name: "/usr/bin/zsh", + want: "zsh", + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := detectShell(tt.name); got != tt.want { + t.Errorf("detectShell(%q) = %q, want %q", tt.name, got, tt.want) + } + }) + } +} diff --git a/segment-hostname.go b/segment-hostname.go index 8d20b655..1f2f961e 100644 --- a/segment-hostname.go +++ b/segment-hostname.go @@ -44,9 +44,9 @@ func segmentHost(p *powerline) []pwl.Segment { foreground = p.theme.HostnameColorizedFgMap[background] } } else { - if *p.args.Shell == "bash" { + if p.shell == "bash" { hostPrompt = "\\h" - } else if *p.args.Shell == "zsh" { + } else if p.shell == "zsh" { hostPrompt = "%m" } else { hostPrompt = getHostName(p.hostname) diff --git a/segment-termtitle.go b/segment-termtitle.go index b54230db..64bae055 100644 --- a/segment-termtitle.go +++ b/segment-termtitle.go @@ -19,9 +19,9 @@ func segmentTermTitle(p *powerline) []pwl.Segment { return []pwl.Segment{} } - if *p.args.Shell == "bash" { + if p.shell == "bash" { title = "\\[\\e]0;\\u@\\h: \\w\\a\\]" - } else if *p.args.Shell == "zsh" { + } else if p.shell == "zsh" { title = "%{\033]0;%n@%m: %~\007%}" } else { cwd := p.cwd diff --git a/segment-username.go b/segment-username.go index ca1f5e20..80c40332 100644 --- a/segment-username.go +++ b/segment-username.go @@ -6,9 +6,9 @@ import ( func segmentUser(p *powerline) []pwl.Segment { var userPrompt string - if *p.args.Shell == "bash" { + if p.shell == "bash" { userPrompt = "\\u" - } else if *p.args.Shell == "zsh" { + } else if p.shell == "zsh" { userPrompt = "%n" } else { userPrompt = p.username