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

Code before:
        }
    }

    // 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);
        _SaveFileHelper(text, L"", defaultFileName);
    }

    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 }) };
                path = co_await SaveFilePicker(*_hostingHwnd, [filename = std::move(cleanedFilename)](auto&& dialog) {
                    THROW_IF_FAILED(dialog->SetClientGuid(clientGuidExportFile));
                    try

Review comment (suggestion):
Propose rename to `SaveStringToFileOrPromptUser` - this function is too complicated for what it does or what it is named.

Code after:
        }
    }

    // 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
    // - filename - the name of the file to save the text to
    // - dialogGuid - the guid to associate with these specific saves (determines where the save dialog opens to by default)
    fire_and_forget TerminalPage::_SaveStringToFileOrPromptUser(const winrt::hstring& text, const winrt::hstring& filepath, const std::wstring_view filename, const winrt::guid dialogGuid)
    {
        // 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"*.*" }
        };

        try
        {
            auto path = filepath;