Last active
May 8, 2024 15:27
-
-
Save LindnerBrewery/e0087f756c7f77aad79f084fbdcc876e to your computer and use it in GitHub Desktop.
PsProfile
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
# Choco settings | |
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" | |
if (Test-Path($ChocolateyProfile)) { | |
Import-Module "$ChocolateyProfile" | |
} | |
# Install and load modules | |
$modulesToLoad = @('DockerCompletion', 'CompletionPredictor') | |
foreach ($module in $modulesToLoad) { | |
if (! (Get-Module $module -ListAvailable)) { | |
Write-Host "Installing module: $module" | |
Install-Module $module -Scope CurrentUser | |
} | |
Import-Module $module | |
} | |
# PSReadline settings | |
Set-PSReadLineKeyHandler -Key Tab -Function complete | |
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward | |
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward | |
$psreadlineOptions = @{ | |
Colors = @{ InlinePrediction = '#2F7004' } | |
BellStyle = 'None' # no more annoying beeps | |
HistorySearchCursorMovesToEnd = $true | |
} | |
Set-PSReadLineOption @psreadlineOptions -ErrorAction SilentlyContinue | |
try { | |
Set-PSReadLineOption -PredictionSource 'HistoryAndPlugin' | |
} | |
catch { | |
"couldn't load prediction source" | |
} | |
if ($host.name -ne 'Windows PowerShell ISE Host') { | |
# Oh-my-posh and visual settings | |
# https://gist.githubusercontent.com/lindnerbrewery/bed217b6b9dd59696a2b732a3c918d2d/raw/setup_terminal.ps1 | |
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/my.omp.json" | Invoke-Expression | |
$env:POSH_GIT_ENABLED = $true | |
if ($PSVersionTable.PSVersion -ge "7.0") { | |
Import-Module Terminal-Icons | |
} | |
} | |
#default property values | |
$PSDefaultParameterValues = @{ | |
"Install-Module:Scope" = "CurrentUser" | |
"Install-Module:AllowClobber" = $true | |
} | |
# aws completion | |
Register-ArgumentCompleter -Native -CommandName aws -ScriptBlock { | |
param($commandName, $wordToComplete, $cursorPosition) | |
$env:COMP_LINE = $wordToComplete | |
if ($env:COMP_LINE.Length -lt $cursorPosition) { | |
$env:COMP_LINE = $env:COMP_LINE + " " | |
} | |
$env:COMP_POINT = $cursorPosition | |
aws_completer.exe | ForEach-Object { | |
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) | |
} | |
Remove-Item Env:\COMP_LINE | |
Remove-Item Env:\COMP_POINT | |
} | |
# kubectl completion | |
kubectl completion powershell | Out-String | Invoke-Expression | |
# eksctl completion | |
# powershell completion for eksctl -*- shell-script -*- | |
function __eksctl_debug { | |
if ($env:BASH_COMP_DEBUG_FILE) { | |
"$args" | Out-File -Append -FilePath "$env:BASH_COMP_DEBUG_FILE" | |
} | |
} | |
filter __eksctl_escapeStringWithSpecialChars { | |
$_ -replace '\s|#|@|\$|;|,|''|\{|\}|\(|\)|"|`|\||<|>|&','`$&' | |
} | |
[scriptblock]${__eksctlCompleterBlock} = { | |
param( | |
$WordToComplete, | |
$CommandAst, | |
$CursorPosition | |
) | |
# Get the current command line and convert into a string | |
$Command = $CommandAst.CommandElements | |
$Command = "$Command" | |
__eksctl_debug "" | |
__eksctl_debug "========= starting completion logic ==========" | |
__eksctl_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition" | |
# The user could have moved the cursor backwards on the command-line. | |
# We need to trigger completion from the $CursorPosition location, so we need | |
# to truncate the command-line ($Command) up to the $CursorPosition location. | |
# Make sure the $Command is longer then the $CursorPosition before we truncate. | |
# This happens because the $Command does not include the last space. | |
if ($Command.Length -gt $CursorPosition) { | |
$Command=$Command.Substring(0,$CursorPosition) | |
} | |
__eksctl_debug "Truncated command: $Command" | |
$ShellCompDirectiveError=1 | |
$ShellCompDirectiveNoSpace=2 | |
$ShellCompDirectiveNoFileComp=4 | |
$ShellCompDirectiveFilterFileExt=8 | |
$ShellCompDirectiveFilterDirs=16 | |
$ShellCompDirectiveKeepOrder=32 | |
# Prepare the command to request completions for the program. | |
# Split the command at the first space to separate the program and arguments. | |
$Program,$Arguments = $Command.Split(" ",2) | |
$RequestComp="$Program __completeNoDesc $Arguments" | |
__eksctl_debug "RequestComp: $RequestComp" | |
# we cannot use $WordToComplete because it | |
# has the wrong values if the cursor was moved | |
# so use the last argument | |
if ($WordToComplete -ne "" ) { | |
$WordToComplete = $Arguments.Split(" ")[-1] | |
} | |
__eksctl_debug "New WordToComplete: $WordToComplete" | |
# Check for flag with equal sign | |
$IsEqualFlag = ($WordToComplete -Like "--*=*" ) | |
if ( $IsEqualFlag ) { | |
__eksctl_debug "Completing equal sign flag" | |
# Remove the flag part | |
$Flag,$WordToComplete = $WordToComplete.Split("=",2) | |
} | |
if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) { | |
# If the last parameter is complete (there is a space following it) | |
# We add an extra empty parameter so we can indicate this to the go method. | |
__eksctl_debug "Adding extra empty parameter" | |
# PowerShell 7.2+ changed the way how the arguments are passed to executables, | |
# so for pre-7.2 or when Legacy argument passing is enabled we need to use | |
# `"`" to pass an empty argument, a "" or '' does not work!!! | |
if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or | |
($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or | |
(($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and | |
$PSNativeCommandArgumentPassing -eq 'Legacy')) { | |
$RequestComp="$RequestComp" + ' `"`"' | |
} else { | |
$RequestComp="$RequestComp" + ' ""' | |
} | |
} | |
__eksctl_debug "Calling $RequestComp" | |
# First disable ActiveHelp which is not supported for Powershell | |
${env:EKSCTL_ACTIVE_HELP}=0 | |
#call the command store the output in $out and redirect stderr and stdout to null | |
# $Out is an array contains each line per element | |
Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null | |
# get directive from last line | |
[int]$Directive = $Out[-1].TrimStart(':') | |
if ($Directive -eq "") { | |
# There is no directive specified | |
$Directive = 0 | |
} | |
__eksctl_debug "The completion directive is: $Directive" | |
# remove directive (last element) from out | |
$Out = $Out | Where-Object { $_ -ne $Out[-1] } | |
__eksctl_debug "The completions are: $Out" | |
if (($Directive -band $ShellCompDirectiveError) -ne 0 ) { | |
# Error code. No completion. | |
__eksctl_debug "Received error from custom completion go code" | |
return | |
} | |
$Longest = 0 | |
[Array]$Values = $Out | ForEach-Object { | |
#Split the output in name and description | |
$Name, $Description = $_.Split("`t",2) | |
__eksctl_debug "Name: $Name Description: $Description" | |
# Look for the longest completion so that we can format things nicely | |
if ($Longest -lt $Name.Length) { | |
$Longest = $Name.Length | |
} | |
# Set the description to a one space string if there is none set. | |
# This is needed because the CompletionResult does not accept an empty string as argument | |
if (-Not $Description) { | |
$Description = " " | |
} | |
@{Name="$Name";Description="$Description"} | |
} | |
$Space = " " | |
if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) { | |
# remove the space here | |
__eksctl_debug "ShellCompDirectiveNoSpace is called" | |
$Space = "" | |
} | |
if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or | |
(($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) { | |
__eksctl_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported" | |
# return here to prevent the completion of the extensions | |
return | |
} | |
$Values = $Values | Where-Object { | |
# filter the result | |
$_.Name -like "$WordToComplete*" | |
# Join the flag back if we have an equal sign flag | |
if ( $IsEqualFlag ) { | |
__eksctl_debug "Join the equal sign flag back to the completion value" | |
$_.Name = $Flag + "=" + $_.Name | |
} | |
} | |
# we sort the values in ascending order by name if keep order isn't passed | |
if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) { | |
$Values = $Values | Sort-Object -Property Name | |
} | |
if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) { | |
__eksctl_debug "ShellCompDirectiveNoFileComp is called" | |
if ($Values.Length -eq 0) { | |
# Just print an empty string here so the | |
# shell does not start to complete paths. | |
# We cannot use CompletionResult here because | |
# it does not accept an empty string as argument. | |
"" | |
return | |
} | |
} | |
# Get the current mode | |
$Mode = (Get-PSReadLineKeyHandler | Where-Object {$_.Key -eq "Tab" }).Function | |
__eksctl_debug "Mode: $Mode" | |
$Values | ForEach-Object { | |
# store temporary because switch will overwrite $_ | |
$comp = $_ | |
# PowerShell supports three different completion modes | |
# - TabCompleteNext (default windows style - on each key press the next option is displayed) | |
# - Complete (works like bash) | |
# - MenuComplete (works like zsh) | |
# You set the mode with Set-PSReadLineKeyHandler -Key Tab -Function <mode> | |
# CompletionResult Arguments: | |
# 1) CompletionText text to be used as the auto completion result | |
# 2) ListItemText text to be displayed in the suggestion list | |
# 3) ResultType type of completion result | |
# 4) ToolTip text for the tooltip with details about the object | |
switch ($Mode) { | |
# bash like | |
"Complete" { | |
if ($Values.Length -eq 1) { | |
__eksctl_debug "Only one completion left" | |
# insert space after value | |
[System.Management.Automation.CompletionResult]::new($($comp.Name | __eksctl_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)") | |
} else { | |
# Add the proper number of spaces to align the descriptions | |
while($comp.Name.Length -lt $Longest) { | |
$comp.Name = $comp.Name + " " | |
} | |
# Check for empty description and only add parentheses if needed | |
if ($($comp.Description) -eq " " ) { | |
$Description = "" | |
} else { | |
$Description = " ($($comp.Description))" | |
} | |
[System.Management.Automation.CompletionResult]::new("$($comp.Name)$Description", "$($comp.Name)$Description", 'ParameterValue', "$($comp.Description)") | |
} | |
} | |
# zsh like | |
"MenuComplete" { | |
# insert space after value | |
# MenuComplete will automatically show the ToolTip of | |
# the highlighted value at the bottom of the suggestions. | |
[System.Management.Automation.CompletionResult]::new($($comp.Name | __eksctl_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)") | |
} | |
# TabCompleteNext and in case we get something unknown | |
Default { | |
# Like MenuComplete but we don't want to add a space here because | |
# the user need to press space anyway to get the completion. | |
# Description will not be shown because that's not possible with TabCompleteNext | |
[System.Management.Automation.CompletionResult]::new($($comp.Name | __eksctl_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)") | |
} | |
} | |
} | |
} | |
Register-ArgumentCompleter -CommandName 'eksctl' -ScriptBlock ${__eksctlCompleterBlock} |
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
# download and copy profile to CurrentUserAllHosts | |
$profileurl = "https://gist.githubusercontent.com/lindnerbrewery/e0087f756c7f77aad79f084fbdcc876e/raw/profile.ps1" | |
if (Test-Path $profile.CurrentUserAllHosts) { | |
Rename-Item -Path $profile.CurrentUserAllHosts "backup$(Get-Date -f "yyyMMddhhmmss")_$(Split-Path $profile.CurrentUserAllHosts -Leaf)" | |
} | |
else { | |
New-Item $profile.CurrentUserAllHosts -Force -ItemType File | |
} | |
Invoke-WebRequest -Uri $profileurl -OutFile $profile.CurrentUserAllHosts | |
if (!(Get-Command oh-my-posh.exe -ErrorAction SilentlyContinue)) { | |
"Install Oh-my-posh" | |
Set-ExecutionPolicy Bypass -Scope Process -Force; | |
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1')) | |
#download oh-my-posh config | |
irm https://gist.githubusercontent.com/lindnerbrewery/bed217b6b9dd59696a2b732a3c918d2d/raw/my.omp.json -Outfile "$env:POSH_THEMES_PATH/my.omp.json" | |
# check if fonts are installed | |
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | |
$installedFonts = (New-Object System.Drawing.Text.InstalledFontCollection).Families | |
if ($installedFonts -notcontains 'CaskaydiaCove Nerd Font') { | |
"Installing Fonts" | |
# download and install nerd font meslo | |
#$url = "https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/Meslo.zip" # MesloLGL Nerd Font | |
$url = "https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/CascadiaCode.zip" # CaskaydiaCove Nerd Font | |
$OutFolder = $env:TMP | |
$outFileName = ($url -split "/")[-1] | |
$outFile = Join-Path $OutFolder $outFileName | |
#download font | |
$wc = [System.Net.WebClient]::new() | |
$wc.DownloadFile($url, $outFile) | |
# unzip | |
$unzipFolder = $outFile.trim(".zip") | |
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | |
$zipFile = [IO.Compression.ZipFile]::OpenRead($outFile) | |
[System.IO.Compression.ZipFileExtensions]::ExtractToDirectory($zipFile, $unzipFolder)#, $true) | |
$zipFile.Dispose() | |
Remove-Item $outFile -Force | |
# install fonts | |
$shellApp = New-Object -ComObject shell.application | |
$fonts = $shellApp.NameSpace(0x14) | |
foreach ($font in $(Get-ChildItem $unzipFolder -Filter *.ttf)) { | |
$fonts.CopyHere($font.FullName) | |
} | |
} | |
} | |
# load profile | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment