How to exit PowerShell on a keypress, after the program has been executed?

555 Views Asked by At

I am trying to build a custom sublime-build that executes c++ programs in powershell. I want powershell to exit itself on pressing enter or any other key. How can this be done ?

This is my sublime-build so far..


 {
    "cmd": ["g++", "${file}", "-o", "${file_base_name}.exe"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "cmd": ["start", "powershell", "-NoExit","& '${file_path}/${file_base_name}.exe'"],
    "shell": true,
    "variants":
    [
        {
            "name": "Run",
            "cmd": ["start", "powershell", "-NoExit","& ${file_path}/${file_base_name}.exe"],
            "shell": true
        }
    ]
}

This is how it looks.. enter image description here

Thanks

1

There are 1 best solutions below

0
postanote On

Give this a shot.

PSHostRawUserInterface.ReadKey Method https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.host.pshostrawuserinterface.readkey?view=powershellsdk-7.0.0

 {
    "cmd": ["g++", "${file}", "-o", "${file_base_name}.exe"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "cmd": ["start", "powershell", "-NoExit","& '${file_path}/${file_base_name}.exe';$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');Exit"],
    "shell": true,
    "variants":
    [
        {
            "name": "Run",
            "cmd": ["start", "powershell", "-NoExit","& ${file_path}/${file_base_name}.exe;$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');Exit"],
            "shell": true
        }
    ]
}