From cfe8a1e1baff89db4d718c0fc32dc0752e42cc70 Mon Sep 17 00:00:00 2001 From: iff Date: Wed, 19 Mar 2025 22:42:19 +0100 Subject: [PATCH] fix: pwsh version <7 and removed cnf --- README.md | 4 ++-- core/src/shell.rs | 53 +++++++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 788f6df..23a37df 100644 --- a/README.md +++ b/README.md @@ -61,12 +61,12 @@ Please follow the instruction for your shell: > Append the following output to your profile: > ```pwsh -> pay-respects pwsh --alias [] [--nocnf] +> pay-respects pwsh --alias [] > ``` > Or directly pipe the output to your profile: > ```pwsh -> pay-respects pwsh --alias [] [--nocnf] >> $PROFILE +> pay-respects pwsh --alias [] >> $PROFILE > ``` diff --git a/core/src/shell.rs b/core/src/shell.rs index 03b7df1..d83c7ad 100644 --- a/core/src/shell.rs +++ b/core/src/shell.rs @@ -538,7 +538,12 @@ def --env {} [] {{ # fetch command and error from session history only when not in cnf mode if ($env:_PR_MODE -ne 'cnf') {{ $env:_PR_LAST_COMMAND = ({}); - $err = Get-Error; + if ($PSVersionTable.PSVersion.Major -ge 7) {{ + $err = Get-Error; + if ($env:_PR_LAST_COMMAND -eq $err.InvocationInfo.Line) {{ + $env:_PR_ERROR_MSG = $err.Exception.Message + }} + }} if ($env:_PR_LAST_COMMAND -eq $err.InvocationInfo.Line) {{ $env:_PR_ERROR_MSG = $err.Exception.Message }} @@ -638,28 +643,30 @@ end ); } "pwsh" => { - initialize = format!( - r#"{} -$ExecutionContext.InvokeCommand.CommandNotFoundAction = -{{ - param( - [string] - $commandName, - [System.Management.Automation.CommandLookupEventArgs] - $eventArgs - ) - # powershell does not support run command with specific environment variables - # but you must set global variables. so we are memorizing the current mode and the alias function will reset it later. - $env:_PR_PWSH_ORIGIN_MODE=$env:_PR_MODE; - $env:_PR_MODE='cnf'; - # powershell may search command with prefix 'get-' or '.\' first when this hook is hit, strip them - $env:_PR_LAST_COMMAND=$commandName -replace '^get-|\.\\',''; - $eventArgs.Command = (Get-Command {}); - $eventArgs.StopSearch = $True; -}} -"#, - initialize, alias - ) + // usage with pwsh is limited as we cannot get arguments + // furthermore there is recursion +// initialize = format!( +// r#"{} +// $ExecutionContext.InvokeCommand.CommandNotFoundAction = +// {{ +// param( +// [string] +// $commandName, +// [System.Management.Automation.CommandLookupEventArgs] +// $eventArgs +// ) +// # powershell does not support run command with specific environment variables +// # but you must set global variables. so we are memorizing the current mode and the alias function will reset it later. +// $env:_PR_PWSH_ORIGIN_MODE=$env:_PR_MODE; +// $env:_PR_MODE='cnf'; +// # powershell may search command with prefix 'get-' or '.\' first when this hook is hit, strip them +// $env:_PR_LAST_COMMAND=$commandName -replace '^get-|\.\\',''; +// $eventArgs.Command = (Get-Command {}); +// $eventArgs.StopSearch = $True; +// }} +// "#, +// initialize, alias +// ) } _ => { println!("Unsupported shell: {}", shell);