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

Run exe or shell command without cmd window popup #249

Closed
harg opened this issue Apr 27, 2020 · 14 comments
Closed

Run exe or shell command without cmd window popup #249

harg opened this issue Apr 27, 2020 · 14 comments
Labels
enhancement New feature or request RFNR Ready to be shipped in next release windows Related to the Windows operating system

Comments

@harg
Copy link

harg commented Apr 27, 2020

Thank you for this great software!
On windows, is it possible to run an exe or a shell command without the flashing cmd window?

@federico-terzi
Copy link
Collaborator

Hey @harg,

Thank you for pointing this out, I'll investigate and let you know if I find something. Hopefully we can get this shipped in the next release!

Cheers :)

@federico-terzi federico-terzi added enhancement New feature or request windows Related to the Windows operating system labels Apr 28, 2020
@harg
Copy link
Author

harg commented May 3, 2020

I do not know Rust but you can do it like that in Go : https://play.golang.org/p/2LAEm2LQc-X

This is the win32 api : ShowWindow(... SW_HIDE)
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

@federico-terzi
Copy link
Collaborator

Thank you @harg, this is definitely very useful starting point to solve the problem

@federico-terzi
Copy link
Collaborator

I may have solved this by changing the default shell from CMD to Powershell on Windows (as CMD had also other problems). Would you have any problem with using Powershell?

@harg
Copy link
Author

harg commented May 6, 2020

It's fine for me but I think some users might want to keep cmd...
Is it possible to have both and select the desired shell in the config file? (powershell by default?)

@federico-terzi
Copy link
Collaborator

It's fine for me but I think some users might want to keep cmd...
Is it possible to have both and select the desired shell in the config file? (powershell by default?)

Fair point, the best solution then is to add another param to select the desired shell.
What do you think?

@harg
Copy link
Author

harg commented May 6, 2020

Yep!
There is a third option on Windows : WSL (Windows Subsystem for Linux) but you invoke it via cmd or powershell.

So if you can do somehing like this : Get-ChildItem | wsl grep -i "document"
We are good to go ! :)

@federico-terzi
Copy link
Collaborator

federico-terzi commented May 6, 2020

This is a wonderful idea! I use the WSL all the time but I never thought about integrating it with espanso. That could improve its capability a lot on Windows.

After a bit of testing, I'd say the best way to integrate it with the current espanso implementation is something like:

wsl bash -c "CMD"

In which CMD is the string passed as cmd in the shell extension

@federico-terzi
Copy link
Collaborator

I made a few tests, and unfortunately, the behavior still occurs.
Digging a bit more into the problem it seems like indeed a solution is possible, but is not trivial.
I also tried with the approach suggested in your Go example, but unfortunately even that does flash the CMD window for a short while.

A possible solution would be to use the rust-subprocess library, hopefully there is an option to specify the required flag (I've opened an issue for that).

@federico-terzi
Copy link
Collaborator

@federico-terzi federico-terzi added the help wanted Extra attention is needed label Jun 9, 2020
@BNolet
Copy link
Contributor

BNolet commented Jul 22, 2020

Happy to see this issue is still being worked on or at least an intention :) Was so happy to find a cross-platform solution but the powershell window is a pretty big deal-breaker at least for now

@federico-terzi federico-terzi removed the help wanted Extra attention is needed label Jul 23, 2020
@federico-terzi
Copy link
Collaborator

I made some more experimentation and I might have found the correct approach, so I'll try to ship it in the next release. For reference:

We first need to activate the windows_subsystem to avoid associating a console to espanso, by placing this line as the topmost command in the main file:

#![windows_subsystem = "windows"]

Then, when spawning the powershell/cmd command, use the CONSOLE_NO_WINDOW flag with:

use std::os::windows::process::CommandExt;

Command::new("powershell").arg("-Command")
        .creation_flags(0x08000000)
...

I already tried this flag before but it didn't work as expected. The answer is that without the first windows_subsystem declaration, that flag has no effect.

As a result though, the console output doesn't work anymore (which is useful when developing), so to solve that problem we have to attach to the parent console with:

unsafe {winapi::um::wincon::AttachConsole(0xFFFFFFFF)};

@BNolet
Copy link
Contributor

BNolet commented Jul 25, 2020

wow damn, sounds like a lot of moving parts! Thanks for the update on this one :)

@federico-terzi
Copy link
Collaborator

federico-terzi commented Aug 15, 2020

Just wanted to say, this bug will be finally fixed in the upcoming release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request RFNR Ready to be shipped in next release windows Related to the Windows operating system
Projects
None yet
Development

No branches or pull requests

3 participants