Skip to content

Commit

Permalink
Autodetect shell by default
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Jan 6, 2021
1 parent c8466d2 commit 7e983c0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 21 additions & 1 deletion powerline.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/user"
"path"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -37,6 +38,7 @@ type powerline struct {
username string
pathAliases map[string]string
theme Theme
shell string
shellInfo ShellInfo
reset string
symbolTemplates Symbols
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
34 changes: 34 additions & 0 deletions powerline_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
4 changes: 2 additions & 2 deletions segment-hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions segment-termtitle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions segment-username.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7e983c0

Please sign in to comment.