Skip to content

Commit

Permalink
Add --appendCommandLine flag for appending to command (#15822)
Browse files Browse the repository at this point in the history
Added --appendCommandLine flag that when set, appends the command to the
preset command in the profile instead of replacing it.

Previously, there was no good way to launch wt while running a command
appended to the set command in the profile. Some uses include profiles
that are set to login or start an application.

Additional comments: Looking for a review, and expecting additional
changes that needs to be done. For example, I am not really sure on how
to include the the option's information in the CallForHelp() screen.
Also, would be great if someone could guide me on including tests for
this new feature. Thanks!

Closes #5528

---------

Co-authored-by: Charles Liu <hliu729@outlook.com>
  • Loading branch information
hanpuliu-charles and Charles Liu authored Aug 24, 2023
1 parent 921d7c3 commit e10b7e4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/cascadia/TerminalApp/AppCommandlineArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,9 @@ void AppCommandlineArgs::_addNewTerminalArgs(AppCommandlineArgs::NewTerminalSubc
subcommand.colorSchemeOption = subcommand.subcommand->add_option("--colorScheme",
_startingColorScheme,
RS_A(L"CmdColorSchemeArgDesc"));

subcommand.appendCommandLineOption = subcommand.subcommand->add_flag("--appendCommandLine", _appendCommandLineOption, RS_A(L"CmdAppendCommandLineDesc"));

// 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 @@ -654,6 +657,10 @@ NewTerminalArgs AppCommandlineArgs::_getNewTerminalArgs(AppCommandlineArgs::NewT
{
args.ColorScheme(winrt::to_hstring(_startingColorScheme));
}
if (*subcommand.appendCommandLineOption)
{
args.AppendCommandLine(_appendCommandLineOption);
}

return args;
}
Expand Down Expand Up @@ -699,6 +706,7 @@ void AppCommandlineArgs::_resetStateToDefault()
_startingTabColor.clear();
_commandline.clear();
_suppressApplicationTitle = false;
_appendCommandLineOption = false;

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

struct NewPaneSubcommand : public NewTerminalSubcommand
Expand Down Expand Up @@ -105,6 +106,7 @@ class TerminalApp::AppCommandlineArgs final

// _commandline will contain the command line with which we'll be spawning a new terminal
std::vector<std::string> _commandline;
bool _appendCommandLineOption{ false };

bool _splitVertical{ false };
bool _splitHorizontal{ false };
Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -840,4 +840,7 @@
<value>Run as Administrator</value>
<comment>This text is displayed on context menu for profile entries in add new tab button.</comment>
</data>
</root>
<data name="CmdAppendCommandLineDesc" xml:space="preserve">
<value>If set, the command will be appended to the profile's default command instead of replacing it.</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
ACTION_ARG(Windows::Foundation::IReference<Windows::UI::Color>, TabColor, nullptr);
ACTION_ARG(Windows::Foundation::IReference<int32_t>, ProfileIndex, nullptr);
ACTION_ARG(winrt::hstring, Profile, L"");
ACTION_ARG(bool, AppendCommandLine, false);
ACTION_ARG(Windows::Foundation::IReference<bool>, SuppressApplicationTitle, nullptr);
ACTION_ARG(winrt::hstring, ColorScheme);
ACTION_ARG(Windows::Foundation::IReference<bool>, Elevate, nullptr);
Expand All @@ -320,6 +321,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
static constexpr std::string_view TabColorKey{ "tabColor" };
static constexpr std::string_view ProfileIndexKey{ "index" };
static constexpr std::string_view ProfileKey{ "profile" };
static constexpr std::string_view AppendCommandLineKey{ "appendCommandLine" };
static constexpr std::string_view SuppressApplicationTitleKey{ "suppressApplicationTitle" };
static constexpr std::string_view ColorSchemeKey{ "colorScheme" };
static constexpr std::string_view ElevateKey{ "elevate" };
Expand All @@ -340,6 +342,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
otherAsUs->_TabColor == _TabColor &&
otherAsUs->_ProfileIndex == _ProfileIndex &&
otherAsUs->_Profile == _Profile &&
otherAsUs->_AppendCommandLine == _AppendCommandLine &&
otherAsUs->_SuppressApplicationTitle == _SuppressApplicationTitle &&
otherAsUs->_ColorScheme == _ColorScheme &&
otherAsUs->_Elevate == _Elevate &&
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 @@ -132,6 +132,7 @@ namespace Microsoft.Terminal.Settings.Model
String TabTitle;
Windows.Foundation.IReference<Windows.UI.Color> TabColor;
String Profile; // Either a GUID or a profile's name if the GUID isn't a match
Boolean AppendCommandLine;

// We use IReference<> to treat some args as nullable where null means
// "use the inherited value". See ProfileIndex,
Expand Down
9 changes: 8 additions & 1 deletion src/cascadia/TerminalSettingsModel/TerminalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// Override commandline, starting directory if they exist in newTerminalArgs
if (!newTerminalArgs.Commandline().empty())
{
defaultSettings.Commandline(newTerminalArgs.Commandline());
if (!newTerminalArgs.AppendCommandLine())
{
defaultSettings.Commandline(newTerminalArgs.Commandline());
}
else
{
defaultSettings.Commandline(defaultSettings.Commandline() + L" " + newTerminalArgs.Commandline());
}
}
if (!newTerminalArgs.StartingDirectory().empty())
{
Expand Down

0 comments on commit e10b7e4

Please sign in to comment.