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
This commit is contained in:
Jan De Dobbeleer 2020-10-08 19:23:20 +02:00 committed by Jan De Dobbeleer
parent 0b0f3a7fa6
commit 8978038a3c
5 changed files with 16 additions and 14 deletions

View file

@ -83,7 +83,7 @@ Edit `$PROFILE` in your preferred PowerShell version and add the following lines
```powershell ```powershell
[ScriptBlock]$Prompt = { [ScriptBlock]$Prompt = {
$realLASTEXITCODE = $global:LASTEXITCODE $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 $global:LASTEXITCODE = $realLASTEXITCODE
Remove-Variable realLASTEXITCODE -Confirm:$false Remove-Variable realLASTEXITCODE -Confirm:$false
} }

View file

@ -44,12 +44,18 @@ func (env *environment) getenv(key string) string {
} }
func (env *environment) getcwd() 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() dir, err := os.Getwd()
if err != nil { if err != nil {
return "" return ""
} }
// on Windows, and being case sentisitive and not consistent and all, this gives silly issues return correctPath(dir)
return strings.Replace(dir, "c:", "C:", 1)
} }
func (env *environment) homeDir() string { func (env *environment) homeDir() string {

View file

@ -12,6 +12,7 @@ type args struct {
PrintConfig *bool PrintConfig *bool
Config *string Config *string
Shell *string Shell *string
PWD *string
} }
func main() { func main() {
@ -32,6 +33,10 @@ func main() {
"shell", "shell",
"", "",
"Override the shell you are working in"), "Override the shell you are working in"),
PWD: flag.String(
"pwd",
"",
"the path you are working in"),
} }
flag.Parse() flag.Parse()
env := &environment{ env := &environment{

View file

@ -52,7 +52,7 @@ function Set-PoshPrompt {
$realLASTEXITCODE = $global:LASTEXITCODE $realLASTEXITCODE = $global:LASTEXITCODE
$poshCommand = Get-PoshCommand $poshCommand = Get-PoshCommand
$config = $global:PoshSettings.Theme $config = $global:PoshSettings.Theme
& $poshCommand -config $config -error $realLASTEXITCODE & $poshCommand -config $config -error $realLASTEXITCODE -pwd $PWD
$global:LASTEXITCODE = $realLASTEXITCODE $global:LASTEXITCODE = $realLASTEXITCODE
Remove-Variable realLASTEXITCODE -Confirm:$false Remove-Variable realLASTEXITCODE -Confirm:$false
} }
@ -79,7 +79,7 @@ function Get-PoshThemes {
Write-Host ("=" * $consoleWidth) Write-Host ("=" * $consoleWidth)
Write-Host "$esc[1m$($_.BaseName)$esc[0m" Write-Host "$esc[1m$($_.BaseName)$esc[0m"
Write-Host "" Write-Host ""
& $poshCommand -config $($_.FullName) & $poshCommand -config $($_.FullName) -pwd $PWD
Write-Host "" Write-Host ""
} }
Write-Host ("=" * $consoleWidth) Write-Host ("=" * $consoleWidth)

View file

@ -263,12 +263,3 @@ func TestParseGitBranchInfoBehindandAhead(t *testing.T) {
assert.Equal(t, "2", got["behind"]) assert.Equal(t, "2", got["behind"])
assert.Equal(t, "1", got["ahead"]) 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)
}