diff --git a/dot_config/powershell/Async/alias.ps1 b/dot_config/powershell/Async/alias.ps1 index ab96592..ae56486 100644 --- a/dot_config/powershell/Async/alias.ps1 +++ b/dot_config/powershell/Async/alias.ps1 @@ -1,19 +1,20 @@ # Aliases - - ### Sudo -function Invoke-LastCommandAsSudo { +function Invoke-LastCommandAsSudo +{ $history = Get-History -Count 3 - if ($history.Count -lt 1) { + if ($history.Count -lt 1) + { Write-Host (Get-ColorString "No previous command found.") return } $lastCommand = $history[-1].CommandLine - if ([string]::IsNullOrWhiteSpace($lastCommand)) { + if ([string]::IsNullOrWhiteSpace($lastCommand)) + { Write-Host (Get-ColorString "Last command was empty.") return } @@ -26,8 +27,10 @@ Set-Alias -Name 's!!' -Value Invoke-LastCommandAsSudo -Scope Global | Out-Null ### EZA -if (Get-Command eza -ErrorAction SilentlyContinue) { - function Get-EzaWithColor { +if (Get-Command eza -ErrorAction SilentlyContinue) +{ + function Get-EzaWithColor + { param ( [Parameter(ValueFromRemainingArguments = $true)] [string[]]$Args @@ -35,31 +38,38 @@ if (Get-Command eza -ErrorAction SilentlyContinue) { eza --icons=auto --color=always --color-scale=size -I lost+found -h @Args } Set-Alias -Name ls -Value Get-EzaWithColor -Scope Global | Out-Null -} -else { +} else +{ $Global:ProfileHints += "eza not installed. Please Run Install-Eza" } -function Install-Eza { - try { - if ((Get-OperatingSystem) -eq "windows") { + +function Install-Eza +{ + try + { + if ((Get-OperatingSystem) -eq "windows") + { Install-WithWinget "eza-community.eza" - } else { + } else + { Install-WithYayPacman "eza" } - } - catch { + } catch + { Write-Warning "Error during the install: $_" } } ### Measure in a new Terminal -function Measure-ScriptInNewTerminal { +function Measure-ScriptInNewTerminal +{ param( [Parameter(Mandatory)] [string]$ScriptPath ) - if (-not (Test-Path $ScriptPath)) { + if (-not (Test-Path $ScriptPath)) + { throw "Script file '$ScriptPath' does not exist." } @@ -76,12 +86,14 @@ Measure-Script -Path '$ScriptPath' } ### Reload -function Update-Profile { +function Update-Profile +{ . $PROFILE.CurrentUserAllHosts } # yay no confirm alias -function Invoke-YayNoConfirm { +function Invoke-YayNoConfirm +{ param ( [Parameter(ValueFromRemainingArguments = $true)] [string[]]$Args @@ -91,19 +103,22 @@ function Invoke-YayNoConfirm { Set-Alias -Name yyay -Value Invoke-YayNoConfirm -Scope Global | Out-Null -function Link-ScrcpyToVirtualMic { +function Link-ScrcpyToVirtualMic +{ param( [string]$AppNodeName = "scrcpy", [string]$VirtualMicName = "scrcpySource" ) # wait until both nodes exist - do { + do + { $nodes = pw-cli ls Node $appExists = $nodes -match "node.name = `"$AppNodeName`"" $micExists = pw-link -i | Select-String $VirtualMicName - if (-not ($appExists -and $micExists)) { + if (-not ($appExists -and $micExists)) + { Start-Sleep -Seconds 1 } } until ($appExists -and $micExists) @@ -111,17 +126,25 @@ function Link-ScrcpyToVirtualMic { # remove existing links from the app $nodes = pw-cli ls Node $appNodeId = $nodes | Select-String "node.name = `"$AppNodeName`"" | ForEach-Object { - if ($_.Line -match 'id (\d+),') { $matches[1] } + if ($_.Line -match 'id (\d+),') + { $matches[1] + } } $links = pw-cli ls Link $linkIdsToRemove = $links | ForEach-Object { - if ($_ -match 'id (\d+),') { $linkId = $matches[1] } + if ($_ -match 'id (\d+),') + { $linkId = $matches[1] + } $outputNodeMatch = $_ -match 'link\.output\.node = "(\d+)"' $inputNodeMatch = $_ -match 'link\.input\.node = "(\d+)"' - if ($outputNodeMatch -and $matches[1] -eq $appNodeId) { $linkId } - elseif ($inputNodeMatch -and $matches[2] -eq $appNodeId) { $linkId } + if ($outputNodeMatch -and $matches[1] -eq $appNodeId) + { $linkId + } elseif ($inputNodeMatch -and $matches[2] -eq $appNodeId) + { $linkId + } } - foreach ($linkId in $linkIdsToRemove | Where-Object { $_ }) { + foreach ($linkId in $linkIdsToRemove | Where-Object { $_ }) + { pw-link -d $linkId | Out-Null } @@ -130,7 +153,8 @@ function Link-ScrcpyToVirtualMic { pw-link "${AppNodeName}:output_FR" "${VirtualMicName}:input_FR" | Out-Null } -function Start-Scrcpy { +function Start-Scrcpy +{ param( # Use -Webcam to enable webcam mode [switch]$Webcam, @@ -149,57 +173,70 @@ function Start-Scrcpy { "--max-fps=$MaxFps" ) - if ($Webcam) { + if ($Webcam) + { $scrcpyArgs += "--v4l2-sink=$V4l2Device", "--video-source=camera", "--camera-id=$CameraId", "--no-video-playback", "--camera-size=1920x1080" } # Run Link-ScrcpyToVirtualMic in the background - if ($global:os -eq 'linux'){ - Start-Job -ScriptBlock { - -function Link-ScrcpyToVirtualMic { - param( - [string]$AppNodeName = "scrcpy", - [string]$VirtualMicName = "scrcpySource" - ) - - # wait until both nodes exist - do { - $nodes = pw-cli ls Node - $appExists = $nodes -match "node.name = `"$AppNodeName`"" - $micExists = pw-link -i | Select-String $VirtualMicName - - if (-not ($appExists -and $micExists)) { - Start-Sleep -Seconds 1 - } - } until ($appExists -and $micExists) - - # remove existing links from the app - $nodes = pw-cli ls Node - $appNodeId = $nodes | Select-String "node.name = `"$AppNodeName`"" | ForEach-Object { - if ($_.Line -match 'id (\d+),') { $matches[1] } - } - $links = pw-cli ls Link - $linkIdsToRemove = $links | ForEach-Object { - if ($_ -match 'id (\d+),') { $linkId = $matches[1] } - $outputNodeMatch = $_ -match 'link\.output\.node = "(\d+)"' - $inputNodeMatch = $_ -match 'link\.input\.node = "(\d+)"' - if ($outputNodeMatch -and $matches[1] -eq $appNodeId) { $linkId } - elseif ($inputNodeMatch -and $matches[2] -eq $appNodeId) { $linkId } + if ($global:os -eq 'linux') + { + Start-Job -ScriptBlock { + + function Link-ScrcpyToVirtualMic + { + param( + [string]$AppNodeName = "scrcpy", + [string]$VirtualMicName = "scrcpySource" + ) + + # wait until both nodes exist + do + { + $nodes = pw-cli ls Node + $appExists = $nodes -match "node.name = `"$AppNodeName`"" + $micExists = pw-link -i | Select-String $VirtualMicName + + if (-not ($appExists -and $micExists)) + { + Start-Sleep -Seconds 1 + } + } until ($appExists -and $micExists) + + # remove existing links from the app + $nodes = pw-cli ls Node + $appNodeId = $nodes | Select-String "node.name = `"$AppNodeName`"" | ForEach-Object { + if ($_.Line -match 'id (\d+),') + { $matches[1] + } + } + $links = pw-cli ls Link + $linkIdsToRemove = $links | ForEach-Object { + if ($_ -match 'id (\d+),') + { $linkId = $matches[1] + } + $outputNodeMatch = $_ -match 'link\.output\.node = "(\d+)"' + $inputNodeMatch = $_ -match 'link\.input\.node = "(\d+)"' + if ($outputNodeMatch -and $matches[1] -eq $appNodeId) + { $linkId + } elseif ($inputNodeMatch -and $matches[2] -eq $appNodeId) + { $linkId + } + } + foreach ($linkId in $linkIdsToRemove | Where-Object { $_ }) + { + pw-link -d $linkId | Out-Null + } + + # create new links quietly + pw-link "${AppNodeName}:output_FL" "${VirtualMicName}:input_FL" | Out-Null + pw-link "${AppNodeName}:output_FR" "${VirtualMicName}:input_FR" | Out-Null + } + + + Link-ScrcpyToVirtualMic } } - foreach ($linkId in $linkIdsToRemove | Where-Object { $_ }) { - pw-link -d $linkId | Out-Null - } - - # create new links quietly - pw-link "${AppNodeName}:output_FL" "${VirtualMicName}:input_FL" | Out-Null - pw-link "${AppNodeName}:output_FR" "${VirtualMicName}:input_FR" | Out-Null -} - - - Link-ScrcpyToVirtualMic } -} # Start scrcpy and get the process object Start-Process "scrcpy" -ArgumentList $scrcpyArgs -Wait } diff --git a/dot_config/powershell/Async/catppuccin.ps1 b/dot_config/powershell/Async/catppuccin.ps1 index 55cb80d..a94fa61 100644 --- a/dot_config/powershell/Async/catppuccin.ps1 +++ b/dot_config/powershell/Async/catppuccin.ps1 @@ -1,43 +1,49 @@ Import-Module Catppuccin $global:Flavor = $Catppuccin['Frappe'] - -function Get-ColorStringCatppuccin { +function Get-ColorStringCatppuccin +{ param ( [Parameter(Mandatory)] [string] $in ) $tagPattern = '<([A-Za-z0-9]+)>' - if (-not $global:Flavor) { + if (-not $global:Flavor) + { return [regex]::Replace($in, $tagPattern, '') } $tagMatches = [regex]::Matches($in, $tagPattern) - if ($tagMatches.Count -eq 0) { + if ($tagMatches.Count -eq 0) + { return $in } $result = New-Object System.Text.StringBuilder $lastIndex = 0 - foreach ($m in $tagMatches) { + foreach ($m in $tagMatches) + { [void]$result.Append($in.Substring($lastIndex, $m.Index - $lastIndex)) $name = $m.Groups[1].Value - if ($name -ieq 'Clear') { + if ($name -ieq 'Clear') + { # Reset only the foreground color to terminal default [void]$result.Append("$([char]27)[39m") - } - else { + } else + { $colourObject = $global:Flavor.$name - if ($null -ne $colourObject) { + if ($null -ne $colourObject) + { [void]$result.Append($colourObject.Foreground()) } } $lastIndex = $m.Index + $m.Length } - if ($lastIndex -lt $in.Length) { + if ($lastIndex -lt $in.Length) + { [void]$result.Append($in.Substring($lastIndex)) } diff --git a/dot_config/powershell/Async/chezmoi.ps1 b/dot_config/powershell/Async/chezmoi.ps1 index 36b148c..a6cff0f 100644 --- a/dot_config/powershell/Async/chezmoi.ps1 +++ b/dot_config/powershell/Async/chezmoi.ps1 @@ -1,17 +1,20 @@ ### Setup Chezmoi -if (Get-Command chezmoi -ErrorAction SilentlyContinue) { +if (Get-Command chezmoi -ErrorAction SilentlyContinue) +{ Invoke-Expression (& { (chezmoi completion powershell | Out-String) }) - function Edit-Chezmoi { + function Edit-Chezmoi + { param ( [string]$FilePath ) # List all if empty - if ([string]::IsNullOrWhiteSpace($FilePath)) { + if ([string]::IsNullOrWhiteSpace($FilePath)) + { chezmoi managed -t - } - else { + } else + { chezmoi edit --watch -a $FilePath } } diff --git a/dot_config/powershell/Async/completion.ps1 b/dot_config/powershell/Async/completion.ps1 index 89200f2..6a27afa 100644 --- a/dot_config/powershell/Async/completion.ps1 +++ b/dot_config/powershell/Async/completion.ps1 @@ -1,23 +1,33 @@ -function Install-Carapace { - If ($global:os -eq "windows") { +function Install-Carapace +{ + If ($global:os -eq "windows") + { Install-WithWinget "rsteube.Carapace" - } else { + } else + { Install-WithYayPacman "carapace-bin" } } -function Initilize-Carapace { - if (-not (Get-Command carapace -ErrorAction SilentlyContinue)) { - $Global:ProfileHints += "carapace not found. Install with Install-Carapace" - return - } +function Enable-Carapace +{ - $env:CARAPACE_BRIDGES = 'zsh,fish,bash,inshellisense' # optional - Set-PSReadLineOption -Colors @{ "Selection" = "`e[7m" } - Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete - carapace _carapace | Out-String | Invoke-Expression + try + { + $env:CARAPACE_BRIDGES = 'zsh,fish,bash,inshellisense' # optional + Set-PSReadLineOption -Colors @{ "Selection" = "`e[7m" } + Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete + carapace _carapace | Out-String | Invoke-Expression + } catch + { + if (-not (Find-Command carapace -ErrorAction SilentlyContinue)) + { + $Global:ProfileHints += "carapace not found. Install with Install-Carapace" + return + } + } } # Load Carapace -Initilize-Carapace +Enable-Carapace diff --git a/dot_config/powershell/Async/oh-my-posh.ps1 b/dot_config/powershell/Async/oh-my-posh.ps1 index 4cbd1c3..d2b0171 100644 --- a/dot_config/powershell/Async/oh-my-posh.ps1 +++ b/dot_config/powershell/Async/oh-my-posh.ps1 @@ -4,11 +4,14 @@ oh-my-posh init --config $configPath pwsh | Invoke-Expression # re-add zoxide if needed $global:__zoxide_hooked = (Get-Variable __zoxide_hooked -ErrorAction Ignore -ValueOnly) -if ($global:__zoxide_hooked -eq 1) { +if ($global:__zoxide_hooked -eq 1) +{ $global:__zoxide_prompt_old = $function:prompt - function global:prompt { - if ($null -ne $__zoxide_prompt_old) { + function global:prompt + { + if ($null -ne $__zoxide_prompt_old) + { & $__zoxide_prompt_old } $null = __zoxide_hook @@ -16,9 +19,10 @@ if ($global:__zoxide_hooked -eq 1) { } # Re-render the prompt as soon as possible -try { +try +{ [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() -} -catch { +} catch +{ # PSReadLine not available; ignore } diff --git a/dot_config/powershell/Async/psprofiler.ps1 b/dot_config/powershell/Async/psprofiler.ps1 index 6c5f5f0..e434aa0 100644 --- a/dot_config/powershell/Async/psprofiler.ps1 +++ b/dot_config/powershell/Async/psprofiler.ps1 @@ -1,19 +1,22 @@ # Check if PSProfiler is installed -if (-not (Get-Module -ListAvailable -Name PSProfiler)) { - try { +if (-not (Get-Module -ListAvailable -Name PSProfiler)) +{ + try + { Write-Host (Get-ColorString "Installing PSProfiler...") Install-Module PSProfiler -Scope CurrentUser -Force -ErrorAction Stop - } - catch { + } catch + { Write-Warning (Get-ColorString "Failed to install PSProfiler: $_") } } # Try to import the module -try { +try +{ Import-Module PSProfiler -ErrorAction Stop -} -catch { +} catch +{ Write-Warning (Get-ColorString "⚠ Could not load PSProfiler: $_") } diff --git a/dot_config/powershell/Async/zoxide.ps1 b/dot_config/powershell/Async/zoxide.ps1 index 23e12c8..1405c74 100644 --- a/dot_config/powershell/Async/zoxide.ps1 +++ b/dot_config/powershell/Async/zoxide.ps1 @@ -1,23 +1,27 @@ -function Install-Zoxide { - if ((Get-OperatingSystem) -eq "windows") { +function Install-Zoxide +{ + if ((Get-OperatingSystem) -eq "windows") + { Write-Host (Get-ColorString "Installing zoxide via winget…") winget install --silent ajeetdsouza.zoxide - } - else { + } else + { Write-Host (Get-ColorString "Installing zoxide via curl…") bash -c "curl -sSfL https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh" } } # Initialize zoxide -try { +try +{ Invoke-Expression (& { (zoxide init --cmd cd powershell | Out-String) }) -} -catch { - if (-not (Get-Command zoxide -ErrorAction SilentlyContinue)) { +} catch +{ + if (-not (Get-Command zoxide -ErrorAction SilentlyContinue)) + { $global:ProfileIssues += "zoxide is not installed. Call Install-Zoxide to install it." - } - else { + } else + { $global:ProfileIssues += "zoxide could not be initialized" } } diff --git a/dot_config/powershell/Async/core.ps1 b/dot_config/powershell/core.ps1 similarity index 54% rename from dot_config/powershell/Async/core.ps1 rename to dot_config/powershell/core.ps1 index eb30e54..6e32896 100644 --- a/dot_config/powershell/Async/core.ps1 +++ b/dot_config/powershell/core.ps1 @@ -1,169 +1,202 @@ # Core functions -function Get-OperatingSystem { - if ($env:OS -eq "Windows_NT") { +function Get-OperatingSystem +{ + if ($env:OS -eq "Windows_NT") + { return "windows" - } - else { + } else + { return "linux" } } $global:os = Get-OperatingSystem -function Show-ProfileIssues { +function Show-ProfileIssues +{ # Render all errors - foreach ($issue in $global:ProfileIssues) { + foreach ($issue in $global:ProfileIssues) + { Write-Warning (Get-ColorString $issue) } } -function Show-ProfileHints { +function Show-ProfileHints +{ param( [string[]] $hints = $Global:ProfileHints ) # Render all errors - foreach ($issue in $hints) { + foreach ($issue in $hints) + { Write-Host (Get-ColorString $issue) } } -function Show-ProfileAsync { +function Show-ProfileAsync +{ # Render all errors - foreach ($issue in $global:ProfileLoadedAsync) { + foreach ($issue in $global:ProfileLoadedAsync) + { Write-Host (Get-ColorString $issue) } } -function Save-ProfileHints { - try { +function Save-ProfileHints +{ + try + { $configDir = Join-Path $HOME ".config/powershell" - if (-not (Test-Path $configDir)) { + if (-not (Test-Path $configDir)) + { New-Item -ItemType Directory -Path $configDir -Force | Out-Null } $filePath = Join-Path $configDir "last_hints" + + + $payload = @{ hints = @($global:ProfileHints) } $json = $payload | ConvertTo-Json -Depth 5 Set-Content -Path $filePath -Value $json -Encoding utf8 - } - catch { + } catch + { $global:ProfileIssues += "Failed to save profile hints: $($_.Exception.Message)" } } -function Get-LastProfileHints { - try { +function Get-LastProfileHints +{ + try + { $filePath = Join-Path $HOME ".config/powershell/last_hints" - if (-not (Test-Path $filePath)) { + if (-not (Test-Path $filePath)) + { return @{ hints = @() } } $raw = Get-Content -Path $filePath -Raw -ErrorAction Stop $obj = $raw | ConvertFrom-Json -ErrorAction Stop - if ($null -eq $obj) { + if ($null -eq $obj) + { return @{ hints = @() } } return $obj - } - catch { + } catch + { $global:ProfileIssues += "Failed to load last profile hints: $($_.Exception.Message)" return @{ hints = @() } } } -function Install-WithWinget { +function Install-WithWinget +{ param ( [Parameter(Mandatory)] [string] $name ) - if ((Get-OperatingSystem) -ne "windows") { + if ((Get-OperatingSystem) -ne "windows") + { Write-Warning "You can only use winget on windows" return } - try { + try + { winget install --exact $name --accept-package-agreements - } - catch { + } catch + { throw "Could not install $name with winget: $_" } pwsh -NoLogo -WorkingDirectory $pwd } -function Install-WithYayPacman { +function Install-WithYayPacman +{ param ( [Parameter(Mandatory, ValueFromRemainingArguments = $true)] [string[]] $Name ) - if ((Get-OperatingSystem) -ne "linux") { + if ((Get-OperatingSystem) -ne "linux") + { Write-Warning "You can use yay/pacman on linux" return } $useYay = $false - if (Get-Command -Name 'yay' -ErrorAction SilentlyContinue) { + if (Get-Command -Name 'yay' -ErrorAction SilentlyContinue) + { $useYay = $true - } - elseif (-not (Get-Command -Name 'pacman' -ErrorAction SilentlyContinue)) { + } elseif (-not (Get-Command -Name 'pacman' -ErrorAction SilentlyContinue)) + { throw "Neither 'yay' nor 'pacman' is available on PATH." } - try { + try + { $packageArgs = @('-S', '--needed', '--noconfirm') + $Name - if ($useYay) { + if ($useYay) + { & yay @packageArgs - } - else { + } else + { & sudo pacman @packageArgs } - } - catch { + } catch + { throw "Could not install $([string]::Join(', ', $Name)) with $([bool]$useYay ? 'yay' : 'pacman'): $_" } pwsh -NoLogo -WorkingDirectory $pwd } -function install-withapt { +function install-withapt +{ param ( [Parameter(Mandatory, ValueFromRemainingArguments = $true)] [string[]] $Name ) - if ((Get-OperatingSystem) -ne "linux") { + if ((Get-OperatingSystem) -ne "linux") + { Write-Warning "You can use apt on linux" return } $aptGetCmd = Get-Command -Name 'apt-get' -ErrorAction SilentlyContinue $aptCmd = Get-Command -Name 'apt' -ErrorAction SilentlyContinue - if (-not $aptGetCmd -and -not $aptCmd) { + if (-not $aptGetCmd -and -not $aptCmd) + { throw "Neither 'apt-get' nor 'apt' is available on PATH." } - try { + try + { $packageArgs = @('install', '-y') + $Name - if ($aptGetCmd) { + if ($aptGetCmd) + { & sudo apt-get @packageArgs - } - else { + } else + { & sudo apt @packageArgs } - } - catch { + } catch + { throw "Could not install $([string]::Join(', ', $Name)) with apt: $_" } pwsh -NoLogo -WorkingDirectory $pwd } -function Get-ColorString { +function Get-ColorString +{ param ( [Parameter(Mandatory)] [string] $in ) $handler = Get-Command -Name 'Get-ColorStringCatppuccin' -CommandType Function -ErrorAction SilentlyContinue - if ($null -ne $handler) { + if ($null -ne $handler) + { return (Get-ColorStringCatppuccin -in $in) } @@ -171,3 +204,66 @@ function Get-ColorString { return [regex]::Replace($in, '<([A-Za-z0-9]+)>', '') } + +# Initialize cache for Find-Command +$global:CommandCache = @{} + +function Initialize-CommandCache +{ + # Cache all available commands from PATH + $pathSep = if ($global:os -eq 'windows') + { ';' + } else + { ':' + } + $pathDirs = $env:PATH -split $pathSep | Where-Object { $_ -and (Test-Path $_) } + foreach ($dir in $pathDirs) + { + $files = Get-ChildItem -Path $dir -File -ErrorAction SilentlyContinue + foreach ($file in $files) + { + $name = if ($os -eq 'windows') + { + if ($file.Extension -in @('.exe', '.cmd', '.bat', '.com')) + { + $file.BaseName + } else + { continue + } + + } else + { + $file.Name + } + $global:CommandCache[$name] = $true + } + } +} + +# Command existence cache +function Find-Command +{ + param ( + [string] $commandName + ) + + # Check if cache is empty and initialize it + if ($global:CommandCache.Count -eq 0) + { + # Initialize-CommandCache + } + + # Check cache first + if ($global:CommandCache.ContainsKey($commandName)) + { + return $global:CommandCache[$commandName] + } + + # Not cached, check and cache the result + $command = Get-Command -Name $commandName -ErrorAction SilentlyContinue + $exists = $null -ne $command + $global:CommandCache[$commandName] = $exists + + return $exists +} + diff --git a/dot_config/powershell/Async/env.ps1 b/dot_config/powershell/env.ps1 similarity index 70% rename from dot_config/powershell/Async/env.ps1 rename to dot_config/powershell/env.ps1 index 2619663..5b7105d 100644 --- a/dot_config/powershell/Async/env.ps1 +++ b/dot_config/powershell/env.ps1 @@ -2,13 +2,18 @@ # pnpm $PNPM_HOME = Join-Path $HOME ".local/share/pnpm" -if (Test-Path -Path $PNPM_HOME) { - if ($IsWindows) { - if (-not ($env:PATH -split ';' | Where-Object { $_ -eq $PNPM_HOME })) { +if (Test-Path -Path $PNPM_HOME) +{ + if ($IsWindows) + { + if (-not ($env:PATH -split ';' | Where-Object { $_ -eq $PNPM_HOME })) + { $env:PATH = "$env:PATH;$PNPM_HOME" } - } else { - if (-not ($env:PATH -split ':' | Where-Object { $_ -eq $PNPM_HOME })) { + } else + { + if (-not ($env:PATH -split ':' | Where-Object { $_ -eq $PNPM_HOME })) + { $env:PATH = "${PNPM_HOME}:$env:PATH" } } @@ -16,19 +21,25 @@ if (Test-Path -Path $PNPM_HOME) { # cargo $CARGO_BIN = Join-Path $HOME ".cargo/bin" -if (Test-Path -Path $CARGO_BIN) { - if ($IsWindows) { - if (-not ($env:PATH -split ';' | Where-Object { $_ -eq $CARGO_BIN })) { +if (Test-Path -Path $CARGO_BIN) +{ + if ($IsWindows) + { + if (-not ($env:PATH -split ';' | Where-Object { $_ -eq $CARGO_BIN })) + { $env:PATH = "$env:PATH;$CARGO_BIN" } - } else { - if (-not ($env:PATH -split ':' | Where-Object { $_ -eq $CARGO_BIN })) { + } else + { + if (-not ($env:PATH -split ':' | Where-Object { $_ -eq $CARGO_BIN })) + { $env:PATH = "${CARGO_BIN}:$env:PATH" } } } # Set SOPS Key if it exists -if (Test-Path -Path (Join-Path $HOME ".config/age/sops")) { +if (Test-Path -Path (Join-Path $HOME ".config/age/sops")) +{ $env:SOPS_AGE_KEY_FILE = Join-Path $HOME ".config/age/sops" } diff --git a/dot_config/powershell/format.psd1 b/dot_config/powershell/format.psd1 new file mode 100644 index 0000000..18b4268 --- /dev/null +++ b/dot_config/powershell/format.psd1 @@ -0,0 +1,12 @@ +$settings = @{ + IncludeRules = @("PSPlaceOpenBrace", "PSUseConsistentIndentation") + Rules = @{ + PSPlaceOpenBrace = @{ + Enable = $true + OnSameLine = $false + } + PSUseConsistentIndentation = @{ + Enable = $true + } + } +} diff --git a/dot_config/powershell/main.ps1 b/dot_config/powershell/main.ps1 index 5a9dd6b..bdbb80e 100644 --- a/dot_config/powershell/main.ps1 +++ b/dot_config/powershell/main.ps1 @@ -1,7 +1,8 @@ $time = [System.Diagnostics.Stopwatch]::StartNew() # Disable update check -if ($env:POWERSHELL_UPDATECHECK -ne 'LTS') { +if ($env:POWERSHELL_UPDATECHECK -ne 'LTS') +{ [System.Environment]::SetEnvironmentVariable('POWERSHELL_UPDATECHECK', 'LTS', 'User') } @@ -18,8 +19,8 @@ $powershellModules = Join-Path $powershellDir "/Modules/" $global:myFiles = @{ completionHelper = Join-Path $powershellDir "bash_complete.sh" apiKeys = Join-Path $powershellDir "api_keys.ps1" - core = Join-Path $powershellAsync "core.ps1" - env = Join-Path $powershellAsync "env.ps1" + core = Join-Path $powershellDir "core.ps1" + env = Join-Path $powershellDir "env.ps1" } $global:myAsync = [ordered]@{ zoxide = Join-Path $powershellAsync "zoxide.ps1" @@ -32,41 +33,48 @@ $global:myAsync = [ordered]@{ } # Load Core -if (Test-Path $global:myFiles.core) { +if (Test-Path $global:myFiles.core) +{ . $global:myFiles.core -} -else { +} else +{ Write-Error "Cant find core module of custom profile (main.ps1)" return } # Load apiKeys -if (Test-Path $global:myFiles.apiKeys) { +if (Test-Path $global:myFiles.apiKeys) +{ . $global:myFiles.apiKeys } # Load Env variables -if (Test-Path $global:myFiles.env) { +if (Test-Path $global:myFiles.env) +{ . $global:myFiles.env } # Add Modules to PS Module load list -if ((Get-OperatingSystem) -eq "windows") { +if ((Get-OperatingSystem) -eq "windows") +{ $env:PSModulePath += "; $powershellModules" -} -else { +} else +{ $env:PSModulePath += ":$powershellModules" } # Ensure all files exist $myFilesAndModules = $myFiles + $myAsync -foreach ($key in $myFilesAndModules.Keys) { - if (-Not (Test-Path $myFilesAndModules[$key])) { +foreach ($key in $myFilesAndModules.Keys) +{ + if (-Not (Test-Path $myFilesAndModules[$key])) + { $global:ProfileIssues += @("Missing File: $key → $($myFilesAndModules[$key])") } } ### Setup Temporary promt -function prompt { +function prompt +{ return "$([char]0x1b)[38;2;239;159;118m[Loading]$([char]0x1b)[0m $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " } @@ -86,13 +94,13 @@ $lastProfileHints = (Get-LastProfileHints).hints Import-Module ProfileAsync # Load with AsyncProfile $AsyncScriptblock = { + . $global:myAsync.catppuccin + . $global:myAsync.ohMyPosh . $global:myAsync.zoxide . $global:myAsync.completion . $global:myAsync.alias . $global:myAsync.chezmoi . $global:myAsync.psprofiler - . $global:myAsync.ohMyPosh - . $global:myAsync.catppuccin Save-ProfileHints } @@ -103,22 +111,28 @@ Import-ProfileAsync $AsyncScriptblock -Delay 200 $time.Stop() # Show any hints from the last session -if ($lastProfileHints.Count -gt 0) { - $hintName = if ($lastProfileHints.Count -eq 1) { "Hint" } else { "Hints" } +if ($lastProfileHints.Count -gt 0) +{ + $hintName = if ($lastProfileHints.Count -eq 1) + { "Hint" + } else + { "Hints" + } Write-Host "$([char]0x1b)[38;2;238;190;190m$($lastProfileHints.Count) $hintName $([char]0x1b)[0mavalible with $([char]0x1b)[38;2;202;158;230mShow-ProfileHints$([char]0x1b)[0m, " -NoNewline } # Pretty print profile load result with timing $elapsedMs = [math]::Round($time.Elapsed.TotalMilliseconds) -if ($elapsedMs -lt 250) { +if ($elapsedMs -lt 250) +{ Write-Host "Profile loaded in $([char]0x1b)[92m$elapsedMs ms$([char]0x1b)[0m" -} -elseif ($elapsedMs -lt 750) { +} elseif ($elapsedMs -lt 750) +{ Write-Host "Profile loaded in $([char]0x1b)[39m$elapsedMs ms$([char]0x1b)[0m" -} -elseif ($elapsedMs -lt 2000) { +} elseif ($elapsedMs -lt 2000) +{ Write-Host "Profile loaded in $([char]0x1b)[93m$elapsedMs ms$([char]0x1b)[0m" -} -else { +} else +{ Write-Host "Profile loaded in $([char]0x1b)[91m$elapsedMs ms$([char]0x1b)[0m" }