Last active
March 21, 2024 07:16
-
-
Save SQLDBAWithABeard/41db1ffaebbb3a5238157c0fa8640678 to your computer and use it in GitHub Desktop.
new improved colourful prompt
This file contains 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
######## POSH-GIT | |
# with props to https://bradwilson.io/blog/prompt/powershell | |
# | |
# | |
# Now for this to work most beautifully - you will need to have the latest posh-git module | |
# | |
# You will also need the MesloLGM Nerd Font Mono font | |
# from here https://github.com/ryanoasis/nerd-fonts/blob/master/patched-fonts/Meslo/M/Regular/complete/Meslo%20LG%20M%20Regular%20Nerd%20Font%20Complete.ttf | |
# if you are super nerdy | |
# or | |
# https://www.nerdfonts.com/ | |
# | |
# Then add in VS Code settings | |
# | |
# "terminal.integrated.fontFamily": "'MesloLGM Nerd Font Mono'", | |
# "editor.fontFamily": "'MesloLGM Nerd Font Mono' ", | |
# | |
# Use these to alter what is shown either dynamically at the command line or in the profile | |
# | |
# $ShowError = $false | |
# $ShowKube = $false | |
# $ShowAzure = $false | |
# $ShowAzureCli = $false | |
# $ShowGit = $true | |
# $ShowPath = $true | |
# $ShowDate = $true | |
# $ShowTime = $true | |
# $ShowUser = $true | |
# $ShowCountDown = $false | |
# $CountDownMessage = "Set `$CountDownMessage and `$CountDownEndDate Rob" | |
# $CountDownEndDate = 0 | |
Import-Module posh-git | |
switch ($ENV:COMPUTERNAME) { | |
'BEARD-DESKTOP' { | |
$GitRoot = 'G:\OneDrive\Documents\GitHub' | |
} | |
'JumpBox' { | |
$GitRoot = 'C:\Git' | |
} | |
Default { | |
$GitRoot = 'C:\Users\mrrob\OneDrive\Documents\GitHub' | |
} | |
} | |
if (-not (Get-PSDrive -Name Presentations -ErrorAction SilentlyContinue)) { | |
$Error.Clear() | |
$Path = "$GitRoot\Presentations" | |
$null = New-PSDrive -Name Presentations -PSProvider FileSystem -Root $Path | |
} | |
if (-not (Get-PSDrive -Name dockercompose -ErrorAction SilentlyContinue)) { | |
$Error.Clear() | |
$Path = "$GitRoot\DockerStuff" | |
$null = New-PSDrive -Name dockercompose -PSProvider FileSystem -Root $Path | |
} | |
# Set-Location Git:\Presentations | |
$ShowError = $false | |
$ShowKube = $false | |
$ShowAzure = $false | |
$ShowAzureCli = $false | |
$ShowGit = $true | |
$ShowPath = $true | |
$ShowDate = $true | |
$ShowTime = $true | |
$ShowUser = $true | |
$ShowCountDown = $false | |
$CountDownMessage = "Set `$CountDownMessage and `$CountDownEndDate Rob" | |
$CountDownEndDate = 0 | |
# Background colors | |
$GitPromptSettings.AfterStash.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.AfterStatus.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BeforeIndex.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BeforeStash.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BeforeStatus.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BranchAheadStatusSymbol.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BranchBehindAndAheadStatusSymbol.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BranchBehindStatusSymbol.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BranchColor.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BranchGoneStatusSymbol.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.BranchIdenticalStatusSymbol.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.DefaultColor.BackgroundColor = [ConsoleColor]::DarkCyan | |
$GitPromptSettings.DelimStatus.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.ErrorColor.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.IndexColor.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.LocalDefaultStatusSymbol.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.LocalStagedStatusSymbol.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.LocalWorkingStatusSymbol.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.StashColor.BackgroundColor = [ConsoleColor]::DarkGray | |
$GitPromptSettings.WorkingColor.BackgroundColor = [ConsoleColor]::DarkGray | |
# Foreground colors | |
$GitPromptSettings.AfterStatus.ForegroundColor = [ConsoleColor]::Blue | |
$GitPromptSettings.BeforeStatus.ForegroundColor = [ConsoleColor]::Blue | |
$GitPromptSettings.BranchColor.ForegroundColor = [ConsoleColor]::White | |
$GitPromptSettings.BranchGoneStatusSymbol.ForegroundColor = [ConsoleColor]::Blue | |
$GitPromptSettings.BranchIdenticalStatusSymbol.ForegroundColor = [ConsoleColor]::Blue | |
$GitPromptSettings.DefaultColor.ForegroundColor = [ConsoleColor]::White | |
$GitPromptSettings.DelimStatus.ForegroundColor = [ConsoleColor]::Blue | |
$GitPromptSettings.IndexColor.ForegroundColor = [ConsoleColor]::Cyan | |
$GitPromptSettings.WorkingColor.ForegroundColor = [ConsoleColor]::Yellow | |
$GitPromptSettings.BranchBehindStatusSymbol.ForegroundColor = [ConsoleColor]::Black | |
$GitPromptSettings.LocalWorkingStatusSymbol.ForegroundColor = [ConsoleColor]::Black | |
# Prompt shape | |
$GitPromptSettings.AfterStatus.Text = " " | |
$GitPromptSettings.BeforeStatus.Text = " " | |
$GitPromptSettings.BranchAheadStatusSymbol.Text = " " | |
$GitPromptSettings.BranchBehindStatusSymbol.Text = " " | |
$GitPromptSettings.BranchGoneStatusSymbol.Text = "" | |
$GitPromptSettings.BranchBehindAndAheadStatusSymbol.Text = "" | |
$GitPromptSettings.BranchIdenticalStatusSymbol.Text = "" | |
$GitPromptSettings.BranchUntrackedText = "" | |
$GitPromptSettings.DelimStatus.Text = " ॥" | |
$GitPromptSettings.EnableStashStatus = $false | |
$GitPromptSettings.ShowStatusWhenZero = $false | |
######## PROMPT | |
set-content Function:prompt { | |
if ($ShowDate) { | |
Write-Host " $(Get-Date -Format "ddd dd MMM HH:mm:ss")" -ForegroundColor Black -BackgroundColor DarkGray -NoNewline | |
} | |
# Reset the foreground color to default | |
$Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultColor.ForegroundColor | |
if($ShowUser){ | |
Write-Host " " -NoNewLine | |
Write-Host " " -NoNewLine -BackgroundColor DarkYellow -ForegroundColor Black | |
Write-Host (whoami) -NoNewLine -BackgroundColor DarkYellow -ForegroundColor Black | |
} | |
# Write ERR for any PowerShell errors | |
if ($ShowError) { | |
if ($Error.Count -ne 0) { | |
Write-Host " " -NoNewLine | |
Write-Host " $($Error.Count) ERR " -NoNewLine -BackgroundColor DarkRed -ForegroundColor Yellow | |
# $Error.Clear() | |
} | |
} | |
# Write non-zero exit code from last launched process | |
if ($LASTEXITCODE -ne "") { | |
Write-Host " " -NoNewLine | |
Write-Host " x $LASTEXITCODE " -NoNewLine -BackgroundColor DarkRed -ForegroundColor Yellow | |
$LASTEXITCODE = "" | |
} | |
if ($ShowKube) { | |
# Write the current kubectl context | |
if ((Get-Command "kubectl" -ErrorAction Ignore) -ne $null) { | |
$currentContext = (& kubectl config current-context 2> $null) | |
$nodes = kubectl get nodes -o json | ConvertFrom-Json | |
$nodename = ($nodes.items.metadata |Where labels -like '*master*').name | |
Write-Host " " -NoNewLine | |
Write-Host "" -NoNewLine -BackgroundColor DarkGray -ForegroundColor Green | |
#Write-Host " $currentContext " -NoNewLine -BackgroundColor DarkYellow -ForegroundColor Black | |
Write-Host " $([char]27)[38;5;112;48;5;242m $([char]27)[38;5;254m$currentContext - $nodename $([char]27)[0m" -NoNewLine | |
} | |
} | |
if ($ShowAzure) { | |
$context = Get-AzContext | |
Write-Host " " -NoNewLine | |
Write-Host "" -NoNewLine -BackgroundColor DarkCyan -ForegroundColor Yellow | |
Write-Host "$([char]27)[38;5;231;48;5;27m ﴃ " -NoNewLine -BackgroundColor DarkBlue -ForegroundColor Yellow | |
Write-Host "$([char]27)[38;5;227;48;5;30m $($context.Account.Id) in $($context.subscription.name) $([char]27)[0m" -NoNewLine -BackgroundColor DarkBlue -ForegroundColor Yellow | |
} | |
if ($ShowAzureCli) { | |
# Write the current public cloud Azure CLI subscription | |
# NOTE: You will need sed from somewhere (for example, from Git for Windows) | |
if (Test-Path ~/.azure/clouds.config) { | |
if ((Get-Command "sed" -ErrorAction Ignore) -ne $null) { | |
$currentSub = & sed -nr "/^\[AzureCloud\]/ { :l /^subscription[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" ~/.azure/clouds.config | |
} | |
else { | |
$file = Get-Content ~/.azure/clouds.config | |
$currentSub = ([regex]::Matches($file, '^.*subscription\s=\s(.*)').Groups[1].Value).Trim() | |
} | |
if ($null -ne $currentSub) { | |
$currentAccount = (Get-Content ~/.azure/azureProfile.json | ConvertFrom-Json).subscriptions | Where-Object { $_.id -eq $currentSub } | |
if ($null -ne $currentAccount) { | |
Write-Host " " -NoNewLine | |
Write-Host "" -NoNewLine -BackgroundColor DarkCyan -ForegroundColor Yellow | |
$currentAccountName = ($currentAccount.Name.Split(' ') | Foreach { $_[0..5] -join '' }) -join ' ' | |
Write-Host "$([char]27)[38;5;227;48;5;30m ﴃ " -NoNewLine -BackgroundColor DarkBlue -ForegroundColor Yellow | |
Write-Host "$([char]27)[38;5;227;48;5;30m $($currentAccount.name) $([char]27)[0m" -NoNewLine -BackgroundColor DarkBlue -ForegroundColor Yellow | |
} | |
} | |
} | |
} | |
if ($ShowGit) { | |
# Write the current Git information | |
if ((Get-Command "Get-GitDirectory" -ErrorAction Ignore) -ne $null) { | |
if (Get-GitDirectory -ne $null) { | |
Write-Host (Write-VcsStatus) -NoNewLine | |
} | |
} | |
} | |
if ($ShowPath) { | |
# Write the current directory, with home folder normalized to ~ | |
# $currentPath = (get-location).Path.replace($home, "~") | |
# $idx = $currentPath.IndexOf("::") | |
# if ($idx -gt -1) { $currentPath = $currentPath.Substring($idx + 2) } | |
if ($IsLinux) { | |
$currentPath = $($pwd.path.Split('/')[-2..-1] -join '/') | |
} | |
else { | |
$currentPath = $($pwd.path.Split('\')[-2..-1] -join '\') | |
} | |
Write-Host " " -NoNewLine | |
Write-Host "$([char]27)[38;5;227;48;5;28m $([char]27)[38;5;254m$currentPath $([char]27)[0m " -NoNewline -BackgroundColor DarkGreen -ForegroundColor Black | |
} | |
# Reset LASTEXITCODE so we don't show it over and over again | |
$global:LASTEXITCODE = 0 | |
if ($ShowTime) { | |
try { | |
Write-Host " " -NoNewline | |
$history = Get-History -ErrorAction Ignore | |
if ($history) { | |
if (([System.Management.Automation.PSTypeName]'Sqlcollaborative.Dbatools.Utility.DbaTimeSpanPretty').Type) { | |
$timemessage = "" + ( [Sqlcollaborative.Dbatools.Utility.DbaTimeSpanPretty]($history[-1].EndExecutionTime - $history[-1].StartExecutionTime)) | |
Write-Host $timemessage -ForegroundColor DarkYellow -BackgroundColor DarkGray -NoNewline | |
} | |
else { | |
Write-Host " $([Math]::Round(($history[-1].EndExecutionTime - $history[-1].StartExecutionTime).TotalMilliseconds,2))" -ForegroundColor DarkYellow -BackgroundColor DarkGray -NoNewline | |
} | |
} | |
Write-Host " " -ForegroundColor DarkBlue -NoNewline | |
} | |
catch { } | |
} | |
# Write one + for each level of the pushd stack | |
if ((get-location -stack).Count -gt 0) { | |
Write-Host " " -NoNewLine | |
Write-Host (("+" * ((get-location -stack).Count))) -NoNewLine -ForegroundColor Cyan | |
} | |
# Newline | |
Write-Host "" | |
if ($ShowCountDown) { | |
$Date = Get-Date | |
$Mins = ($CountDownEndDate - $Date).TotalMinutes | |
Write-Host $CountDownMessage -ForegroundColor DarkGreen -NoNewline | |
switch ($Mins) { | |
{ $_ -ge 30 } { | |
$ToGo = [Math]::Round($mins, 1) | |
$Time = $Date.ToShortTimeString() | |
Write-Host " $Time $ToGo Mins to go" -ForegroundColor DarkGreen -NoNewline | |
} | |
{ $_ -lt 30 -and $_ -gt 10 } { | |
$ToGo = [Math]::Round($mins, 1) | |
$Time = $Date.ToShortTimeString() | |
Write-Host " $Time " -ForegroundColor DarkGreen -NoNewline | |
Write-Host " $ToGo Mins to go" -ForegroundColor Yellow -NoNewline | |
} | |
{ $_ -le 10 } { | |
$ToGo = [Math]::Round($mins, 1) | |
$Time = $Date.ToShortTimeString() | |
Write-Host " $Time " -ForegroundColor DarkGreen -NoNewline | |
Write-Host " $ToGo Mins to go" -ForegroundColor Red -BackgroundColor DarkYellow -NoNewline | |
} | |
Default { } | |
} | |
# Newline | |
Write-Host "" | |
} | |
# Determine if the user is admin, so we color the prompt green or red | |
$isAdmin = $false | |
$isDesktop = ($PSVersionTable.PSEdition -eq "Desktop") | |
if ($isDesktop -or $IsWindows) { | |
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() | |
$windowsPrincipal = new-object 'System.Security.Principal.WindowsPrincipal' $windowsIdentity | |
$isAdmin = $windowsPrincipal.IsInRole("Administrators") -eq 1 | |
} | |
else { | |
$isAdmin = ((& id -u) -eq 0) | |
} | |
if ($isAdmin) { $color = $color = "`e[38;5;9;48;5;237m"; } | |
else { $color = "`e[38;5;231;48;5;27m "; } | |
# Write PS> for desktop PowerShell, pwsh> for PowerShell Core | |
if ($isDesktop) { | |
Write-Host " PS5>" -NoNewLine -ForegroundColor $color | |
} | |
else { | |
$version = $PSVersionTable.PSVersion.ToString() | |
#Write-Host " pwsh $Version>" -NoNewLine -ForegroundColor $color | |
Write-Host "$($color)pwsh $Version>" -NoNewLine | |
} | |
# Always have to return something or else we get the default prompt | |
return " " | |
} | |
function Start-BeardSQL { | |
#region Run as Admin Code | |
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) | |
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
Write-Verbose "Process is running as Admin" | |
Start-Service SQLSERVERAGENT | |
Start-Service MSSQLSERVER | |
} | |
else { | |
$MyInvocation.MyCommand.Definition | |
Start-Process powershell.exe "-Command", ('"{0}"' -f "Start-Service SQLSERVERAGENT;Start-Service MSSQLSERVER") -Verb RunAs | |
} | |
} | |
function Decompile-GithubArmTemplate { | |
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[string] | |
$GithubPath, | |
[Parameter()] | |
[string] | |
$Path, | |
[Parameter()] | |
[string] | |
$FileName | |
) | |
if($GithubPath.StartsWith('https://raw.githubusercontent.com')){ | |
Write-Verbose "Well it looks like raw content URL" | |
} elseif ($GithubPath.StartsWith('https://github.com')) { | |
Write-Verbose "Well it looks like a base Github URL" | |
$GithubPath = $GithubPath -replace 'github', 'raw.githubusercontent' -replace '/blob','' | |
$Name = $GithubPath.Split('/')[-2] | |
}else{ | |
Write-Warning "Use the right path and start with https://" | |
Return | |
} | |
$DownloadFile = "$env:TEMP\{0}.{1}" -f $Name, 'json' | |
($Path) ? ($outputPath = $Path) : ($outputPath = $Pwd.Path) | |
($FileName) ? ($FileName) : ($FileName = "$Name.{0}" -f 'bicep') | |
$wc = New-Object System.Net.WebClient | |
$wc.DownloadFile($GithubPath, $DownloadFile) | |
bicep decompile $DownloadFile --outfile "$outputPath\$FileName" | |
Write-Output "Decompiled $GithubPath to $outputPath\$FileName" | |
} | |
function whatsmyip { | |
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[switch] | |
$clip | |
) | |
if($clip){ | |
(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content | Set-Clipboard | |
} else{ | |
(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment