From 8978038a3cba3298abe352a20964e234532a1a23 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 8 Oct 2020 19:23:20 +0200 Subject: [PATCH] fix: use PWD for current dir 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 --- docs/docs/installation.mdx | 2 +- environment.go | 10 ++++++++-- main.go | 5 +++++ packages/powershell/oh-my-posh/oh-my-posh.psm1 | 4 ++-- segment_git_test.go | 9 --------- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/docs/installation.mdx b/docs/docs/installation.mdx index 284d183b..0ad2cb35 100644 --- a/docs/docs/installation.mdx +++ b/docs/docs/installation.mdx @@ -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 } diff --git a/environment.go b/environment.go index 3386b947..90ae1ac6 100755 --- a/environment.go +++ b/environment.go @@ -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 { diff --git a/main.go b/main.go index 0560ffdf..9b7be0c8 100755 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ type args struct { PrintConfig *bool Config *string Shell *string + PWD *string } func main() { @@ -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{ diff --git a/packages/powershell/oh-my-posh/oh-my-posh.psm1 b/packages/powershell/oh-my-posh/oh-my-posh.psm1 index fdf3ebf7..740d140e 100644 --- a/packages/powershell/oh-my-posh/oh-my-posh.psm1 +++ b/packages/powershell/oh-my-posh/oh-my-posh.psm1 @@ -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 } @@ -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) diff --git a/segment_git_test.go b/segment_git_test.go index 9e21c7b1..14bc3d9e 100755 --- a/segment_git_test.go +++ b/segment_git_test.go @@ -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) -}