From 18bbe73ca84be669ec8f3a0e483bb25275127173 Mon Sep 17 00:00:00 2001 From: Mikael CAPELLE Date: Tue, 16 Aug 2022 08:43:25 +0200 Subject: [PATCH] Initial commit. --- Microsoft.PowerShell_profile.ps1 | 33 ++++++ Modules/posh-nodejs/posh-nodejs.psm1 | 59 ++++++++++ Modules/posh-ssh/posh-ssh.psm1 | 20 ++++ Modules/posh-venv/posh-venv.psm1 | 54 ++++++++++ theme.omp.json | 154 +++++++++++++++++++++++++++ 5 files changed, 320 insertions(+) create mode 100644 Microsoft.PowerShell_profile.ps1 create mode 100644 Modules/posh-nodejs/posh-nodejs.psm1 create mode 100644 Modules/posh-ssh/posh-ssh.psm1 create mode 100644 Modules/posh-venv/posh-venv.psm1 create mode 100644 theme.omp.json diff --git a/Microsoft.PowerShell_profile.ps1 b/Microsoft.PowerShell_profile.ps1 new file mode 100644 index 0000000..e9792b5 --- /dev/null +++ b/Microsoft.PowerShell_profile.ps1 @@ -0,0 +1,33 @@ +# Import module: +$profile_folder = (Split-Path -Parent $PROFILE) + +Import-Module posh-git + +$env:POSH_GIT_ENABLED = $true +$env:VIRTUAL_ENV_DISABLE_PROMPT = "True" + +oh-my-posh init pwsh --config (Join-Path $profile_folder theme.omp.json) | Invoke-Expression + +function docker { + wsl -- docker $args +} + +Import-Module posh-ssh +Import-Module posh-venv +Import-Module DockerCompletion + +Set-PSReadLineOption -PredictionSource History +Set-PSReadLineOption -HistorySearchCursorMovesToEnd +Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward +Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward +Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete +Set-PSReadlineOption -BellStyle None + +# kube +if (Get-Command kubectl -ErrorAction SilentlyContinue) { + kubectl completion powershell | Out-String | Invoke-Expression +} + +if (Get-Command helm -ErrorAction SilentlyContinue) { + helm completion powershell | Out-String | Invoke-Expression +} diff --git a/Modules/posh-nodejs/posh-nodejs.psm1 b/Modules/posh-nodejs/posh-nodejs.psm1 new file mode 100644 index 0000000..f5ca18e --- /dev/null +++ b/Modules/posh-nodejs/posh-nodejs.psm1 @@ -0,0 +1,59 @@ +if (Get-Module posh-nodejs) { return } + +$NodeJsHome = "$HOME\Apps\nodejs" + +$InitialNodePath = $env:Path.Split(";") | Where-Object { $_ -like "$NodeJsHome*" } + +function CompleteNodeJsVersion { + param($commandName, $parameterName, $wordToComplete, + $commandAst, $fakeBoundParameters) + + Get-ChildItem -Directory $NodeJsHome + | ForEach-Object { $_.Name.Split("-")[1].Substring(1) } + | Where-Object { $_ -Like "$wordToComplete*" } + | Sort-Object +} + + +function nvm { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [ValidateSet("list", "activate", "deactivate")] + $command, + + [Parameter(Mandatory = $false)] + [ArgumentCompleter({ CompleteNodeJsVersion @args })] + $version) + switch ($command) { + "list" { + Get-ChildItem -Directory $NodeJsHome + | ForEach-Object { + $version = $_.Name.Split("-")[1].Substring(1) + Write-Output "$version" + } + } + + "activate" { + $path = "$NodeJsHome\node-v$version-win-x64" + if (Test-Path "$path") { + $paths = $env:Path.Split(";") | Where-Object { $_ -NotLike "$NodeJsHome*" } + $paths += $path + $env:Path = $paths | Join-String -Separator ";" + } + else { + Write-Error "NodeJS version '${version}' not found." + } + } + + "deactivate" { + $paths = $env:Path.Split(";") | Where-Object { $_ -NotLike "$NodeJsHome*" } + $paths += $InitialNodePath + $env:Path = $paths | Join-String -Separator ";" + } + + default { + Write-Error "Unknown nvm command ${args[0]}." + } + } +} diff --git a/Modules/posh-ssh/posh-ssh.psm1 b/Modules/posh-ssh/posh-ssh.psm1 new file mode 100644 index 0000000..4b652c5 --- /dev/null +++ b/Modules/posh-ssh/posh-ssh.psm1 @@ -0,0 +1,20 @@ +if (Get-Module posh-ssh) { + return +} + +$cmdNames = "ssh" +$cmdNames += Get-Alias -Definition $cmdNames -ErrorAction Ignore | ForEach-Object Name + +$configFile = "$env:USERPROFILE/.ssh/config" + +Register-ArgumentCompleter -CommandName $cmdNames -Native -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + + Get-Content $configFile | Select-String -Pattern "^Host(\s+([.A-Za-z0-9\-_]+))+\s*$" | ForEach-Object { + $_.Matches.Groups[2].Captures.Value + } | Where-Object { + $_ -like "$wordToComplete*" + } | ForEach-Object { + "$_" + } +} diff --git a/Modules/posh-venv/posh-venv.psm1 b/Modules/posh-venv/posh-venv.psm1 new file mode 100644 index 0000000..3afee1d --- /dev/null +++ b/Modules/posh-venv/posh-venv.psm1 @@ -0,0 +1,54 @@ +if (Get-Module posh-venv) { return } + +function CompleteVenvName { + param($commandName, $parameterName, $wordToComplete, + $commandAst, $fakeBoundParameters) + + Get-ChildItem -Directory $env:WORKON_HOME + | ForEach-Object { $_.Name } + | Where-Object { $_ -Like "$wordToComplete*" } + | Sort-Object +} + + +function venv { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [ValidateSet("list", "activate", "deactivate")] + $command, + + [Parameter(Mandatory = $false)] + [ArgumentCompleter({ CompleteVenvName @args })] + $venv) + switch ($command) { + "list" { + Get-ChildItem -Directory $env:WORKON_HOME + | ForEach-Object { + $name = $_.Name + $version = (& "$_/Scripts/python.exe" --version) + Write-Output "${name}: $version" + } + } + + "activate" { + if (Test-Path "$env:WORKON_HOME/$venv/Scripts/activate.ps1") { + & "$env:WORKON_HOME/$venv/Scripts/activate.ps1" + } + elseif ((!$venv -Or !$venv.Trim()) -And (Test-Path "venv/Scripts/activate.ps1")) { + & "venv/Scripts/activate.ps1" + } + else { + Write-Error "Environment '${venv}' does not exists." + } + } + + "deactivate" { + deactivate + } + + default { + Write-Error "Unknown venv command ${args[0]}." + } + } +} diff --git a/theme.omp.json b/theme.omp.json new file mode 100644 index 0000000..7fae4ef --- /dev/null +++ b/theme.omp.json @@ -0,0 +1,154 @@ +{ + "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json", + "blocks": [ + { + "alignment": "left", + "segments": [ + { + "foreground": "#ffee58", + "style": "plain", + "template": " \uf0e7 ", + "type": "root" + }, + { + "foreground": "#06884b", + "foreground_templates": [ + "{{ if gt .Code 0 }}#e61010{{ end }}" + ], + "properties": { + "always_enabled": true + }, + "style": "powerline", + "template": " {{ if gt .Code 0 }}\u2717{{ else }}\u2714{{ end }} ", + "type": "exit" + }, + { + "foreground": "#ffffff", + "properties": { + "display_host": false + }, + "style": "plain", + "template": " {{ if .SSHSession }}\uf817 {{ end }}{{ .UserName }}", + "type": "session" + } + ], + "type": "prompt" + }, + { + "alignment": "left", + "segments": [ + { + "foreground": "#fff000", + "properties": { + "prefix": " \ufcb5 ", + "var_name": "__SHELL_INFORMATION_POSH_258__" + }, + "style": "powerline", + "type": "envvar" + }, + { + "foreground": "#b600ff", + "style": "powerline", + "template": " \ufcd1 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} ", + "type": "go" + }, + { + "foreground": "#b600ff", + "style": "powerline", + "template": " \ue718 {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }} ", + "type": "node" + }, + { + "background": "#b600ff", + "foreground": "#ffffff", + "powerline_symbol": "\ue0b0", + "properties": { + "display_mode": "context", + "home_enabled": true + }, + "style": "powerline", + "template": " \ue235 {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}{{ end }} ", + "type": "python" + } + ], + "type": "prompt" + }, + { + "alignment": "left", + "segments": [ + { + "foreground": "#8e24aa", + "style": "plain", + "template": " {{ if .WSL }}WSL at {{ end }}{{.Icon}} ", + "type": "os" + }, + { + "background": "#0037da", + "foreground": "#ffffff", + "powerline_symbol": "\ue0b0", + "properties": { + "folder_separator_icon": " \ue0b1 ", + "home_icon": "\uf7db", + "style": "letter" + }, + "style": "powerline", + "template": " {{ .Path }} ", + "type": "path" + }, + { + "foreground": "#193549", + "powerline_symbol": "\ue0b0", + "style": "powerline", + "template": " {{ .Status }} ", + "type": "poshgit" + } + ], + "type": "prompt" + }, + { + "alignment": "right", + "segments": [ + { + "foreground": "#8454bb", + "powerline_symbol": "\ue0b0", + "properties": { + "style": "austin", + "threshold": 500 + }, + "style": "plain", + "template": " <#fefefe>\ufbab {{ .FormattedMs }} ", + "type": "executiontime" + }, + { + "foreground": "#007ACC", + "properties": { + "time_format": "[15:04:05]" + }, + "style": "plain", + "template": " {{ .CurrentDate | date .Format }} ", + "type": "time" + } + ], + "type": "rprompt" + }, + { + "alignment": "left", + "newline": true, + "segments": [ + { + "foreground": "#0037da", + "style": "powerline", + "template": " \u276f ", + "type": "text" + } + ], + "type": "prompt" + } + ], + "final_space": true, + "transient_prompt": { + "background": "transparent", + "foreground": "#8800dd" + }, + "version": 2 +}