Skip to content

Instantly share code, notes, and snippets.

@piotrgredowski
Last active August 7, 2023 12:55
Show Gist options
  • Save piotrgredowski/6b7f3895b691e99df2556948a19e7bf2 to your computer and use it in GitHub Desktop.
Save piotrgredowski/6b7f3895b691e99df2556948a19e7bf2 to your computer and use it in GitHub Desktop.
My powershell prompt
######### MY PROMPT START
$SHOW_GIT_PROMPT = 0
function get_git_branch {
# Because git is crazy slow on my Windows, I need to do sth like below
$script = @'
import pathlib as p
import sys
counter = 0
file_path = p.WindowsPath('.git', 'HEAD')
while counter < 5:
try:
f = open(file_path)
except FileNotFoundError:
file_path = p.WindowsPath('..') / file_path
counter += 1
continue
content = f.read()
parts = content.split('ref: refs/heads/')
if len(parts) > 1:
print(parts[-1], end='')
break
sys.exit(0)
'@
return python -c $script
}
function prompt {
if ($UserType -eq "Admin") {
$host.UI.RawUI.WindowTitle = "" + $(get-location) + " : Admin"
$host.UI.RawUI.ForegroundColor = "white"
}
else {
$host.ui.rawui.WindowTitle = $(get-location)
}
Write-Host($specialChar1)
Write-Host ("PS") -nonewline
$folder_icon = [Char]0xe5ff
$arrow_icon = [Char]0x25ba
$diamond_icon = [Char]0x2666
$spacer = [Char]0x2771
Write-Host (" $spacer ") -nonewline -foregroundcolor Magenta
Write-Host ($(get-location)) -nonewline
Write-Host (" $spacer ") -NoNewline -ForegroundColor Magenta
Write-Host (Get-Date -Format "ddd HH:mm:ss") -NoNewline -ForegroundColor Yellow
Write-Host (" $spacer") -NoNewline -ForegroundColor Magenta
if ($SHOW_GIT_PROMPT -eq 1) {
$symbolicref = git symbolic-ref HEAD
if ($NULL -ne $symbolicref) {
Write-Host (" [") -NoNewline -ForegroundColor Cyan
Write-Host ($symbolicref.substring($symbolicref.LastIndexOf("/") + 1)) -NoNewline -ForegroundColor Blue
$differences = (git diff-index --name-status HEAD)
if ($NULL -ne $differences) {
$git_update_count = [regex]::matches($differences, "M`t").count
$git_create_count = [regex]::matches($differences, "A`t").count
$git_delete_count = [regex]::matches($differences, "D`t").count
Write-Host (" (") -NoNewline -ForegroundColor Magenta
Write-Host ("c:" + $git_create_count) -NoNewline -ForegroundColor Green
Write-Host (" u:" + $git_update_count) -NoNewline -ForegroundColor Blue
Write-Host (" d:" + $git_delete_count) -NoNewline -ForegroundColor Red
Write-Host (")") -NoNewline -ForegroundColor Magenta
}
Write-Host ("]") -NoNewline -ForegroundColor Cyan
}
} else {
$branch_name = get_git_branch
if ($branch_name -ne '') {
Write-Host (" [") -NoNewline -ForegroundColor Cyan
Write-Host (get_git_branch) -NoNewline -ForegroundColor Blue
Write-Host ("]") -NoNewline -ForegroundColor Cyan
}
}
if ($LASTEXITCODE -ne 0) {
Write-Host (" exited ") -NoNewline
Write-Host ($LASTEXITCODE) -NoNewline -ForegroundColor Red
}
Write-Host ("")
Write-Host ("$arrow_icon") -nonewline -foregroundcolor cyan
return " "
}
######### MY PROMPT END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment