Split profile.ps1 into separate files

This commit is contained in:
Kenneth Benzie 2024-12-13 15:17:20 +00:00
parent f61e0768d2
commit 75496f76cd
7 changed files with 115 additions and 102 deletions

4
alias.ps1 Normal file
View File

@ -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

25
completion.ps1 Normal file
View File

@ -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)
}

15
environment.ps1 Normal file
View File

@ -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'
}
}

View File

@ -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

27
prompt.ps1 Normal file
View File

@ -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 ' '
}

33
readline.ps1 Normal file
View File

@ -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"

2
utils.ps1 Normal file
View File

@ -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