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 17 commits into
base: main
Choose a base branch
from
Draft
5 changes: 5 additions & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,11 @@
"type": "boolean",
"default": false,
"description": "This will override the profile's `elevate` setting."
},
"keepOpen": {
"type": "boolean",
"default": false,
"description": "This will override the profile's `closeOnExit` setting to keep the tab/pane/window open."
}
},
"type": "object"
Expand Down
8 changes: 8 additions & 0 deletions src/cascadia/TerminalApp/AppCommandlineArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ void AppCommandlineArgs::_addNewTerminalArgs(AppCommandlineArgs::NewTerminalSubc
_inheritEnvironment,
RS_A(L"CmdInheritEnvDesc"));

subcommand.keepOpenOption = subcommand.subcommand->add_flag("-o,--keepOpen", _keepOpenOption, RS_A(L"CmdKeepOpenDesc"));

// Using positionals_at_end allows us to support "wt new-tab -d wsl -d Ubuntu"
// without CLI11 thinking that we've specified -d twice.
// There's an alternate construction where we make all subcommands "prefix commands",
Expand Down Expand Up @@ -685,6 +687,11 @@ NewTerminalArgs AppCommandlineArgs::_getNewTerminalArgs(AppCommandlineArgs::NewT
}
args.ReloadEnvironmentVariables(!inheritEnv);

if (*subcommand.keepOpenOption)
{
args.KeepOpen(_keepOpenOption);
}

return args;
}

Expand Down Expand Up @@ -731,6 +738,7 @@ void AppCommandlineArgs::_resetStateToDefault()
_commandline.clear();
_suppressApplicationTitle = false;
_appendCommandLineOption = false;
_keepOpenOption = false;

_splitVertical = false;
_splitHorizontal = false;
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/AppCommandlineArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TerminalApp::AppCommandlineArgs final
CLI::Option* colorSchemeOption;
CLI::Option* appendCommandLineOption;
CLI::Option* inheritEnvOption;
CLI::Option* keepOpenOption;
};

struct NewPaneSubcommand : public NewTerminalSubcommand
Expand Down Expand Up @@ -112,6 +113,8 @@ class TerminalApp::AppCommandlineArgs final
std::vector<std::string> _commandline;
bool _appendCommandLineOption{ false };

bool _keepOpenOption{ false };

bool _splitVertical{ false };
bool _splitHorizontal{ false };
bool _splitDuplicate{ false };
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -898,4 +898,7 @@
<data name="RestartConnectionToolTip" xml:space="preserve">
<value>Restart the active pane connection</value>
</data>
<data name="CmdKeepOpenDesc" xml:space="preserve">
<value>If set, the window/tab/pane will stay open after the command has exited. (It is he same as setting the profile termination behavior to never.)</value>
</data>
</root>
12 changes: 12 additions & 0 deletions src/cascadia/TerminalApp/TerminalPaneContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@
co_return;
}

// We try to read the setting from the command line arguments
if (const auto& settings{ _cache.TryLookup(_profile) })
{
const auto closeMode = settings.DefaultSettings().CloseOnExit();
Copy link
Author

@htcfreek htcfreek Jun 24, 2024

Choose a reason for hiding this comment

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

@zadjii-msft
I have updated the code to handle the command line setting now. But it doesn't work because closeMode is always the value from Profile settings and never the updated value after command line parsing. How can I get the updated value that I manipulate in the command line parser?

Copy link
Author

Choose a reason for hiding this comment

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

Opened issue #17473.


// If the command line argument "keepOpen" is set to kepp the pane/tab/window open we stop further execution here.

Check failure on line 243 in src/cascadia/TerminalApp/TerminalPaneContent.cpp

View workflow job for this annotation

GitHub Actions / Spell checking

`kepp` is not a recognized word. (unrecognized-spelling)
if (closeMode == CloseOnExitMode::Never)
{
co_return;
}
}

if (_profile)
{
const auto mode = _profile.CloseOnExit();
Expand Down
7 changes: 7 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
ACTION_ARG(winrt::hstring, ColorScheme);
ACTION_ARG(Windows::Foundation::IReference<bool>, Elevate, nullptr);
ACTION_ARG(Windows::Foundation::IReference<bool>, ReloadEnvironmentVariables, nullptr);
ACTION_ARG(bool, KeepOpen, false)
ACTION_ARG(uint64_t, ContentId);

static constexpr std::string_view CommandlineKey{ "commandline" };
Expand All @@ -383,6 +384,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
static constexpr std::string_view ColorSchemeKey{ "colorScheme" };
static constexpr std::string_view ElevateKey{ "elevate" };
static constexpr std::string_view ReloadEnvironmentVariablesKey{ "reloadEnvironmentVariables" };
static constexpr std::string_view KeepOpenKey{ "keepOpen" };
static constexpr std::string_view ContentKey{ "__content" };

public:
Expand All @@ -405,6 +407,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
otherAsUs->_ColorScheme == _ColorScheme &&
otherAsUs->_Elevate == _Elevate &&
otherAsUs->_ReloadEnvironmentVariables == _ReloadEnvironmentVariables &&
otherAsUs->_KeepOpen == _KeepOpen &&
otherAsUs->_ContentId == _ContentId;
}
return false;
Expand All @@ -424,6 +427,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);
htcfreek marked this conversation as resolved.
Show resolved Hide resolved
JsonUtils::GetValueForKey(json, ContentKey, args->_ContentId);
return *args;
}
Expand All @@ -446,6 +450,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::SetValueForKey(json, ColorSchemeKey, args->_ColorScheme);
JsonUtils::SetValueForKey(json, ElevateKey, args->_Elevate);
JsonUtils::SetValueForKey(json, ReloadEnvironmentVariablesKey, args->_ReloadEnvironmentVariables);
JsonUtils::SetValueForKey(json, KeepOpenKey, args->_KeepOpen);
JsonUtils::SetValueForKey(json, ContentKey, args->_ContentId);
return json;
}
Expand All @@ -463,6 +468,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
copy->_ColorScheme = _ColorScheme;
copy->_Elevate = _Elevate;
copy->_ReloadEnvironmentVariables = _ReloadEnvironmentVariables;
copy->_KeepOpen = _KeepOpen;
copy->_ContentId = _ContentId;
return *copy;
}
Expand All @@ -484,6 +490,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
h.write(ColorScheme());
h.write(Elevate());
h.write(ReloadEnvironmentVariables());
h.write(KeepOpen());
h.write(ContentId());
}
};
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ namespace Microsoft.Terminal.Settings.Model
String Profile; // Either a GUID or a profile's name if the GUID isn't a match
Guid SessionId;
Boolean AppendCommandLine;
Boolean KeepOpen;

// We use IReference<> to treat some args as nullable where null means
// "use the inherited value". See ProfileIndex,
Expand Down
7 changes: 7 additions & 0 deletions src/cascadia/TerminalSettingsModel/TerminalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
defaultSettings.ReloadEnvironmentVariables(newTerminalArgs.ReloadEnvironmentVariables().Value());
}

if (newTerminalArgs.KeepOpen())
{
defaultSettings.CloseOnExit(CloseOnExitMode::Never);
}
}

return settingsPair;
Expand Down Expand Up @@ -347,6 +352,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
_RepositionCursorWithMouse = profile.RepositionCursorWithMouse();

_ReloadEnvironmentVariables = profile.ReloadEnvironmentVariables();

_CloseOnExit = profile.CloseOnExit();
htcfreek marked this conversation as resolved.
Show resolved Hide resolved
}

// Method Description:
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/TerminalSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

INHERITABLE_SETTING(Model::TerminalSettings, bool, ReloadEnvironmentVariables, true);

INHERITABLE_SETTING(Model::TerminalSettings, CloseOnExitMode, CloseOnExit, CloseOnExitMode::Automatic);

private:
std::optional<std::array<Microsoft::Terminal::Core::Color, COLOR_TABLE_SIZE>> _ColorTable;
std::span<Microsoft::Terminal::Core::Color> _getColorTableImpl();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/TerminalSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ namespace Microsoft.Terminal.Settings.Model

Boolean Elevate;
Boolean ReloadEnvironmentVariables;
CloseOnExitMode CloseOnExit;
};
}
Loading