Skip to content

Instantly share code, notes, and snippets.

@tamuhey
Created April 9, 2019 02:52
Show Gist options
  • Save tamuhey/3d1f2054602d834593dd13d4a0aafeaa to your computer and use it in GitHub Desktop.
Save tamuhey/3d1f2054602d834593dd13d4a0aafeaa to your computer and use it in GitHub Desktop.
powershell prompt with git branch name and conda env name
# https://stackoverflow.com/a/44411205/10051099
function Write-BranchName () {
try {
$branch = git rev-parse --abbrev-ref HEAD
if ($branch -eq "HEAD") {
# we're probably in detached HEAD state, so print the SHA
$branch = git rev-parse --short HEAD
Write-Host " ($branch)" -ForegroundColor "red"
}
else {
# we're on an actual branch, so print it
Write-Host " ($branch)" -ForegroundColor "cyan"
}
} catch {
# we'll end up here if we're in a newly initiated git repo
Write-Host " (no branches yet)" -ForegroundColor "yellow"
}
}
function prompt {
$base = "PS ($env:CONDA_DEFAULT_ENV) "
$path = "$($executionContext.SessionState.Path.CurrentLocation)"
$userPrompt = "$('>' * ($nestedPromptLevel + 1)) "
Write-Host "`n$base" -NoNewline
if (Test-Path .git) {
Write-Host $path -NoNewline -ForegroundColor "green"
Write-BranchName
}
else {
# we're not in a repo so don't bother displaying branch name/sha
Write-Host $path -ForegroundColor "green"
}
return $userPrompt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment