Skip to content

Instantly share code, notes, and snippets.

@brccabral
Last active June 17, 2022 18:31
Show Gist options
  • Save brccabral/fc09bfc712703afcf1f0c9acd7717700 to your computer and use it in GitHub Desktop.
Save brccabral/fc09bfc712703afcf1f0c9acd7717700 to your computer and use it in GitHub Desktop.
Powershell Oh-My-Posh

Powershell Oh-My-Posh

Script to setup Oh-My-Posh in Powershell

# install oh-my-posh
winget install oh-my-posh
# create a profile file if it doesn't exist
New-Item -Path $PROFILE -Type File -Force
# get a list of themes to choose
Get-PoshThemes
# inside the profile file, set the theme (choose one)
# oh-my-posh init pwsh | Invoke-Expression
# oh-my-posh init pwsh --config 'https://raw.githubusercontent.com/dracula/oh-my-posh/master/dracula.omp.json' | Invoke-Expression
oh-my-posh init pwsh --config $env:USERPROFILE\AppData\Local\Programs\oh-my-posh\themes\powerlevel10k_rainbow.omp.json | Invoke-Expression
# oh-my-posh init pwsh --config $env:USERPROFILE\AppData\Local\Programs\oh-my-posh\themes\pwsh10k.omp.json | Invoke-Expression
# oh-my-posh init pwsh --config $env:USERPROFILE\AppData\Local\Programs\oh-my-posh\themes\paradox.omp.json | Invoke-Expression
# if you change the theme, export to a new file
oh-my-posh config export --output $env:USERPROFILE\AppData\Local\Programs\oh-my-posh\themes\mytheme.omp.json | Invoke-Expression
# if needed, change the ExecutionPolicy
Get-ExecutionPolicy -List
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser
# this reloads the $PROFILE file in case of any changes
. $PROFILE
Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser
# Start the default settings
Set-Prompt
# Alternatively set the desired theme:
Set-Theme Agnoster
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $PROFILE
# inside $PROFILE
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox
#requires -Version 2 -Modules posh-git
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
}
#check for elevated prompt
If (Test-Administrator) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor $sl.Colors.AdminIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
$user = $sl.CurrentUser
$computer = $sl.CurrentHostname
$path = Get-FullPath -dir $pwd
if (Test-NotDefaultUser($user)) {
$prompt += Write-Prompt -Object "$user@$computer " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
$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
# Writes the postfix to the prompt
$prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor
$timeStamp = Get-Date -UFormat %R
$timestamp = "[$timeStamp]"
$prompt += Set-CursorForRightBlockWrite -textLength ($timestamp.Length + 1)
$prompt += Write-Prompt $timeStamp -ForegroundColor $sl.Colors.PromptForegroundColor
$prompt += Set-Newline
$lastColor = $sl.Colors.SessionInfoBackgroundColor
if (Test-VirtualEnv) {
$prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
$prompt += Write-Prompt -Object " $($sl.PromptSymbols.VirtualEnvSymbol) $(Get-VirtualEnvName) " -ForegroundColor $sl.Colors.VirtualEnvForegroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
$lastColor = $sl.Colors.VirtualEnvBackgroundColor
}
$status = Get-VCSStatus
if ($status) {
$themeInfo = Get-VcsInfo -status ($status)
$themecolor = $themeInfo.BackgroundColor
$prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor -BackgroundColor $themecolor
$prompt += Write-Prompt -Object " $($themeInfo.VcInfo) " -BackgroundColor $themecolor -ForegroundColor $sl.Colors.GitForegroundColor
$lastcolor = $themecolor
}
$prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor
$prompt += Set-Newline
if ($with) {
$prompt += Write-Prompt -Object "$($with.ToUpper()) " -BackgroundColor $sl.Colors.WithBackgroundColor -ForegroundColor $sl.Colors.WithForegroundColor
}
$prompt += Write-Prompt -Object ($sl.PromptSymbols.PromptIndicator) -ForegroundColor $sl.Colors.PromptBackgroundColor
$prompt += ' '
$prompt
}
$sl = $global:ThemeSettings #local settings
$sl.PromptSymbols.StartSymbol = ''
$sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x276F)
$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
#$sl.Colors.PromptHighlightColor = [ConsoleColor]::Red
#$sl.Colors.PromptBackgroundColor = [ConsoleColor]::Yellow
$sl.Colors.GitForegroundColor = [ConsoleColor]::Black
$sl.Colors.WithForegroundColor = [ConsoleColor]::DarkRed
$sl.Colors.WithBackgroundColor = [ConsoleColor]::Magenta
$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment