Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle shell arguments on Windows #214

Merged
merged 9 commits into from
Oct 3, 2023
Merged

Conversation

ndtoan96
Copy link
Contributor

Fix #192

@ndtoan96 ndtoan96 changed the title Handle shell arguments on Windows fix: Handle shell arguments on Windows Sep 23, 2023
@sxyazi
Copy link
Owner

sxyazi commented Sep 24, 2023

I've made a simple test with:

cmdexpand::expand_cmd(r#"  a "b \"%1\""  "#, &["c", "d"])

And got:

  a "b \"

Is this expected?

@ndtoan96
Copy link
Contributor Author

I'm aware that it has problem with escaped quote, but I spent yesterday fixing it in my crate and making documentation so haven't ported it here. Will find time to do it later.

@ndtoan96
Copy link
Contributor Author

Just realized this expand thing only works with no-space argument. I tried to open a text file "D:\a b.txt" by select the file and run %EDITOR% %1. Even though it expands correctly to %EDITOR% "D:\a b.txt", the editor actually open 2 files "D:\a and b.txt".

Pretty sure it's because the way cmd /C works, argument should be feed one by one instead of all in one.

@sxyazi
Copy link
Owner

sxyazi commented Sep 24, 2023

I have to sleep, will investigate it tomorrow. =)

@sxyazi
Copy link
Owner

sxyazi commented Sep 25, 2023

I just tested it, and it indeed doesn't work as expected:

cmd /C "nvim 'rust fmt.toml'"

This results in opening two files ('rust and fmt.toml'), and cmd doesn't correctly recognize the quotes. I'm thinking of two simple workarounds now:

  • Use PowerShell instead of cmd -- I found that PowerShell can work correctly:

    powershell -Command "& nvim 'rust fmt.toml'"

    However, this requires PowerShell to be present on the user's system, it requires a minimum of Windows 8. In fact, I haven't tested whether Yazi works correctly on Windows 7 and earlier systems yet...

  • Parse only the first-level placeholders, and, if they exist, pass them as cmd parameters:

    From:

    cmd /C "nvim 'rust fmt.toml'"

    To:

    Command::new("cmd").args(["/C", "nvim", "rust fmt.toml"])

    It was similar to Yazi's current behavior, but the difference is that this time we can use nom to parse more accurately which first-level placeholders are available.

Apart from these, I'm not sure if there are better solutions. Any ideas?

@ndtoan96
Copy link
Contributor Author

I prefer the second approach. Reason is that user can use environment variable in the form %SOME_VAR%, which is nicer, and more well known than $env:SOME_VAR in powershell.

@sxyazi
Copy link
Owner

sxyazi commented Sep 26, 2023

Oh, that makes sense, It's the first time I've seen this kind of peculiar format 🤣

@ndtoan96
Copy link
Contributor Author

I thought it would be easy enough to make a manual parser. The code doesn't look pretty but it works (at least if users don't do anything too crazy).😅

core/src/external/shell.rs Outdated Show resolved Hide resolved
@sxyazi sxyazi changed the title fix: Handle shell arguments on Windows fix: handle shell arguments on Windows Oct 3, 2023
@sxyazi
Copy link
Owner

sxyazi commented Oct 3, 2023

I've made some simplifications, merging now. Thanks!

@sxyazi sxyazi merged commit d721b01 into sxyazi:main Oct 3, 2023
3 checks passed
sxyazi pushed a commit that referenced this pull request Oct 4, 2023
sxyazi pushed a commit that referenced this pull request Oct 4, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Path(s) are added automatically to shell command on Windows
2 participants