From 75496f76cd4fa526078c33775985b0d8ba08f4b5 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Fri, 13 Dec 2024 15:17:20 +0000 Subject: [PATCH] Split profile.ps1 into separate files --- alias.ps1 | 4 ++ completion.ps1 | 25 +++++++++++ environment.ps1 | 15 +++++++ profile.ps1 | 111 ++++-------------------------------------------- prompt.ps1 | 27 ++++++++++++ readline.ps1 | 33 ++++++++++++++ utils.ps1 | 2 + 7 files changed, 115 insertions(+), 102 deletions(-) create mode 100644 alias.ps1 create mode 100644 completion.ps1 create mode 100644 environment.ps1 create mode 100644 prompt.ps1 create mode 100644 readline.ps1 create mode 100644 utils.ps1 diff --git a/alias.ps1 b/alias.ps1 new file mode 100644 index 0000000..7c3d5e6 --- /dev/null +++ b/alias.ps1 @@ -0,0 +1,4 @@ +# Remove these aliases to PowerShell builtins which clobber the actaul exes. +Remove-Item alias:curl +Remove-Item alias:wget +# TODO: Remove default abbriviation aliases diff --git a/completion.ps1 b/completion.ps1 new file mode 100644 index 0000000..3780d37 --- /dev/null +++ b/completion.ps1 @@ -0,0 +1,25 @@ +# Enable Git completions & prompt. +Import-Module Posh-Git +$GitPromptSettings.EnableFileStatus = $false +$GitPromptSettings.DefaultPromptPath = "" +$GitPromptSettings.DefaultPromptPrefix.Text = "" +$GitPromptSettings.DefaultPromptSuffix.Text = "" +$GitPromptSettings.BeforeStatus = "" +$GitPromptSettings.AfterStatus = "" +# Enable Scoop completions. +if (Get-Command scoop.ps1 -ErrorAction SilentlyContinue) { + $ScoopCompletion = "$($(Get-Item $(Get-Command scoop.ps1).Path).Directory.Parent.FullName)\modules\scoop-completion" + if (Test-Path $ScoopCompletion) { + Import-Module $ScoopCompletion + } +} +# Enable Chocolatey completions. +Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" +# Enable op completions. +if (Test-Path "$env:LOCALAPPDATA\1Password\cli\opProfile.psm1") { + Import-Module "$env:LOCALAPPDATA\1Password\cli\opProfile.psm1" +} +# Enable gh completions. +if (Get-Command "gh" -ErrorAction SilentlyContinue) { + Invoke-Expression -Command $(gh completion -s powershell | Out-String) +} diff --git a/environment.ps1 b/environment.ps1 new file mode 100644 index 0000000..c588b38 --- /dev/null +++ b/environment.ps1 @@ -0,0 +1,15 @@ +# Set environment variables +if (Get-Command nvim.exe -ErrorAction SilentlyContinue) { + $Env:EDITOR = 'nvim.exe' +} + +# FIXME: These run to early since profile.ps1 loads before VS dev env setup. +if (Get-Command cmake.exe -ErrorAction SilentlyContinue) { + if (Get-Command ninja.exe -ErrorAction SilentlyContinue) { + $Env:CMAKE_GENERATOR = 'Ninja' + } + if (Get-Command ccache.exe -ErrorAction SilentlyContinue) { + $Env:CMAKE_C_COMPILER_LAUNCHER = 'ccache.exe' + $Env:CMAKE_CXX_COMPILER_LAUNCHER = 'ccache.exe' + } +} diff --git a/profile.ps1 b/profile.ps1 index f5a5a80..1ef05f3 100644 --- a/profile.ps1 +++ b/profile.ps1 @@ -1,107 +1,14 @@ -# Enable Git completions & prompt. -Import-Module Posh-Git -$GitPromptSettings.EnableFileStatus = $false -$GitPromptSettings.DefaultPromptPath = "" -$GitPromptSettings.DefaultPromptPrefix.Text = "" -$GitPromptSettings.DefaultPromptSuffix.Text = "" -$GitPromptSettings.BeforeStatus = "" -$GitPromptSettings.AfterStatus = "" -# Enable Scoop completions. -if (Get-Command scoop.ps1 -ErrorAction SilentlyContinue) { - $ScoopCompletion = "$($(Get-Item $(Get-Command scoop.ps1).Path).Directory.Parent.FullName)\modules\scoop-completion" - if (Test-Path $ScoopCompletion) { - Import-Module $ScoopCompletion - } -} -# Enable Chocolatey completions. -Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" -# Enable op completions. -if (Test-Path "$env:LOCALAPPDATA\1Password\cli\opProfile.psm1") { - Import-Module "$env:LOCALAPPDATA\1Password\cli\opProfile.psm1" -} -# Enable gh completions. -if (Get-Command "gh" -ErrorAction SilentlyContinue) { - Invoke-Expression -Command $(gh completion -s powershell | Out-String) -} - -# Customize PowerShell prompt. -function Prompt { - $exitCode = $LastExitCode - # Line one - Get-Date -Format "HH:mm:ss " | Write-Host -ForegroundColor DarkGray -NoNewline - $location = (Get-Location).Path - if ($location.StartsWith($env:USERPROFILE)) { - $location = $location.Replace($env:USERPROFILE, "~") - } - $location | Write-Host -ForegroundColor DarkCyan -NoNewline - if ($exitCode -ne 0) { - " $exitCode" | Write-Host -ForegroundColor DarkRed -NoNewline - } - & $GitPromptScriptBlock | Write-Host -NoNewline - Write-Host - # Line two - $identity = [Security.Principal.WindowsIdentity]::GetCurrent() - $principal = [Security.Principal.WindowsPrincipal] $identity - $adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator - $userName = $Env:USERNAME.ToLower() - if($principal.IsInRole($adminRole)) { - $userName | Write-Host -ForegroundColor DarkRed -NoNewline - } else { - $userName | Write-Host -ForegroundColor DarkGreen -NoNewline - } - return ' ' -} - -# Set UTF-8 as the character set for pipes and output objects. +# Set UTF-8 as the character set for pipes and output objects. # https://gist.github.com/xoner/4671514 $OutputEncoding = New-Object -typename System.Text.UTF8Encoding [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8 -# Enable Vi line editing mode. -Set-PSReadLineOption -BellStyle None -EditMode Vi -# Make C-w backward delete a word. -Set-PSReadLineKeyHandler -Chord Ctrl+w -Function BackwardDeleteWord -# Make C-[ escape insert mode. -# https://github.com/PowerShell/PSReadLine/issues/906#issuecomment-916847040 -Set-PSReadLineKeyHandler -Chord 'Ctrl+Oem4' -Function ViCommandMode -# Enable menu style tab completions. https://stackoverflow.com/a/37715242 -Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete - -# Autosuggestion bindings for PSReadLine 2.1.0+ -Set-PSReadLineKeyHandler -Chord "Ctrl+p" -Function ForwardChar -Set-PSReadLineKeyHandler -Chord "Ctrl+o" -Function ForwardWord -# Use grey but not italic for autosuggestion highlights -Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[38;5;238m" } - -# Change cursor shape based on what Vi mode the line editor is in. -# Tested and works with PowerShell 5.1 -# NOTE: Requires: Install-Module PsReadline -Scope CurrentUser -Force -# TODO: This doesn't handle all cases well. For example when exiting terminal -# nvim in a PowerShell session the cursor is a block while in insert mode. -function OnViModeChange { - if ($args[0] -eq 'Command') { - # Set the cursor to non-blinking block. - Write-Host -NoNewLine "$([char]0x1b)[2 q" - } else { - # Set the cursor to non-blinking line. - Write-Host -NoNewLine "$([char]0x1b)[6 q" - } -} -Set-PSReadLineOption -ViModeIndicator Script -ViModeChangeHandler $Function:OnViModeChange -# And set the cursor to a non-blinking line on profile load. -Write-Host -NoNewLine "$([char]0x1b)[6 q" - -# Remove these aliases to PowerShell builtins which clobber the actaul exes. -Remove-Item alias:curl -Remove-Item alias:wget - -# Set environment variables -# TODO: Wrap Enter-VsDevShell to also set these? -if ((Get-Command cmake.exe -ErrorAction SilentlyContinue).Source) { - $env:CMAKE_GENERATOR = 'Ninja' - $env:CMAKE_C_COMPILER_LAUNCHER = 'cmake.exe' - $env:CMAKE_CXX_COMPILER_LAUNCHER = 'cmake.exe' -} - -# Load plugins +# Load config from files +. $PSScriptRoot\environment.ps1 +. $PSScriptRoot\completion.ps1 +. $PSScriptRoot\prompt.ps1 +. $PSScriptRoot\readline.ps1 +. $PSScriptRoot\alias.ps1 +. $PSScriptRoot\utils.ps1 . $PSScriptRoot\layout.ps1 +# TODO: $PSScriptRoot\build.ps1 diff --git a/prompt.ps1 b/prompt.ps1 new file mode 100644 index 0000000..ea93418 --- /dev/null +++ b/prompt.ps1 @@ -0,0 +1,27 @@ +# Customize PowerShell prompt. +function Prompt { + $exitCode = $LastExitCode + # Line one + Get-Date -Format "HH:mm:ss " | Write-Host -ForegroundColor DarkGray -NoNewline + $location = (Get-Location).Path + if ($location.StartsWith($env:USERPROFILE)) { + $location = $location.Replace($env:USERPROFILE, "~") + } + $location | Write-Host -ForegroundColor DarkCyan -NoNewline + if ($exitCode -ne 0) { + " $exitCode" | Write-Host -ForegroundColor DarkRed -NoNewline + } + & $GitPromptScriptBlock | Write-Host -NoNewline + Write-Host + # Line two + $identity = [Security.Principal.WindowsIdentity]::GetCurrent() + $principal = [Security.Principal.WindowsPrincipal] $identity + $adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator + $userName = $Env:USERNAME.ToLower() + if($principal.IsInRole($adminRole)) { + $userName | Write-Host -ForegroundColor DarkRed -NoNewline + } else { + $userName | Write-Host -ForegroundColor DarkGreen -NoNewline + } + return ' ' +} diff --git a/readline.ps1 b/readline.ps1 new file mode 100644 index 0000000..ae20d13 --- /dev/null +++ b/readline.ps1 @@ -0,0 +1,33 @@ +# Enable Vi line editing mode. +Set-PSReadLineOption -BellStyle None -EditMode Vi +# Make C-w backward delete a word. +Set-PSReadLineKeyHandler -Chord Ctrl+w -Function BackwardDeleteWord +# Make C-[ escape insert mode. +# https://github.com/PowerShell/PSReadLine/issues/906#issuecomment-916847040 +Set-PSReadLineKeyHandler -Chord 'Ctrl+Oem4' -Function ViCommandMode +# Enable menu style tab completions. https://stackoverflow.com/a/37715242 +Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete + +# Autosuggestion bindings for PSReadLine 2.1.0+ +Set-PSReadLineKeyHandler -Chord "Ctrl+p" -Function ForwardChar +Set-PSReadLineKeyHandler -Chord "Ctrl+o" -Function ForwardWord +# Use grey but not italic for autosuggestion highlights +Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[38;5;238m" } + +# Change cursor shape based on what Vi mode the line editor is in. +# Tested and works with PowerShell 5.1 +# NOTE: Requires: Install-Module PsReadline -Scope CurrentUser -Force +# TODO: This doesn't handle all cases well. For example when exiting terminal +# nvim in a PowerShell session the cursor is a block while in insert mode. +function OnViModeChange { + if ($args[0] -eq 'Command') { + # Set the cursor to non-blinking block. + Write-Host -NoNewLine "$([char]0x1b)[2 q" + } else { + # Set the cursor to non-blinking line. + Write-Host -NoNewLine "$([char]0x1b)[6 q" + } +} +Set-PSReadLineOption -ViModeIndicator Script -ViModeChangeHandler $Function:OnViModeChange +# And set the cursor to a non-blinking line on profile load. +Write-Host -NoNewLine "$([char]0x1b)[6 q" diff --git a/utils.ps1 b/utils.ps1 new file mode 100644 index 0000000..b850a50 --- /dev/null +++ b/utils.ps1 @@ -0,0 +1,2 @@ +# TODO: Define cd to work with zero args for home and - for last directory +# TODO: Define rm to work with -rf and multiple entries