Skip to content

Instantly share code, notes, and snippets.

@arthurcohen
Created February 10, 2020 19:32
Show Gist options
  • Save arthurcohen/a321c28345127de6caffc08d693c1952 to your computer and use it in GitHub Desktop.
Save arthurcohen/a321c28345127de6caffc08d693c1952 to your computer and use it in GitHub Desktop.
Powershell script to create a powerline with relative path of the current console, git repo information and time.
$global:foregroundColor = 'white'
$time = Get-Date
$psVersion= $host.Version.Major
$curUser= (Get-ChildItem Env:\USERNAME).Value
$curComp= (Get-ChildItem Env:\COMPUTERNAME).Value
$showGitInfo = false
clear
Write-Host "- PowerShell V$psVersion -"
Write-Host
function relativePathToHome{
$currentPath = (Get-Location).Path
$relativePath = $currentPath -replace ($home -replace "\\", "\\"), "~"
return $relativePath
}
function Prompt {
$prompt_user_text = "White"
$prompt_user_background = "Blue"
$prompt_time_text = "Black"
$prompt_time_background = "Gray"
$prompt_text = "White"
$prompt_background = "Blue"
$prompt_git_background = "Yellow"
$prompt_git_text = "Black"
$git_string = "";
if ($showGitInfo) {
# Grab Git Branch
git branch | foreach {
if ($_ -match "^\* (.*)"){
$git_string += $matches[1]
}
}
$to_push = (git rev-list origin..HEAD).Count
$to_pull = (git rev-list HEAD..origin).Count
$changed = 0
$added = 0
$untracked = 0
$deleted = 0
# Grab Git Status
$git_status = "";
git status --porcelain | foreach {
if ($_[0] -eq "A" -or $_[1] -eq "A") {
$added++
} elseif ($_[0] -eq "M" -or $_[1] -eq "M") {
$changed++
} elseif ($_[0] -eq "D" -or $_[1] -eq "D") {
$deleted++
}elseif ($_[0] -eq "?" -or $_[1] -eq "?") {
$untracked++
}
$git_status = $_ #just replace other wise it will be empty
}
if ($added -eq 0 -and $deleted -eq 0 -and $changed -eq 0 -and $untracked -eq 0) {
$prompt_git_text = "White"
$prompt_git_background = "DarkGreen"
}
}
$curtime = Get-Date
# $drive = (PWD).Drive.Name
$path = Split-Path (PWD) -Leaf
$relativePath = relativePathToHome
Write-Host -NoNewLine (" {0:HH}:{0:mm}:{0:ss} " -f (Get-Date)) -foregroundColor $prompt_time_text -backgroundColor $prompt_time_background
Write-Host -NoNewLine "$([char]57520)" -foregroundColor $prompt_time_background -backgroundColor $prompt_background
# Write-Host " $path " -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
Write-Host " $relativePath " -foregroundColor $prompt_text -backgroundColor $prompt_background -NoNewLine
if ($git_string){
Write-Host "$([char]57520)" -foregroundColor $prompt_background -NoNewLine -backgroundColor $prompt_git_background
Write-Host " $([char]57504) " -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background -NoNewLine
Write-Host "$git_string " -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
if ($added -gt 0) {
Write-Host "+$added" -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
}
if ($changed -gt 0) {
Write-Host "*$changed" -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
}
if ($untracked -gt 0) {
Write-Host "%$untracked" -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
}
if ($deleted -gt 0) {
Write-Host "×$deleted" -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
}
if ($to_push -gt 0) {
Write-Host " ↑$to_push" -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
}
if ($to_pull -gt 0) {
Write-Host "↓$to_pull " -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
}
if ($added -eq 0 -and $deleted -eq 0 -and $changed -eq 0 -and $untracked -eq 0) {
Write-Host "√" -NoNewLine -foregroundColor $prompt_git_text -backgroundColor $prompt_git_background
}
Write-Host -NoNewLine "$([char]57520)" -foregroundColor $prompt_git_background
}
else{
Write-Host -NoNewLine "$([char]57520)" -foregroundColor $prompt_background
}
Write-Host
Write-Host -NoNewLine ">"
Return " "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment