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

[CmdLineArgs] "--keepOpen" to keep the window/tab/pane open after the process/profile has exited. #16497

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

htcfreek
Copy link

@htcfreek htcfreek commented Dec 25, 2023

Summary of the Pull Request

Adding a parameter to wt.exe that stops WT from closing after the sub process has exited.

References and Relevant Issues

Detailed Description of the Pull Request / Additional comments

This PR add the command line argument --keepOpen (short: -o) that stops WT from closing after the sub process has exited. This is done by overriding the Profile termination behavior setting at runtime with Never.

The new parameter also supports to be written to/read from a command line arguments JSON file.

Validation Steps Performed

  • βœ… Tested that the new parameter is recognized on long and short form.
  • πŸ”² Tested the following parameter cases:
Commandline Result Reason
wt.exe --keepOpen ping.exe learn.microsoft.com ❌ wt/profile closes on Exit 0
wt.exe --keepOpen cmd.exe /c echo "test" ❌ wt/profile closes on Exit 0
wt.exe new-tab PowerShell --keepOpen -c Get-Service ❌ append to cmd
wt.exe nt -p "Command Prompt" --keepOpen ❌ wt/profile closes on Exit 0
wt.exe nt -p "PowerShell" --keepOpen ❌ wt/profile closes on Exit 0
wt.exe nt -p "Windows PowerShell" --keepOpen ❌ wt/profile closes on Exit 0
wt.exe -p "Command Prompt" ; split-pane -p "PowerShell" --keepOpen ❌ powershell pane closes on Exit 0
wt.exe -p "Command Prompt" ; split-pane -p "PowerShell" -o ❌ powershell pane closes on Exit 0
wt.exe --keepOpen -p "Command Prompt" ; split-pane -p "PowerShell" ❌ powershell pane closes on Exit 0
wt.exe new-tab PowerShell --keepOpen --appendCommandLine -- Write-Host test ❌ append to commandline
wt.exe new-tab PowerShell -o --appendCommandLine -- Write-Host test ❌ append to commandline
  • πŸ”² Tested that the new parameter stops WT from closing regardless of the current setting choice.
  • πŸ”² Tested that the new parameter not changes the setting permanently.
  • πŸ”² Tested that the net tests succeed.

PR Checklist

@microsoft-github-policy-service microsoft-github-policy-service bot added Issue-Task It's a feature request, but it doesn't really need a major design. Area-Commandline wt.exe's commandline arguments Product-Terminal The new Windows Terminal. labels Dec 25, 2023

This comment has been minimized.

@htcfreek htcfreek changed the title [CmdLineArgs] "--keepOpen" to keep the window/tab/pane open after the process/profile has exitted. [CmdLineArgs] "--keepOpen" to keep the window/tab/pane open after the process/profile has exited. Dec 25, 2023
@@ -367,6 +370,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(json, ColorSchemeKey, args->_ColorScheme);
JsonUtils::GetValueForKey(json, ElevateKey, args->_Elevate);
JsonUtils::GetValueForKey(json, ReloadEnvironmentVariablesKey, args->_ReloadEnvironmentVariables);
JsonUtils::GetValueForKey(json, KeepOpenKey, args->_KeepOpen);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct that this json code and the one in line 395 is used to read form/write to a WT arguments json file? Do I need to update a json schema? (I can't find one.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's pretty much it. The schema can be found at terminal/doc/cascadia /profiles.schema.json

src/cascadia/TerminalSettingsModel/TerminalSettings.cpp Outdated Show resolved Hide resolved
@@ -576,6 +576,8 @@ void AppCommandlineArgs::_addNewTerminalArgs(AppCommandlineArgs::NewTerminalSubc
_inheritEnvironment,
RS_A(L"CmdInheritEnvDesc"));

subcommand.keepOpenOption = subcommand.subcommand->add_flag("-ko,--keepOpen", _keepOpenOption, RS_A(L"CmdKeepOpenDesc"));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that this line also add the new parameter to the help window. But it doesn't do that:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yea that's by design. The help text for wt is different than the help text for wt new-tab. Behind the scenes, we'll secretly treat a wt without a subcommand as a new-tab subcommand, but the automatically generated help text for CLI11 doesn't know how to handle that πŸ˜…

@htcfreek

This comment was marked as outdated.

@htcfreek
Copy link
Author

htcfreek commented Dec 31, 2023

@zadjii-msft
Status update: I have two important problems and need your help here.

  1. wt.exe new-tab "PowerShell" --keepOpen -c Get-Service parses to the following command line: PowerShell --keepOpen -c Get-Service (Same when using the parameter --appendCommandLine -- Write-Host test.)
  2. The settings of the profile are using the default value/value form settings.json and my parameter is ignored: What I need to know is, where can I manipulate the behavior of the profile/terminal?
  3. [not important] wt.xe -? is incomplete and does not show the new parameter. (See my code comment.)

@htcfreek
Copy link
Author

htcfreek commented May 7, 2024

@zadjii-msft , @DHowett
Can you please look at this PR. I am not sure if it makes sense to keep it open as I don't know why it isn't working? Please give me some feedback.

@zadjii-msft
Copy link
Member

My apologies! I think this one slipped through the cracks while I was off being distracted writing sudo. I can try and take a spin through the review - though, I might be a little more brief than usual until after Build (May 21-23)

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly the rest of this looks like it should work

@@ -341,6 +346,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
_RepositionCursorWithMouse = profile.RepositionCursorWithMouse();

_ReloadEnvironmentVariables = profile.ReloadEnvironmentVariables();

_CloseOnExit = profile.CloseOnExit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems... weird. This should already be plumbed through to the TermControl somewhere else...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. It's not actually used by the TermControl. The pane itself handles that, in this blob of code:

if (_profile)
{
if (_isDefTermSession && _profile.CloseOnExit() == CloseOnExitMode::Automatic)
{
// For 'automatic', we only care about the connection state if we were launched by Terminal
// Since we were launched via defterm, ignore the connection state (i.e. we treat the
// close on exit mode as 'always', see GH #13325 for discussion)
Close();
}
const auto mode = _profile.CloseOnExit();
if ((mode == CloseOnExitMode::Always) ||
((mode == CloseOnExitMode::Graceful || mode == CloseOnExitMode::Automatic) && newConnectionState == ConnectionState::Closed))
{
Close();
}
}

Looks like that's specifically looking up the CloseOnExit value out of the profile, not the TerminalSettings. I bet you could update that to use the value from the TerminalSettings too.

This code has an example in that file of getting settings from the cache:

if (const auto& settings{ _cache.TryLookup(_profile) })

That'll get you a TerminalSettingsCreateResult, who's .DefaultSettings() will have the value you're looking for

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label May 7, 2024
@zadjii-msft
Copy link
Member

wt.exe new-tab "PowerShell" --keepOpen -c Get-Service

You'll probably want wt.exe new-tab -p "PowerShell" --keepOpen --appendCommandLine -c Get-Service (note the -p, to make sure it uses the PowerShell profile).

@microsoft-github-policy-service microsoft-github-policy-service bot added the No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. label May 14, 2024
@htcfreek

This comment has been minimized.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. labels May 14, 2024
@zadjii-msft zadjii-msft removed the Needs-Attention The core contributors need to come back around and look at this ASAP. label May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Commandline wt.exe's commandline arguments Issue-Task It's a feature request, but it doesn't really need a major design. Product-Terminal The new Windows Terminal.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a parameter for --closeOnExit
2 participants