Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/Command.cpp (C++)

Code before:
            parseElement(root);
        }
        return result;
    }

    // Method description:
    // * Convert the list of recent commands into a list of sendInput actions to
    //   send those commands.
    // * We'll give each command a "history" icon.
    // * If directories is true, we'll prepend "cd " to each command, so that
    //   the command will be run as a directory change instead.
    IVector<Model::Command> Command::HistoryToCommands(IVector<winrt::hstring> history,
                                                       winrt::hstring currentCommandline,
                                                       bool directories)
    {
        std::wstring cdText = directories ? L"cd " : L"";
        auto result = winrt::single_threaded_vector<Model::Command>();

        // Use this map to discard duplicates.
        std::unordered_map<std::wstring_view, bool> foundCommands{};

        auto backspaces = std::wstring(currentCommandline.size(), L'\x7f');

        auto createAction = [&](std::wstring_view line) {
            if (line.empty())
            {
                return;
            }
            if (foundCommands.contains(line))
            {
                return;
            }
            Model::SendInputArgs args{ winrt::hstring{ fmt::format(L"{}{}{}", cdText, backspaces, line) } };
            Model::ActionAndArgs actionAndArgs{ ShortcutAction::SendInput, args };

            auto command = winrt::make_self<Command>();
            command->_ActionAndArgs = actionAndArgs;
            command->_name = winrt::hstring{ line };
            command->_iconPath = directories ?
                                     L"\ue8da" : // OpenLocal (a folder with an arrow pointing up)
                                     L"\ue81c"; // History icon
            result.Append(*command);
            foundCommands[line] = true;
        };

        // Iterate in reverse over the history, so that most recent commands are first
        for (auto i = history.Size(); i > 0; i--)
        {
            createAction(history.GetAt(i - 1));
        }
        return result;

Review comment (suggestion):
Some thoughts:
* Why did this change in this PR in particular?
* Why does it say "backspaces" but uses DELs? Shouldn't it use `\b`?
* `fmt::format` below should use `FMT_COMPILE` if possible.
* The proper way to do this is to use `{:\x7f^{}}` with `L""` and `currentCommandline.size()` as the arguments. This will pad the empty string to be at least that many padding characters wide. (For `printf()` functions you'd use the same technique.) Here's an example: https://godbolt.org/z/Gd5hfaqKj (The example uses narrow strings. You'd have to convert all of those arguments to wide strings for MSVC.)
* It would probably help with readability if you put the `fmt::format` call on an extra line.
* Since `Model::SendInputArgs` is a WinRT projection, shouldn't the argument technically be a `winrt::param::hstring` and thus accept a `std::wstring_view` as the argument? In that case you can drop the `winrt::hstring{}` wrapping.

Code after:
        else if (root.isObject())
        {
            // If we instead only got a single element back, just parse the root element.
            parseElement(root);
        }

        return winrt::single_threaded_vector<Model::Command>(std::move(result));
    }

    // Method description:
    // * Convert the list of recent commands into a list of sendInput actions to
    //   send those commands.
    // * We'll give each command a "history" icon.
    // * If directories is true, we'll prepend "cd " to each command, so that
    //   the command will be run as a directory change instead.
    IVector<Model::Command> Command::HistoryToCommands(IVector<winrt::hstring> history,
                                                       winrt::hstring currentCommandline,
                                                       bool directories)
    {
        std::wstring cdText = directories ? L"cd " : L"";
        auto result = std::vector<Model::Command>();

        // Use this map to discard duplicates.
        std::unordered_map<std::wstring_view, bool> foundCommands{};

        auto backspaces = std::wstring(currentCommandline.size(), L'\x7f');

        // Iterate in reverse over the history, so that most recent commands are first
        for (auto i = history.Size(); i > 0; i--)
        {
            std::wstring_view line{ history.GetAt(i - 1) };

            if (line.empty())
            {
                continue;
            }
            if (foundCommands.contains(line))
            {
                continue;
            }
            auto args = winrt::make_self<SendInputArgs>(
                winrt::hstring{ fmt::format(L"{}{}{}", cdText, backspaces, line) });

            Model::ActionAndArgs actionAndArgs{ ShortcutAction::SendInput, *args };

            auto command = winrt::make_self<Command>();
            command->_ActionAndArgs = actionAndArgs;
            command->_name = winrt::hstring{ line };
            command->_iconPath = directories ?
                                     L"\ue8da" : // OpenLocal (a folder with an arrow pointing up)
                                     L"\ue81c"; // History icon