Skip to content

Instantly share code, notes, and snippets.

@michaeltlombardi
Created October 16, 2020 14:04
Show Gist options
  • Save michaeltlombardi/8676475a8f65dea876c4f745dfb5ddce to your computer and use it in GitHub Desktop.
Save michaeltlombardi/8676475a8f65dea876c4f745dfb5ddce to your computer and use it in GitHub Desktop.
oh-my-posh bigmikey
#requires -Version 2 -Modules posh-git
function Write-Theme
{
param(
[bool]
$lastCommandFailed,
[string]
$with
)
# Retrieve info for use in prompt
$DateTime = Get-Date -format s
$TimeStamp = $DateTime -split 'T' | Select-Object -First 1
If ([string]::IsNullOrEmpty($env:USERNAME)) {
If (! [string]::IsNullOrEmpty($env:USER)) {
$User = $env:USER
} Else {
$User = $null
}
} Else {
$User = $env:USERNAME
}
If ([string]::IsNullOrEmpty($env:USERNAME)) {
$Computer = hostname
} Else {
$Computer = $Env:COMPUTERNAME
}
$Path = Get-FullPath -dir $pwd
# Check for elevated prompt
If (Test-Administrator) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor $sl.Colors.AdminIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
# Add info about user and node
If (Test-NotDefaultUser($User)) {
$prompt += Write-Prompt -Object "$User@$computer " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
$Prompt += Write-Prompt -Object "$($sl.PromptSymbols.OperatingSystem) " -ForegroundColor $sl.Colors.OSIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
$LastBackgroundColor = $sl.Colors.SessionInfoBackgroundColor
# Add info about Ruby, if desired.
If ($env:ShowRubyInfo -eq $true) {
If ([string]::IsNullOrEmpty($env:RubyVersion)) {
$env:RubyVersion = Get-Ruby
}
If (![string]::IsNullOrEmpty($env:RubyVersion)) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $LastBackgroundColor -BackgroundColor $sl.Colors.RubyBackgroundColor
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.Ruby) $env:RubyVersion " -ForegroundColor $sl.Colors.RubyForegroundColor -BackgroundColor $sl.Colors.RubyBackgroundColor
$LastBackgroundColor = $sl.Colors.RubyBackgroundColor
}
}
# Handle Git
$status = Get-VCSStatus
if ($status) {
$themeInfo = Get-VcsInfo -status ($status)
# Strip the branch icon because we're using the upstream one.
$VcsInfo = (Get-VcsInfo -status (Get-VCSStatus)).vcinfo
$lastColor = $themeInfo.BackgroundColor
$prompt += Write-Prompt -Object $($sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor $LastBackgroundColor -BackgroundColor $lastColor
$prompt += Write-Prompt -object " $(Get-VcsSymbol)" -BackgroundColor $lastColor -ForegroundColor $sl.Colors.GitForegroundColor
$prompt += Write-Prompt -Object " $VcsInfo " -BackgroundColor $lastColor -ForegroundColor $sl.Colors.GitForegroundColor
$LastBackgroundColor = $lastColor
}
# Insert line break
$prompt += Write-Prompt -Object $($sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor $LastBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
$prompt += Set-Newline
# Write Command Status
If ( $lastCommandFailed ) {
$Prompt += Write-Prompt -Object "$($sl.PromptSymbols.CommandFailure) " -ForegroundColor $Sl.Colors.CommandFailureSymbol -BackgroundColor $sl.Colors.PromptBackgroundColor
} Else {
$Prompt += Write-Prompt -Object "$($sl.PromptSymbols.CommandSuccess) " -ForegroundColor $Sl.Colors.CommandSuccessSymbol -BackgroundColor $sl.Colors.PromptBackgroundColor
}
$LastBackgroundColor = $sl.Colors.PromptBackgroundColor
# Writes the drive portion
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $LastBackgroundColor -BackgroundColor $sl.Colors.DriveBackgroundColor
$prompt += Write-Prompt -Object "$path " -ForegroundColor $sl.Colors.DriveForegroundColor -BackgroundColor $sl.Colors.DriveBackgroundColor
$LastBackgroundColor = $sl.Colors.DriveBackgroundColor
# Write Final Segment
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $LastBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
$prompt
}
$sl = $global:ThemeSettings #local settings
# Segment Symbols
$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
$sl.PromptSymbols.SegmentBackwardSymbol = [char]::ConvertFromUtf32(0xE0B2)
$sl.PromptSymbols.SegmentSeparatorForwardSymbol = [char]::ConvertFromUtf32(0xE0B1)
$sl.PromptSymbols.SegmentSeparatorBackwardSymbol = [char]::ConvertFromUtf32(0xE0B3)
# Command Success/Failure Symbols
$sl.PromptSymbols.CommandSuccess = [char]::ConvertFromUtf32(0xf00c)
$sl.PromptSymbols.CommandFailure = [char]::ConvertFromUtf32(0xf467)
$sl.Colors.CommandSuccessSymbol = [System.ConsoleColor]::Green
$sl.Colors.CommandFailureSymbol = [System.ConsoleColor]::Red
# VCS Repo Symbols
$sl.PromptSymbols.Gitlab = [char]::ConvertFromUtf32(0xf296)
$sl.PromptSymbols.Github = [char]::ConvertFromUtf32(0xf09b)
$sl.PromptSymbols.Bitbucket = [char]::ConvertFromUtf32(0xe703)
# OS Symbol
If ($PSVersionTable.PSVersion.Major -ge 6) {
Switch -Regex ($PSVersionTable.OS) {
'^Darwin' {
$sl.PromptSymbols.OperatingSystem = [char]::ConvertFromUtf32(0xf179)
$sl.Colors.OSIconForegroundColor = [System.ConsoleColor]::White
}
'^Linux' {
$sl.PromptSymbols.OperatingSystem = [char]::ConvertFromUtf32(0xf17c)
$sl.Colors.OSIconForegroundColor = [System.ConsoleColor]::DarkGray
}
default {
$sl.PromptSymbols.OperatingSystem = [char]::ConvertFromUtf32(0xf17a)
$sl.Colors.OSIconForegroundColor = [System.ConsoleColor]::Cyan
}
}
} Else {
$sl.PromptSymbols.OperatingSystem = [char]::ConvertFromUtf32(0xf17a)
$sl.Colors.OSIconForegroundColor = [System.ConsoleColor]::Cyan
}
# Code Language Symbols
$sl.PromptSymbols.Ruby = [char]::ConvertFromUtf32(0xe21e)
$sl.Colors.RubyForegroundColor = [System.ConsoleColor]::White
$sl.Colors.RubyBackgroundColor = [System.ConsoleColor]::Red
# Colors
$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
$sl.Colors.PromptBackgroundColor = [ConsoleColor]::Black
$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
$sl.Colors.PromptHighlightColor = [ConsoleColor]::Magenta
$sl.Colors.GitForegroundColor = [ConsoleColor]::Black
$sl.Colors.WithForegroundColor = [ConsoleColor]::White
$sl.Colors.WithBackgroundColor = [ConsoleColor]::DarkRed
$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White
$sl.Colors.SessionInfoBackgroundColor = [System.ConsoleColor]::Black
$sl.Colors.DriveBackgroundColor = [System.ConsoleColor]::DarkBlue
$sl.Colors.DriveForegroundColor = [System.ConsoleColor]::Yellow
Function Get-VcsSymbol {
$Remotes = git remote -v
$RawRemote = $Remotes | Where-Object -FilterScript { $_ -match '^origin\s+\S+\s+\(fetch\)'}
$RemoteUrl = ($RawRemote -split '\s+')[1]
Switch -Regex ($RemoteUrl) {
'github' { $sl.PromptSymbols.Github }
'gitlab' { $sl.PromptSymbols.Gitlab }
'bitbucket' { $sl.PromptSymbols.Bitbucket }
}
}
Function Get-Ruby {
If (Get-Command -Name ruby -CommandType Application -ErrorAction SilentlyContinue) {
$RubyString = ruby --version
($RubyString -split '\s+')[1]
} Else {
return (Write-Output $null)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment