Skip to content

Commit

Permalink
fix: use PWD for current dir
Browse files Browse the repository at this point in the history
On Windows, when in the registry, os.Getwd() returns the previous
path rather than the registry location. Settings PWD as an environment
variable might seem hacky but it's the only way to resolve this.

Resolves #40
  • Loading branch information
JanDeDobbeleer committed Oct 9, 2020
1 parent 0b0f3a7 commit 8978038
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/docs/installation.mdx
Expand Up @@ -83,7 +83,7 @@ Edit `$PROFILE` in your preferred PowerShell version and add the following lines
```powershell
[ScriptBlock]$Prompt = {
$realLASTEXITCODE = $global:LASTEXITCODE
& "C:\tools\oh-my-posh.exe" -config "~/downloadedtheme.json" -error $realLASTEXITCODE
& "C:\tools\oh-my-posh.exe" -config "~/downloadedtheme.json" -error $realLASTEXITCODE -pwd $PWD
$global:LASTEXITCODE = $realLASTEXITCODE
Remove-Variable realLASTEXITCODE -Confirm:$false
}
Expand Down
10 changes: 8 additions & 2 deletions environment.go
Expand Up @@ -44,12 +44,18 @@ func (env *environment) getenv(key string) string {
}

func (env *environment) getcwd() string {
correctPath := func(pwd string) string {
// on Windows, and being case sentisitive and not consistent and all, this gives silly issues
return strings.Replace(pwd, "c:", "C:", 1)
}
if *env.args.PWD != "" {
return correctPath(*env.args.PWD)
}
dir, err := os.Getwd()
if err != nil {
return ""
}
// on Windows, and being case sentisitive and not consistent and all, this gives silly issues
return strings.Replace(dir, "c:", "C:", 1)
return correctPath(dir)
}

func (env *environment) homeDir() string {
Expand Down
5 changes: 5 additions & 0 deletions main.go
Expand Up @@ -12,6 +12,7 @@ type args struct {
PrintConfig *bool
Config *string
Shell *string
PWD *string
}

func main() {
Expand All @@ -32,6 +33,10 @@ func main() {
"shell",
"",
"Override the shell you are working in"),
PWD: flag.String(
"pwd",
"",
"the path you are working in"),
}
flag.Parse()
env := &environment{
Expand Down
4 changes: 2 additions & 2 deletions packages/powershell/oh-my-posh/oh-my-posh.psm1
Expand Up @@ -52,7 +52,7 @@ function Set-PoshPrompt {
$realLASTEXITCODE = $global:LASTEXITCODE
$poshCommand = Get-PoshCommand
$config = $global:PoshSettings.Theme
& $poshCommand -config $config -error $realLASTEXITCODE
& $poshCommand -config $config -error $realLASTEXITCODE -pwd $PWD
$global:LASTEXITCODE = $realLASTEXITCODE
Remove-Variable realLASTEXITCODE -Confirm:$false
}
Expand All @@ -79,7 +79,7 @@ function Get-PoshThemes {
Write-Host ("=" * $consoleWidth)
Write-Host "$esc[1m$($_.BaseName)$esc[0m"
Write-Host ""
& $poshCommand -config $($_.FullName)
& $poshCommand -config $($_.FullName) -pwd $PWD
Write-Host ""
}
Write-Host ("=" * $consoleWidth)
Expand Down
9 changes: 0 additions & 9 deletions segment_git_test.go
Expand Up @@ -263,12 +263,3 @@ func TestParseGitBranchInfoBehindandAhead(t *testing.T) {
assert.Equal(t, "2", got["behind"])
assert.Equal(t, "1", got["ahead"])
}

func TestGetGitStatus(t *testing.T) {
env := new(environment)
git := git{
env: env,
}
git.getGitStatus()
assert.NotEmpty(t, git.repo)
}

0 comments on commit 8978038

Please sign in to comment.