Skip to content

Commit

Permalink
Improved cross-platform path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
justjanne committed Apr 23, 2020
1 parent e657540 commit d63f2a4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
41 changes: 29 additions & 12 deletions powerline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"os/user"
"strconv"
"strings"
"sync"
Expand All @@ -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
}
Expand All @@ -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")
Expand Down
10 changes: 4 additions & 6 deletions segment-cwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
7 changes: 3 additions & 4 deletions segment-hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"strings"
)

func getHostName() string {
fullyQualifiedDomainName, _ := os.Hostname()
func getHostName(fullyQualifiedDomainName string) string {
return strings.SplitN(fullyQualifiedDomainName, ".", 2)[0]
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
11 changes: 1 addition & 10 deletions segment-termtitle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"fmt"
"os"
"os/user"
"strings"

pwl "github.com/justjanne/powerline-go/powerline"
Expand All @@ -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{{
Expand Down
9 changes: 2 additions & 7 deletions segment-username.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main

import (
"os"
"os/user"

pwl "github.com/justjanne/powerline-go/powerline"
"os"
)

func segmentUser(p *powerline) []pwl.Segment {
Expand All @@ -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
Expand Down

0 comments on commit d63f2a4

Please sign in to comment.