From 1cdb390a0b11cbb071beca45fb1190ef4fd774b6 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Thu, 23 Apr 2020 11:38:53 -0500 Subject: [PATCH 1/3] Add support for displaying the version with `wt --version` --- .../TerminalApp/AppCommandlineArgs.cpp | 48 ++++++++++++++++ src/cascadia/TerminalApp/AppCommandlineArgs.h | 6 ++ src/cascadia/TerminalApp/AppLogic.cpp | 13 ++++- src/cascadia/TerminalApp/AppLogic.h | 3 +- src/cascadia/TerminalApp/AppLogic.idl | 6 +- .../Resources/en-US/Resources.resw | 57 ++++++++++--------- src/cascadia/TerminalApp/TerminalPage.cpp | 19 ++++++- src/cascadia/TerminalApp/TerminalPage.h | 3 +- src/cascadia/TerminalApp/TerminalPage.idl | 3 +- src/cascadia/WindowsTerminal/AppHost.cpp | 7 ++- 10 files changed, 128 insertions(+), 37 deletions(-) diff --git a/src/cascadia/TerminalApp/AppCommandlineArgs.cpp b/src/cascadia/TerminalApp/AppCommandlineArgs.cpp index e25363449af..728b5b5bc09 100644 --- a/src/cascadia/TerminalApp/AppCommandlineArgs.cpp +++ b/src/cascadia/TerminalApp/AppCommandlineArgs.cpp @@ -18,6 +18,22 @@ AppCommandlineArgs::AppCommandlineArgs() _resetStateToDefault(); } +// Method Description: +// - Set the application name and version string for the application. If the +// user calls `wt --version`, we'll display these strings by setting the exit +// message (the string returned by GetExitMessage). +// Arguments: +// - applicationName: a hstring containing the application name +// - versionString: a hstring containing the version string +// Return Value: +// - +void AppCommandlineArgs::SetVersionString(const winrt::hstring& applicationName, + const winrt::hstring& versionString) +{ + _applicationName = winrt::to_string(applicationName); + _versionString = winrt::to_string(versionString); +} + // Method Description: // - Attempt to parse a given command as a single commandline. If the command // doesn't have a subcommand, we'll try parsing the commandline again, as a @@ -140,6 +156,11 @@ int AppCommandlineArgs::_handleExit(const CLI::App& command, const CLI::Error& e { _exitMessage = err.str(); } + + // We're displaying an error message - we should always exit instead of + // actually starting the Terminal. + _shouldExitEarly = true; + return result; } @@ -151,6 +172,18 @@ int AppCommandlineArgs::_handleExit(const CLI::App& command, const CLI::Error& e // - void AppCommandlineArgs::_buildParser() { + auto versionCallback = [this](int64_t /*count*/) { + // Set our message to display the application name and the current verison. + _exitMessage = _applicationName + "\n" + _versionString; + + // Theoretically, we don't need to exit now, since this isn't really an + // error case. However, in practice, it feels weird to have `wt -v` open + // a new tab, and makes enough sense that `wt -v ; split-pane` (or + // whatever) just displays the version and exits. + _shouldExitEarly = true; + }; + _app.add_flag_function("-v,--version", versionCallback, RS_A(L"CmdVersionDesc")); + _buildNewTabParser(); _buildSplitPaneParser(); _buildFocusTabParser(); @@ -388,6 +421,8 @@ void AppCommandlineArgs::_resetStateToDefault() _focusTabIndex = -1; _focusNextTab = false; _focusPrevTab = false; + + // _requestedVersion = false; } // Function Description: @@ -540,6 +575,19 @@ const std::string& AppCommandlineArgs::GetExitMessage() return _exitMessage; } +// Method Description: +// - Returns true if we should exit the application before even starting the +// window. We might want to do this if we're displaying an error message or +// the version string, or if we want to open the settings file. +// Arguments: +// - +// Return Value: +// - true iff we should exit the application before even starting the window +bool AppCommandlineArgs::ShouldExitEarly() const noexcept +{ + return _shouldExitEarly; +} + // Method Description: // - Ensure that the first command in our list of actions is a NewTab action. // This makes sure that if the user passes a commandline like "wt split-pane diff --git a/src/cascadia/TerminalApp/AppCommandlineArgs.h b/src/cascadia/TerminalApp/AppCommandlineArgs.h index f315f42f0d8..bb798226d66 100644 --- a/src/cascadia/TerminalApp/AppCommandlineArgs.h +++ b/src/cascadia/TerminalApp/AppCommandlineArgs.h @@ -36,6 +36,9 @@ class TerminalApp::AppCommandlineArgs final void ValidateStartupCommands(); std::deque& GetStartupActions(); const std::string& GetExitMessage(); + bool ShouldExitEarly() const noexcept; + + void SetVersionString(const winrt::hstring& applicationName, const winrt::hstring& versionString); private: static const std::wregex _commandDelimiterRegex; @@ -79,6 +82,9 @@ class TerminalApp::AppCommandlineArgs final std::deque _startupActions; std::string _exitMessage; + bool _shouldExitEarly{ false }; + std::string _applicationName{ "" }; + std::string _versionString{ "" }; winrt::TerminalApp::NewTerminalArgs _getNewTerminalArgs(NewTerminalSubcommand& subcommand); void _addNewTerminalArgs(NewTerminalSubcommand& subcommand); diff --git a/src/cascadia/TerminalApp/AppLogic.cpp b/src/cascadia/TerminalApp/AppLogic.cpp index 85d57ea9bb8..aeca9cad801 100644 --- a/src/cascadia/TerminalApp/AppLogic.cpp +++ b/src/cascadia/TerminalApp/AppLogic.cpp @@ -880,15 +880,24 @@ namespace winrt::TerminalApp::implementation return 0; } - winrt::hstring AppLogic::EarlyExitMessage() + winrt::hstring AppLogic::ParseCommandlineMessage() { if (_root) { - return _root->EarlyExitMessage(); + return _root->ParseCommandlineMessage(); } return { L"" }; } + bool AppLogic::ShouldExitEarly() + { + if (_root) + { + return _root->ShouldExitEarly(); + } + return false; + } + winrt::hstring AppLogic::ApplicationDisplayName() const { try diff --git a/src/cascadia/TerminalApp/AppLogic.h b/src/cascadia/TerminalApp/AppLogic.h index dfff44f18fe..767b704248c 100644 --- a/src/cascadia/TerminalApp/AppLogic.h +++ b/src/cascadia/TerminalApp/AppLogic.h @@ -28,7 +28,8 @@ namespace winrt::TerminalApp::implementation [[nodiscard]] std::shared_ptr<::TerminalApp::CascadiaSettings> GetSettings() const noexcept; int32_t SetStartupCommandline(array_view actions); - winrt::hstring EarlyExitMessage(); + winrt::hstring ParseCommandlineMessage(); + bool ShouldExitEarly(); winrt::hstring ApplicationDisplayName() const; winrt::hstring ApplicationVersion() const; diff --git a/src/cascadia/TerminalApp/AppLogic.idl b/src/cascadia/TerminalApp/AppLogic.idl index bd01b43e49a..513f6c543e7 100644 --- a/src/cascadia/TerminalApp/AppLogic.idl +++ b/src/cascadia/TerminalApp/AppLogic.idl @@ -13,7 +13,8 @@ namespace TerminalApp MaximizedMode, }; - [default_interface] runtimeclass AppLogic: IF7Listener { + [default_interface] runtimeclass AppLogic : IF7Listener + { AppLogic(); // For your own sanity, it's better to do setup outside the ctor. @@ -28,7 +29,8 @@ namespace TerminalApp Boolean IsElevated(); Int32 SetStartupCommandline(String[] commands); - String EarlyExitMessage { get; }; + String ParseCommandlineMessage { get; }; + Boolean ShouldExitEarly { get; }; void LoadSettings(); Windows.UI.Xaml.UIElement GetRoot(); diff --git a/src/cascadia/TerminalApp/Resources/en-US/Resources.resw b/src/cascadia/TerminalApp/Resources/en-US/Resources.resw index c31ad6448a5..14489c2840f 100644 --- a/src/cascadia/TerminalApp/Resources/en-US/Resources.resw +++ b/src/cascadia/TerminalApp/Resources/en-US/Resources.resw @@ -1,17 +1,17 @@  - @@ -221,6 +221,9 @@ Open in the given directory instead of the profile's set "startingDirectory" {Locked="\"startingDirectory\""} + + Display the application version + Press the button to open a new terminal tab with your default profile. Open the flyout to select which profile you want to open. diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index d7c143a0670..71ba8ec25e3 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -1631,6 +1631,8 @@ namespace winrt::TerminalApp::implementation // or 0. (see TerminalPage::_ParseArgs) int32_t TerminalPage::SetStartupCommandline(winrt::array_view args) { + _appArgs.SetVersionString(ApplicationDisplayName(), ApplicationVersion()); + return _ParseArgs(args); } @@ -1732,16 +1734,31 @@ namespace winrt::TerminalApp::implementation // message. If there were no errors, this message will be blank. // - If the user requested help on any command (using --help), this will // contain the help message. + // - If the user requested the version number (using --version), this will + // contain the version string. // Arguments: // - // Return Value: // - the help text or error message for the provided commandline, if one // exists, otherwise the empty string. - winrt::hstring TerminalPage::EarlyExitMessage() + winrt::hstring TerminalPage::ParseCommandlineMessage() { return winrt::to_hstring(_appArgs.GetExitMessage()); } + // Method Description: + // - Returns true if we should exit the application before even starting the + // window. We might want to do this if we're displaying an error message or + // the version string, or if we want to open the settings file. + // Arguments: + // - + // Return Value: + // - true iff we should exit the application before even starting the window + bool TerminalPage::ShouldExitEarly() + { + return _appArgs.ShouldExitEarly(); + } + // Method Description: // - Returns a com_ptr to the implementation type of the tab at the given index // Arguments: diff --git a/src/cascadia/TerminalApp/TerminalPage.h b/src/cascadia/TerminalApp/TerminalPage.h index a7d37c138b1..168eaf0c765 100644 --- a/src/cascadia/TerminalApp/TerminalPage.h +++ b/src/cascadia/TerminalApp/TerminalPage.h @@ -48,7 +48,8 @@ namespace winrt::TerminalApp::implementation void CloseWindow(); int32_t SetStartupCommandline(winrt::array_view args); - winrt::hstring EarlyExitMessage(); + winrt::hstring ParseCommandlineMessage(); + bool ShouldExitEarly(); // -------------------------------- WinRT Events --------------------------------- DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(TitleChanged, _titleChangeHandlers, winrt::Windows::Foundation::IInspectable, winrt::hstring); diff --git a/src/cascadia/TerminalApp/TerminalPage.idl b/src/cascadia/TerminalApp/TerminalPage.idl index dc4b7820d6a..ef7ca699de0 100644 --- a/src/cascadia/TerminalApp/TerminalPage.idl +++ b/src/cascadia/TerminalApp/TerminalPage.idl @@ -11,7 +11,8 @@ namespace TerminalApp TerminalPage(); Int32 SetStartupCommandline(String[] commands); - String EarlyExitMessage { get; }; + String ParseCommandlineMessage { get; }; + Boolean ShouldExitEarly { get; }; // XAML bound properties String ApplicationDisplayName { get; }; diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp index adf042ba22f..6c300529955 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -102,7 +102,7 @@ void AppHost::_HandleCommandlineArgs() } const auto result = _logic.SetStartupCommandline({ args }); - const auto message = _logic.EarlyExitMessage(); + const auto message = _logic.ParseCommandlineMessage(); if (!message.empty()) { const auto displayHelp = result == 0; @@ -115,7 +115,10 @@ void AppHost::_HandleCommandlineArgs() GetStringResource(messageTitle).data(), MB_OK | messageIcon); - ExitProcess(result); + if (_logic.ShouldExitEarly()) + { + ExitProcess(result); + } } } } From ced475960d92f28b98e115d6696412936c1991c1 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Thu, 23 Apr 2020 11:44:05 -0500 Subject: [PATCH 2/3] good bot --- src/cascadia/TerminalApp/AppCommandlineArgs.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/cascadia/TerminalApp/AppCommandlineArgs.cpp b/src/cascadia/TerminalApp/AppCommandlineArgs.cpp index 728b5b5bc09..ce567b536ba 100644 --- a/src/cascadia/TerminalApp/AppCommandlineArgs.cpp +++ b/src/cascadia/TerminalApp/AppCommandlineArgs.cpp @@ -173,7 +173,7 @@ int AppCommandlineArgs::_handleExit(const CLI::App& command, const CLI::Error& e void AppCommandlineArgs::_buildParser() { auto versionCallback = [this](int64_t /*count*/) { - // Set our message to display the application name and the current verison. + // Set our message to display the application name and the current version. _exitMessage = _applicationName + "\n" + _versionString; // Theoretically, we don't need to exit now, since this isn't really an @@ -421,8 +421,6 @@ void AppCommandlineArgs::_resetStateToDefault() _focusTabIndex = -1; _focusNextTab = false; _focusPrevTab = false; - - // _requestedVersion = false; } // Function Description: From a867a9d09b29584324cee0ffc1135dba46a4b33c Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Thu, 23 Apr 2020 14:46:15 -0500 Subject: [PATCH 3/3] this is cleaner --- .../TerminalApp/AppCommandlineArgs.cpp | 37 +++++++------------ src/cascadia/TerminalApp/AppCommandlineArgs.h | 4 -- src/cascadia/TerminalApp/TerminalPage.cpp | 2 - 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/cascadia/TerminalApp/AppCommandlineArgs.cpp b/src/cascadia/TerminalApp/AppCommandlineArgs.cpp index ce567b536ba..0d8dbec1a71 100644 --- a/src/cascadia/TerminalApp/AppCommandlineArgs.cpp +++ b/src/cascadia/TerminalApp/AppCommandlineArgs.cpp @@ -2,6 +2,7 @@ // Licensed under the MIT license. #include "pch.h" +#include "AppLogic.h" #include "AppCommandlineArgs.h" #include "ActionArgs.h" #include @@ -18,22 +19,6 @@ AppCommandlineArgs::AppCommandlineArgs() _resetStateToDefault(); } -// Method Description: -// - Set the application name and version string for the application. If the -// user calls `wt --version`, we'll display these strings by setting the exit -// message (the string returned by GetExitMessage). -// Arguments: -// - applicationName: a hstring containing the application name -// - versionString: a hstring containing the version string -// Return Value: -// - -void AppCommandlineArgs::SetVersionString(const winrt::hstring& applicationName, - const winrt::hstring& versionString) -{ - _applicationName = winrt::to_string(applicationName); - _versionString = winrt::to_string(versionString); -} - // Method Description: // - Attempt to parse a given command as a single commandline. If the command // doesn't have a subcommand, we'll try parsing the commandline again, as a @@ -173,14 +158,18 @@ int AppCommandlineArgs::_handleExit(const CLI::App& command, const CLI::Error& e void AppCommandlineArgs::_buildParser() { auto versionCallback = [this](int64_t /*count*/) { - // Set our message to display the application name and the current version. - _exitMessage = _applicationName + "\n" + _versionString; - - // Theoretically, we don't need to exit now, since this isn't really an - // error case. However, in practice, it feels weird to have `wt -v` open - // a new tab, and makes enough sense that `wt -v ; split-pane` (or - // whatever) just displays the version and exits. - _shouldExitEarly = true; + if (const auto appLogic{ winrt::TerminalApp::implementation::AppLogic::Current() }) + { + // Set our message to display the application name and the current version. + _exitMessage = fmt::format("{0}\n{1}", + til::u16u8(appLogic->ApplicationDisplayName()), + til::u16u8(appLogic->ApplicationVersion())); + // Theoretically, we don't need to exit now, since this isn't really + // an error case. However, in practice, it feels weird to have `wt + // -v` open a new tab, and makes enough sense that `wt -v ; + // split-pane` (or whatever) just displays the version and exits. + _shouldExitEarly = true; + } }; _app.add_flag_function("-v,--version", versionCallback, RS_A(L"CmdVersionDesc")); diff --git a/src/cascadia/TerminalApp/AppCommandlineArgs.h b/src/cascadia/TerminalApp/AppCommandlineArgs.h index bb798226d66..1a46d644b8a 100644 --- a/src/cascadia/TerminalApp/AppCommandlineArgs.h +++ b/src/cascadia/TerminalApp/AppCommandlineArgs.h @@ -38,8 +38,6 @@ class TerminalApp::AppCommandlineArgs final const std::string& GetExitMessage(); bool ShouldExitEarly() const noexcept; - void SetVersionString(const winrt::hstring& applicationName, const winrt::hstring& versionString); - private: static const std::wregex _commandDelimiterRegex; @@ -83,8 +81,6 @@ class TerminalApp::AppCommandlineArgs final std::deque _startupActions; std::string _exitMessage; bool _shouldExitEarly{ false }; - std::string _applicationName{ "" }; - std::string _versionString{ "" }; winrt::TerminalApp::NewTerminalArgs _getNewTerminalArgs(NewTerminalSubcommand& subcommand); void _addNewTerminalArgs(NewTerminalSubcommand& subcommand); diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index 71ba8ec25e3..e3318ae63d8 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -1631,8 +1631,6 @@ namespace winrt::TerminalApp::implementation // or 0. (see TerminalPage::_ParseArgs) int32_t TerminalPage::SetStartupCommandline(winrt::array_view args) { - _appArgs.SetVersionString(ApplicationDisplayName(), ApplicationVersion()); - return _ParseArgs(args); }