Skip to content

Instantly share code, notes, and snippets.

@TylerLeonhardt
Last active October 31, 2021 14:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TylerLeonhardt/de8f1da4880d1427ed90ede329e48ec9 to your computer and use it in GitHub Desktop.
Save TylerLeonhardt/de8f1da4880d1427ed90ede329e48ec9 to your computer and use it in GitHub Desktop.
Profiles
#region UX config
Import-Module posh-git
if (Get-Module PSReadLine) {
Import-Module oh-my-posh
$ThemeSettings.MyThemesLocation = "~/.config/powershell/oh-my-posh/Themes"
if (Get-Theme | Where-Object Name -eq Sorin-NL) {
Set-Theme Sorin-NL
} else {
Set-Theme Sorin
}
Set-PSReadLineKeyHandler -Chord Alt+Enter -Function AddLine
Set-PSReadLineOption -ContinuationPrompt " " -Colors @{ Operator = "`e[95m"; Parameter = "`e[95m" }
}
#endregion
#region Helper functions
#endregion
#region Argument completers
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# dotnet suggest shell start
if (Get-Command dotnet-suggest -ErrorAction SilentlyContinue) {
$availableToComplete = (dotnet-suggest list) | Out-String
$availableToCompleteArray = $availableToComplete.Split([Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries)
Register-ArgumentCompleter -Native -CommandName $availableToCompleteArray -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$fullpath = (Get-Command $wordToComplete.CommandElements[0]).Source
$arguments = $wordToComplete.Extent.ToString().Replace('"', '\"')
dotnet-suggest get -e $fullpath --position $cursorPosition -- "$arguments" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
$env:DOTNET_SUGGEST_SCRIPT_VERSION = "1.0.0"
}
# dotnet suggest script end
#endregion
#region Update PowerShell Daily
$updateJob = Start-ThreadJob -ScriptBlock {
Invoke-Expression "& {$(Invoke-RestMethod aka.ms/install-powershell.ps1)} -Daily"
}
$eventJob = Register-ObjectEvent -InputObject $updateJob -EventName StateChanged -Action {
if($Event.Sender.State -eq [System.Management.Automation.JobState]::Completed) {
Get-EventSubscriber $eventJob.Name | Unregister-Event
Remove-Job $eventJob -ErrorAction SilentlyContinue
Receive-Job $updateJob -Wait -AutoRemoveJob -ErrorAction SilentlyContinue
}
}
#endregion
#startregion Hooks
# Set CurrentDirectory when LocationChangedAction is invoked.
# This allows iTerm2's "Reuse previous session's directory" to work
$ExecutionContext.SessionState.InvokeCommand.LocationChangedAction += {
[Environment]::CurrentDirectory = $pwd.Path
}
#endregion
#requires -Version 2 -Modules posh-git
# PUT IN ~/.config/powershell/oh-my-posh/Themes
function Write-Theme {
param(
[bool]
$lastCommandFailed,
[string]
$with
)
#check the last command state and indicate if failed
If ($lastCommandFailed) {
$prompt = Write-Prompt -Object "$($sl.PromptSymbols.FailedCommandSymbol) " -ForegroundColor $sl.Colors.CommandFailedIconForegroundColor
}
#check for elevated prompt
If (Test-Administrator) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor $sl.Colors.AdminIconForegroundColor
}
if ($PSVersionTable.GitCommitId.Length -gt 20) {
$prompt += Write-Prompt -Object '[DEV] ' -ForegroundColor $sl.Colors.CommandFailedIconForegroundColor
}
$user = [System.Environment]::UserName
if (Test-NotDefaultUser($user)) {
$prompt += Write-Prompt -Object "`e[1m$user`e[0m " -ForegroundColor $sl.Colors.PromptForegroundColor
}
# Writes the path
$prompt += Write-Prompt -Object "$(Get-FullPath -dir $pwd) " -ForegroundColor $sl.Colors.DriveForegroundColor
$status = Get-VCSStatus
if ($status) {
$themeInfo = Get-VcsInfo -status ($status)
$prompt += Write-Prompt -Object "git:" -ForegroundColor $sl.Colors.PromptForegroundColor
$prompt += Write-Prompt -Object "$($themeInfo.VcInfo) " -ForegroundColor $themeInfo.BackgroundColor
}
# write virtualenv
if (Test-VirtualEnv) {
$prompt += Write-Prompt -Object 'env:' -ForegroundColor $sl.Colors.PromptForegroundColor
$prompt += Write-Prompt -Object "$(Get-VirtualEnvName) " -ForegroundColor $themeInfo.VirtualEnvForegroundColor
}
if ($with) {
$prompt += Write-Prompt -Object "$($with.ToUpper()) " -BackgroundColor $sl.Colors.WithBackgroundColor -ForegroundColor $sl.Colors.WithForegroundColor
}
$prompt += Set-Newline
# Writes the postfixes to the prompt
$prompt += Write-Prompt -Object $sl.PromptSymbols.PromptIndicator -ForegroundColor $sl.Colors.CommandFailedIconForegroundColor
$prompt += Write-Prompt -Object $sl.PromptSymbols.PromptIndicator -ForegroundColor $sl.Colors.AdminIconForegroundColor
$prompt += Write-Prompt -Object $sl.PromptSymbols.PromptIndicator -ForegroundColor $sl.Colors.GitNoLocalChangesAndAheadColor
$prompt += ' '
$prompt
if($global:PostPromptHook) {
. $global:PostPromptHook
}
}
$sl = $global:ThemeSettings #local settings
$sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x276F)
$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
$sl.Colors.WithForegroundColor = [ConsoleColor]::DarkRed
$sl.Colors.WithBackgroundColor = [ConsoleColor]::Magenta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment