lf icon indicating copy to clipboard operation
lf copied to clipboard

How LF send args in powershell?

Open KawaiiSelbst opened this issue 1 year ago • 3 comments

I've been struggling for hours and can't figure out how to get the arguments in the &{{}} block, my shell is powershell, I've tried $args[0], $args.0, $args:0, $0, $env:0, $env:args[0], $env:lf_args[0], $_ how do I get the arguments into the script?!?!

cmd z &{{ 
    $result=((zoxide query --exclude $PWD "$arg[0]") -replace "/", "//") 
    lf -remote "send $env:id cd '$result'" 
}} 
cmd zz &{{{ 
    lf -remote "send $env:id echo'$arg[0]'" 
}} 

Returns nothing with any of the above options

KawaiiSelbst avatar Nov 20 '23 01:11 KawaiiSelbst

It was discovered that for the -c flag, the arguments should be supplied in this form [-Command { - | <script-block> [-args <arg-array>] | <string> [<CommandParameters>] } ] just a couple of days ago a new version of powershell 7.4 was released, which introduced the experimental -CommandWithArgs | -cwa flag and if you write set shellflag "-cwa" in the config, everything starts to work

cmd z ${{
	$a = $args[0]
	lf -remote "send $env:id echo '$a'"	
}}

Because now the first line in the command is considered a command, and the rest of the space-separated commands are considered arguments and are collected in the args array. Up until this point, it was virtually impossible for powershell to get the arguments to the commands that were expected without fixing the source code. Another issue is that for older versions, and for the version that comes with Windows by default, code changes are still required. And experimental flag may work unstable or may be removed again.

KawaiiSelbst avatar Nov 20 '23 03:11 KawaiiSelbst

Also powershell for some reason when getting non-Latin characters from a third-party program and saving them to a variable converts them to Ascii, so if there are non-Latin characters in directories, then [Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("UTF-8") should be added.

Total:

set shellflag "-cwa"

cmd z ${{
	[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("UTF-8")
	$result = ((zoxide query --exclude $PWD $args[0]) -replace "/", "//")
	lf -remote "send $env:id cd '$result'"
}}

And Powershell version at least 7.4

KawaiiSelbst avatar Nov 20 '23 04:11 KawaiiSelbst

I've just been saving powershell scripts in a folder and calling them with mappings. Example lfrc

map e $Edit.ps1

Edit.ps1

$FilePath = $Env:f
if($Env:LfInNvim -eq 'True'){
  Start-Process -WindowStyle hidden -FilePath 'wezterm.exe' -ArgumentList "cli", "split-pane", "-- nvim $($FilePath)"
  Start-Process -WindowStyle hidden -FilePath 'lf.exe' -ArgumentList '-remote "send quit"'
  Start-Process -WindowStyle hidden -FilePath 'wezterm.exe' -ArgumentList 'cli kill-pane'
}else{
  Start-Process -WindowStyle hidden -FilePath 'lf.exe' -ArgumentList '-remote "send $nvim $Env:f"'
}

Starting all these processes probably has an adverse effect on my performance long-term, so I am totally down to trying out this -cwa thing.

jamesonBradfield avatar Jan 23 '24 21:01 jamesonBradfield