Skip to content

Instantly share code, notes, and snippets.

@belotn
Created December 3, 2020 12:41
Show Gist options
  • Save belotn/590d83f53330a05c5503e69c5ea2e1de to your computer and use it in GitHub Desktop.
Save belotn/590d83f53330a05c5503e69c5ea2e1de to your computer and use it in GitHub Desktop.
Oh-My-Posh 2 (Pure Powershell) Themes
#requires -Version 2 -Modules posh-git
$Global:ThemeSettings.PromptSymbols.PromptIndicator = '%'
$Global:ThemeSettings.PromptSymbols.ElevatedSymbol = "#"
$Global:ThemeSettings.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
$Global:ThemeSettings.Colors.PromptForegroundColor = [ConsoleColor]::White
$Global:ThemeSettings.Colors.PromptSymbolColor = [ConsoleColor]::White
$Global:ThemeSettings.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
$Global:ThemeSettings.Colors.GitForegroundColor = [ConsoleColor]::Black
$Global:ThemeSettings.Colors.WithForegroundColor = [ConsoleColor]::DarkRed
$Global:ThemeSettings.Colors.WithBackgroundColor = [ConsoleColor]::Magenta
$Global:ThemeSettings.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
if($Global:ThemeSettings.CurrentDomain){
$Global:ThemeSettings.CurrentDomain = $env:USerDomain
}else{
$Global:ThemeSettings= $Global:themesettings | Add-Member -Type NoteProperty -Name CurrentDomain -Value $env:USERDOMAIN -PassThru
}
function Get-PathLeaf {
param(
[Parameter(Mandatory = $true)]
[System.Management.Automation.PathInfo]
$dir
)
if ($dir.path -eq "$($dir.Drive.Name):\") {
return "$($dir.Drive.Name):"
}
$path = $dir.path.Replace($HOME, $sl.PromptSymbols.HomeSymbol)
if($path -ne '~'){
$path = Split-Path -Path $path -Leaf
}
return $path
}
function Write-Theme {
param(
[bool]
$lastCommandFailed,
[string]
$with
)
$lastColor = $sl.Colors.PromptBackgroundColor
$prompt = Write-Prompt -Object $sl.PromptSymbols.StartSymbol -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
#check the last command state and indicate if failed
If ($lastCommandFailed) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.FailedCommandSymbol) " -ForegroundColor $sl.Colors.CommandFailedIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
}
$user = $sl.CurrentUser
$computer = $sl.CurrentHostname
$path = Get-PathLeaf -dir $pwd
$shell = "PS:" + $PSVersionTable.PSversion.Major
$editor = (Get-PSreadlineOption ).EditMode
$freespace = "$([math]::Round( ((get-location).Drive.free)/1gb ))Gb"
$prompt += Write-Prompt -Object "$shell $editor " -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
if (Test-NotDefaultUser($user)) {
if($sl.CurrentDomain){
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.PromptBackgroundColor -BackgroundColor $sl.Colors.SessionInfoForegroundColor
$prompt += Write-Prompt -Object "$($sl.CurrentDomain) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.SessionInfoForegroundColor
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}else{
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.PromptBackgroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
$prompt += Write-Prompt -Object "$user@$computer " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
if (Test-VirtualEnv) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.VirtualEnvSymbol) $(Get-VirtualEnvName) " -ForegroundColor $sl.Colors.VirtualEnvForegroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.VirtualEnvBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
}
else {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
}
# Writes the drive portion
$prompt += Write-Prompt -Object "$path " -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
$prompt += Write-Prompt -Object $freespace -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
$status = Get-VCSStatus
if ($status) {
$themeInfo = Get-VcsInfo -status ($status)
$lastColor = $themeInfo.BackgroundColor
$prompt += Write-Prompt -Object $($sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor $sl.Colors.PromptBackgroundColor -BackgroundColor $lastColor
$prompt += Write-Prompt -Object " $($themeInfo.VcInfo) " -BackgroundColor $lastColor -ForegroundColor $sl.Colors.GitForegroundColor
}
# Writes the postfix to the prompt
$prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor
$timeStamp = Get-Date -UFormat "%Y-%m-%d %R"
$timestamp = "[$timeStamp]"
$prompt += Set-CursorForRightBlockWrite -textLength ($timestamp.Length + 1)
$prompt += Write-Prompt $timeStamp -ForegroundColor $sl.Colors.PromptForegroundColor
$prompt += Set-Newline
if ($with) {
$prompt += Write-Prompt -Object "$($with.ToUpper()) " -BackgroundColor $sl.Colors.WithBackgroundColor -ForegroundColor $sl.Colors.WithForegroundColor
}
If (Test-Administrator) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.ElevatedSymbol)" -ForegroundColor $sl.Colors.PromptSymbolColor # -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
Set-PSreadlineOption -PromptText "$($sl.PromptSymbols.ElevatedSymbol) "
}else {
$prompt += Write-Prompt -Object ($sl.PromptSymbols.PromptIndicator) -ForegroundColor $sl.Colors.PromptSymbolColor
Set-PSreadlineOption -PromptText "$($sl.PromptSymbols.PromptIndicator) "
}
$prompt += ' '
$prompt
}
$sl = $global:ThemeSettings #local settings
$sl.PromptSymbols.StartSymbol = '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment