From d63f2a4e11b4e8b3589a0d6efbb3cb729cbab357 Mon Sep 17 00:00:00 2001 From: Janne Koschinski Date: Thu, 23 Apr 2020 15:56:38 +0200 Subject: [PATCH] Improved cross-platform path handling --- powerline.go | 41 +++++++++++++++++++++++++++++------------ segment-cwd.go | 10 ++++------ segment-hostname.go | 7 +++---- segment-termtitle.go | 11 +---------- segment-username.go | 9 ++------- 5 files changed, 39 insertions(+), 39 deletions(-) diff --git a/powerline.go b/powerline.go index d7cda3a4..66f8b315 100644 --- a/powerline.go +++ b/powerline.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "os" + "os/user" "strconv" "strings" "sync" @@ -28,18 +29,21 @@ type ShellInfo struct { } type powerline struct { - args args - cwd string - pathAliases map[string]string - theme Theme - shellInfo ShellInfo - reset string - symbolTemplates Symbols - priorities map[string]int - ignoreRepos map[string]bool - Segments [][]pwl.Segment - curSegment int - align alignment + args args + cwd string + userInfo user.User + hostname string + username string + pathAliases map[string]string + theme Theme + shellInfo ShellInfo + reset string + symbolTemplates Symbols + priorities map[string]int + ignoreRepos map[string]bool + Segments [][]pwl.Segment + curSegment int + align alignment rightPowerline *powerline appendEastAsianPadding int } @@ -53,6 +57,19 @@ func newPowerline(args args, cwd string, priorities map[string]int, align alignm p := new(powerline) p.args = args p.cwd = cwd + userInfo, err := user.Current() + if userInfo != nil && err == nil { + p.userInfo = *userInfo + } + p.hostname, _ = os.Hostname() + + hostnamePrefix := fmt.Sprintf("%s%c", p.hostname, os.PathSeparator) + if strings.HasPrefix(p.userInfo.Username, hostnamePrefix) { + p.username = p.userInfo.Username[len(hostnamePrefix):] + } else { + p.username = p.userInfo.Username + } + p.theme = themes[*args.Theme] p.shellInfo = shellInfos[*args.Shell] p.reset = fmt.Sprintf(p.shellInfo.colorTemplate, "[0m") diff --git a/segment-cwd.go b/segment-cwd.go index 34cb4cc4..4b04c78a 100644 --- a/segment-cwd.go +++ b/segment-cwd.go @@ -105,13 +105,12 @@ func cwdToPathSegments(p *powerline, cwd string) []pathSegment { pathSeparator := string(os.PathSeparator) pathSegments := make([]pathSegment, 0) - home, _ := os.LookupEnv("HOME") - if strings.HasPrefix(cwd, home) { + if strings.HasPrefix(cwd, p.userInfo.HomeDir) { pathSegments = append(pathSegments, pathSegment{ path: "~", home: true, }) - cwd = cwd[len(home):] + cwd = cwd[len(p.userInfo.HomeDir):] } else if cwd == pathSeparator { pathSegments = append(pathSegments, pathSegment{ path: pathSeparator, @@ -163,9 +162,8 @@ func segmentCwd(p *powerline) (segments []pwl.Segment) { cwd := p.cwd if *p.args.CwdMode == "plain" { - home, _ := os.LookupEnv("HOME") - if strings.HasPrefix(cwd, home) { - cwd = "~" + cwd[len(home):] + if strings.HasPrefix(cwd, p.userInfo.HomeDir) { + cwd = "~" + cwd[len(p.userInfo.HomeDir):] } segments = append(segments, pwl.Segment{ diff --git a/segment-hostname.go b/segment-hostname.go index 42e92815..03863729 100644 --- a/segment-hostname.go +++ b/segment-hostname.go @@ -8,8 +8,7 @@ import ( "strings" ) -func getHostName() string { - fullyQualifiedDomainName, _ := os.Hostname() +func getHostName(fullyQualifiedDomainName string) string { return strings.SplitN(fullyQualifiedDomainName, ".", 2)[0] } @@ -37,7 +36,7 @@ func segmentHost(p *powerline) []pwl.Segment { foreground = uint8(foregroundEnv) background = uint8(backgroundEnv) } else { - hostName := getHostName() + hostName := getHostName(p.hostname) hostPrompt = hostName hash := getMd5(hostName) @@ -50,7 +49,7 @@ func segmentHost(p *powerline) []pwl.Segment { } else if *p.args.Shell == "zsh" { hostPrompt = "%m" } else { - hostPrompt = getHostName() + hostPrompt = getHostName(p.hostname) } foreground = p.theme.HostnameFg diff --git a/segment-termtitle.go b/segment-termtitle.go index f40a7df0..b54230db 100644 --- a/segment-termtitle.go +++ b/segment-termtitle.go @@ -6,7 +6,6 @@ package main import ( "fmt" "os" - "os/user" "strings" pwl "github.com/justjanne/powerline-go/powerline" @@ -25,16 +24,8 @@ func segmentTermTitle(p *powerline) []pwl.Segment { } else if *p.args.Shell == "zsh" { title = "%{\033]0;%n@%m: %~\007%}" } else { - userName, found := os.LookupEnv("USER") - if !found { - userInfo, err := user.Current() - if err == nil { - userName = userInfo.Username - } - } - hostName, _ := os.Hostname() cwd := p.cwd - title = fmt.Sprintf("\033]0;%s@%s: %s\007", userName, hostName, cwd) + title = fmt.Sprintf("\033]0;%s@%s: %s\007", p.username, p.hostname, cwd) } return []pwl.Segment{{ diff --git a/segment-username.go b/segment-username.go index f4eec316..176dbb68 100644 --- a/segment-username.go +++ b/segment-username.go @@ -1,10 +1,8 @@ package main import ( - "os" - "os/user" - pwl "github.com/justjanne/powerline-go/powerline" + "os" ) func segmentUser(p *powerline) []pwl.Segment { @@ -14,10 +12,7 @@ func segmentUser(p *powerline) []pwl.Segment { } else if *p.args.Shell == "zsh" { userPrompt = "%n" } else { - userInfo, err := user.Current() - if err == nil { - userPrompt = userInfo.Username - } + userPrompt = p.username } var background uint8