powershell

powershell script to show git info and just the working directory


I've done some googling and havent found a solution so I was hoping for some help here.

Ive installed posh-git to use with powershell and want to keep the git status it adds to the prompt line, but I dont want the entire working directory printed because its annoying. I just want the current folder. Ive found the code to do each, but have no idea how to combine them.

The default posh-git to print path and git info is

function global:prompt {
$realLASTEXITCODE = $LASTEXITCODE

# Reset color, which can be messed up by Enable-GitColors
$Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor

Write-Host($pwd.ProviderPath) -nonewline

Write-VcsStatus

$global:LASTEXITCODE = $realLASTEXITCODE
return "> "
}

the code to just print the current folder in the path is

function prompt { 
'PS ' + ($pwd -split '\\')[0]+' '+$(($pwd -split '\\')[-1] -join '\') + '> '
}

Ive tried various mishmashes of both and havent figured out how to make it do what I want.

heres a pic to help illustrate

enter image description here

any help would be great, thank you :)


Solution

  • Instead of printing $pwd.ProviderPath, print Split-Path -Leaf:

    function global:prompt {
        $realLASTEXITCODE = $LASTEXITCODE
    
        # Reset color, which can be messed up by Enable-GitColors
        $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor
    
        Write-Host(Split-Path -Leaf $pwd.ProviderPath) -nonewline
    
        Write-VcsStatus
    
        $global:LASTEXITCODE = $realLASTEXITCODE
        return "> "
    }