From 4556e38e32c52862ecf3d88e7b8faeae231b44a2 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Tue, 28 Jan 2025 22:06:58 -0500 Subject: [PATCH] Save and restore space indices for new PowerShell sessions (fixes up arrow commands) --- completions/powershell7plus/mutn.ps1 | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/completions/powershell7plus/mutn.ps1 b/completions/powershell7plus/mutn.ps1 index a9395b0..bdd5113 100644 --- a/completions/powershell7plus/mutn.ps1 +++ b/completions/powershell7plus/mutn.ps1 @@ -3,6 +3,21 @@ $global:cliMUTN_entriesToSpaceIndicesMap = @{} if (!$IsWindows) { # since the executable has the same name as the completion function (on non-Windows platforms), its path must be stored before the function is registered $MUTNExecutablePath = (Get-Command mutn).Path + $spaceIndicesFilePath = '~/.config/libmutton/psCompletionsCache.json' +} else { + $spaceIndicesFilePath = '~\AppData\Local\libmutton\psCompletionsCache.json' +} + +function cliMUTN-loadSpaceIndices { + if (Test-Path $global:spaceIndicesFilePath) { + $jsonContent = Get-Content -Path $global:spaceIndicesFilePath -Raw + $global:cliMUTN_entriesToSpaceIndicesMap = ConvertFrom-Json -InputObject $jsonContent -AsHashtable + } +} + +function cliMUTN-saveSpaceIndices { + $jsonContent = $global:cliMUTN_entriesToSpaceIndicesMap | ConvertTo-Json -Compress + $jsonContent | Set-Content -Path $global:spaceIndicesFilePath } function cliMUTN-getSpaceIndices { @@ -59,8 +74,9 @@ function cliMUTN-entryCompleter { $spaceIndices = cliMUTN-getSpaceIndices -inputString $_ $replacedEntry = $_ -replace ' ', '_' $replacedEntry - $cliMUTN_entriesToSpaceIndicesMap[$replacedEntry] = $spaceIndices + $global:cliMUTN_entriesToSpaceIndicesMap[$replacedEntry] = $spaceIndices } + cliMUTN-saveSpaceIndices } $trimmedPaths | Where-Object { $_ -like "$wordToComplete*" } } @@ -106,6 +122,8 @@ function mutn { if ($IsWindows) { Invoke-Expression -Command ('mutn.exe ' + $escapedEntry, $argument, $option).Trim() } else { - Invoke-Expression -Command ($MUTNExecutablePath + ' ' + $escapedEntry, $argument, $option).Trim() + Invoke-Expression -Command ($global:MUTNExecutablePath + ' ' + $escapedEntry, $argument, $option).Trim() } } + +cliMUTN-loadSpaceIndices