Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:

    // Method Description:
    // - This method is called once the query palette suggestion was chosen
    //   We'll use this event to input the suggestion
    // Arguments:
    // - suggestion - suggestion to dispatch
    // Return Value:
    // - <none>
    void TerminalPage::_OnInputSuggestionRequested(const IInspectable& /*sender*/, const winrt::hstring& suggestion)
    {
        if (auto activeControl = _GetActiveControl())
        {
            activeControl.SendInput(suggestion);
        }
    }

    // Method Description:
    // - This method is called when the user clicks the "export message history" button
    //   in the query palette
    // Arguments:
    // - text - the text to export
    // Return Value:
    // - <none>
    void TerminalPage::_OnExportChatHistoryRequested(const IInspectable& /*sender*/, const winrt::hstring& text)
    {
        _SaveFileHelper(text, L"", L"");
    }

    fire_and_forget TerminalPage::_SaveFileHelper(const winrt::hstring& text, const winrt::hstring& filepath, const std::wstring_view filename)
    {
        // This will be used to set up the file picker "filter", to select .txt
        // files by default.
        static constexpr COMDLG_FILTERSPEC supportedFileTypes[] = {
            { L"Text Files (*.txt)", L"*.txt" },
            { L"All Files (*.*)", L"*.*" }
        };
        // An arbitrary GUID to associate with all instances of this
        // dialog, so they all re-open in the same path as they were
        // open before:
        static constexpr winrt::guid clientGuidExportFile{ 0xF6AF20BB, 0x0800, 0x48E6, { 0xB0, 0x17, 0xA1, 0x4C, 0xD8, 0x73, 0xDD, 0x58 } };

        try
        {
            auto path = filepath;

            if (path.empty())
            {
                // GH#11356 - we can't use the UWP apis for writing the file,
                // because they don't work elevated (shocker) So just use the
                // shell32 file picker manually.
                std::wstring cleanedFilename{ til::clean_filename(std::wstring{ filename }) };

Review comment (bug):
(correct me if/where I'm wrong)
Ok, so this opens up the file picker in the Downloads folder (which is perfect imo).

We're not passing in a filename, which means we're forcing the user to create a filename in the file picker UI.

Can we be extra nice and provide a unique default filename so the user can just click "save" and move on? Maybe something like `TerminalChat_<ProfileName>_<TimeStamp>` (i.e. `TerminalChat_PowerShell_20240716-1210`)? (though I'm not sure how valuable `<ProfileName>` is and I'm not sure what a nice format for `<TimeStamp>` would be. Use your discretion here)

Code after:
        _actionDispatch->DoAction(actionAndArgs);
    }

    // Method Description:
    // - This method is called once the query palette suggestion was chosen
    //   We'll use this event to input the suggestion
    // Arguments:
    // - suggestion - suggestion to dispatch
    // Return Value:
    // - <none>
    void TerminalPage::_OnInputSuggestionRequested(const IInspectable& /*sender*/, const winrt::hstring& suggestion)
    {
        if (auto activeControl = _GetActiveControl())
        {
            activeControl.SendInput(suggestion);
        }
    }

    // Method Description:
    // - This method is called when the user clicks the "export message history" button
    //   in the query palette
    // Arguments:
    // - text - the text to export
    // Return Value:
    // - <none>
    void TerminalPage::_OnExportChatHistoryRequested(const IInspectable& /*sender*/, const winrt::hstring& text)
    {
        time_t nowTime;
        time(&nowTime);

        tm nowTm;
        localtime_s(&nowTm, &nowTime);

        wchar_t buf[64];
        wcsftime(&buf[0], ARRAYSIZE(buf), L"%F %T", &nowTm);

        const auto defaultFileName = RS_(L"TerminalChatHistoryDefaultFileName") + winrt::to_hstring(buf);

        // An arbitrary GUID to associate with all instances of the save file dialog
        // for exporting terminal chat histories, so they all re-open in the same path as they were
        // open before:
        static constexpr winrt::guid terminalChatSaveFileDialogGuid{ 0xc3e449f6, 0x1b5, 0x44e0, { 0x9e, 0x6d, 0x63, 0xca, 0x15, 0x43, 0x4b, 0xdc } };

        _SaveStringToFileOrPromptUser(text, L"", defaultFileName, terminalChatSaveFileDialogGuid);
    }

    // Method Description:
    // - Saves the given text to the file path provided, or prompts the user for the location to save it
    // Arguments:
    // - text - the text to save
    // - filepath - the location to save the text