Created
June 8, 2024 20:07
-
-
Save arturslab/1e6e2a682c55f18feeee8dcfbd9e15fc to your computer and use it in GitHub Desktop.
PowerShell custom prompt with some informations about GIT commits, npm and node version. More details on https://melma.pl/blog/powershell-prompt-i-git/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-Date { | |
$date = Get-Date -Format "dddd MM/dd/yyyy HH:mm" | |
Write-Host "$date " -BackgroundColor "black" -ForegroundColor "yellow" | |
} | |
# Print current version of node.js | |
function Write-NodeVer { | |
# This try gracefully handles when node isn't available | |
try { | |
$iconNode = "" # icon node | |
$verNode = node -v | |
Write-Host " $iconNode $verNode " -NoNewline -BackgroundColor "green" -ForegroundColor "black" | |
} | |
catch {} | |
} | |
# Print current version of npm | |
function Write-NpmVer { | |
# This try gracefully handles when npm isn't available | |
try { | |
$iconNpm = "" # icon npm | |
$verNpm = npm -v | |
Write-Host " $iconNpm v$verNpm " -NoNewline -BackgroundColor "red" -ForegroundColor "black" | |
} | |
catch {} | |
} | |
# Print GIT branch and changes | |
function Write-GitInfo () { | |
try { | |
$iconBranch = "" # icon branch | |
$iconAhead = "" # icon arrow up | |
$iconBehind = "" # icon arrow down | |
$branch = git rev-parse --abbrev-ref HEAD | |
Write-Host "`n" -NoNewline | |
if ($branch -eq "HEAD") { | |
# we're probably in detached HEAD state, so print the SHA | |
$branch = git rev-parse --short HEAD | |
Write-Host "$iconBranch $branch" -NoNewline -ForegroundColor "red" | |
} | |
else { | |
# we're on an actual branch, so print it | |
Write-Host "$iconBranch $branch" -NoNewline -ForegroundColor "blue" | |
} | |
Write-Host " [" -NoNewline -ForegroundColor "blue" | |
$ahead = $(git rev-list --count origin/$branch..$branch) | |
$behind = $(git rev-list --count $branch..origin/$branch) | |
if ($ahead -gt 0) { | |
Write-Host " $iconAhead $ahead " -NoNewline -ForegroundColor "green" | |
} | |
if ($behind -gt 0) { | |
Write-Host " $iconBehind $behind " -NoNewline -ForegroundColor "red" | |
} | |
Write-GitChangesInfo | |
Write-Host "]" -NoNewline -ForegroundColor "blue" | |
} catch { | |
# we'll end up here if we're in a newly initiated git repo | |
Write-Host "`n$iconBranch no branches yet" -NoNewline -ForegroundColor "yellow" | |
} | |
} | |
# Function which prints all changes in git repository | |
function Write-GitChangesInfo () { | |
$iconRemoved = "✗" # icon cross | |
$iconAdded = "✚" # icon plus | |
$iconModified = "" # icon pencil ✎ | |
$iconUntracked = "…" # icon ellipsis | |
$iconUncommitted = "" # icon arrow up | |
$iconNoChanges = "✓" # icon checkmark | |
$gitStatusList = git status --porcelain | |
$ModifiedFilesCount = $gitStatusList | Where {$_ -match '^ M'} | Measure-Object | Select-Object -expand Count | |
$AddedFilesCount = $gitStatusList | Where {$_ -match '^A'} | Measure-Object | Select-Object -expand Count | |
$RemovedFilesCount = $gitStatusList | Where {$_ -match '^D'} | Measure-Object | Select-Object -expand Count | |
$UntrackedFilesCount = $gitStatusList | Where {$_ -match '^\?\?'} | Measure-Object | Select-Object -expand Count | |
# Show only if there aren't any changes | |
if ($ModifiedFilesCount -eq 0 -and $AddedFilesCount -eq 0 -and $RemovedFilesCount -eq 0 -and $UntrackedFilesCount -eq 0) | |
{ | |
Write-Host " $iconNoChanges " -NoNewline -ForegroundColor "green" | |
return | |
} | |
if ($ModifiedFilesCount -gt 0) | |
{ | |
Write-Host " $iconModified $ModifiedFilesCount " -NoNewline -ForegroundColor "blue" | |
} | |
if ($AddedFilesCount -gt 0) | |
{ | |
Write-Host " $iconAdded $AddedFilesCount " -NoNewline -ForegroundColor "green" | |
} | |
if ($RemovedFilesCount -gt 0) | |
{ | |
Write-Host " $iconRemoved $RemovedFilesCount " -NoNewline -ForegroundColor "red" | |
} | |
if ($UntrackedFilesCount -gt 0) | |
{ | |
Write-Host " $iconUntracked $UntrackedFilesCount " -NoNewline -ForegroundColor "yellow" | |
} | |
} | |
# Check if parent directory contains .git folder | |
function Check-ParentGit { | |
$gitPaths = @(".git", "..\.git", "..\..\.git", "..\..\..\.git", "..\..\..\..\.git", "..\..\..\..\..\.git", "..\..\..\..\..\..\.git", "..\..\..\..\..\..\..\.git") | |
foreach ($path in $gitPaths) { | |
if (Test-Path $path) { | |
return $true | |
} | |
} | |
return $false | |
} | |
# Print some informations before prompt | |
Write-Date | |
Write-NodeVer | |
Write-NpmVer | |
Write-Host "`n" -NoNewline | |
# Custom prompt function | |
function prompt { | |
$base = " " # icon folder | |
$path = "$($executionContext.SessionState.Path.CurrentLocation)" | |
$userPrompt = "$('>' * ($nestedPromptLevel + 1)) " | |
Write-Host "`n$base" -NoNewline -ForegroundColor "green" | |
$isGitRepo = Check-ParentGit | |
if ($isGitRepo) { | |
Write-Host $path -NoNewline -ForegroundColor "green" | |
Write-GitInfo | |
} | |
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