Skip to content

Commit

Permalink
Addressed review comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
MKadaner committed Dec 28, 2023
1 parent 3f57637 commit 976304e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
75 changes: 37 additions & 38 deletions far/vmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ int find_nearest(std::ranges::contiguous_range auto const& Range, const int Pos,
: FindPos(drop(Pos), take(Pos) | reverse));
}

template <std::integral T>
std::pair<T, T> Intersect(std::pair<T, T> A, std::pair<T, T> B)
std::pair<int, int> Intersect(std::pair<int, int> A, std::pair<int, int> B)
{
assert(A.first < A.second);
assert(B.first < B.second);
Expand All @@ -181,13 +180,12 @@ std::pair<T, T> Intersect(std::pair<T, T> A, std::pair<T, T> B)
std::swap(A, B);

if (A.second <= B.first)
return { {}, {} };
return {};

return { B.first, std::min(A.second, B.second) };
}

template <std::integral T>
void MarkupSliceBoundaries(std::pair<T, T> Segment, std::ranges::input_range auto const& Slices, std::weakly_incrementable auto Markup)
void MarkupSliceBoundaries(std::pair<int, int> Segment, std::ranges::input_range auto const& Slices, std::vector<int>& Markup)
{
assert(Segment.first < Segment.second);

Expand All @@ -201,15 +199,15 @@ void MarkupSliceBoundaries(std::pair<T, T> Segment, std::ranges::input_range aut
if (Intersection.first == Intersection.second)
continue;

*Markup = Intersection.first; ++Markup;
*Markup = Intersection.second; ++Markup;
Markup.emplace_back(Intersection.first);
Markup.emplace_back(Intersection.second);
Segment.first = Intersection.second;

if (Segment.first == Segment.second)
return;
}

*Markup = Segment.second;
Markup.emplace_back(Segment.second);
}

//может иметь фокус
Expand Down Expand Up @@ -2066,7 +2064,8 @@ void VMenu::DisplayObject()

if (!CheckFlags(VMENU_DISABLEDRAWBACKGROUND) && !CheckFlags(VMENU_LISTBOX))
{
// BUGBUG, dead code -- 2023-07-08 MZK: Is it though? I've got here when pressed hotkey of "Select search area" combobox on "find file" dialog.
// BUGBUG, dead code -- 2023-07-08 MZK: I've got here when pressed hotkey of "Select search area" combobox on "find file" dialog.
// It may be related to the CheckFlags(VMENU_DISABLEDRAWBACKGROUND) part of the condition only. Need to investigate.

if (BoxType==SHORT_DOUBLE_BOX || BoxType==SHORT_SINGLE_BOX)
{
Expand Down Expand Up @@ -2390,12 +2389,12 @@ void VMenu::ApplySeparatorName(const MenuItemEx& CurItem, string& separator) con
auto NameWidth{ std::min(CurItem.Name.size(), separator.size() - 2) };
auto NamePos{ (separator.size() - NameWidth) / 2 };

separator.at(NamePos - 1) = L' ';
separator[NamePos - 1] = L' ';
separator.replace(NamePos, NameWidth, fit_to_left(CurItem.Name, NameWidth));
separator.at(NamePos + NameWidth) = L' ';
separator[NamePos + NameWidth] = L' ';
}

void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout, const int Y, std::vector<int>& HighlightMarkup, const string& BlankLine) const
void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout, const int Y, std::vector<int>& HighlightMarkup, string_view BlankLine) const
{
if (!Layout.Text) return;

Expand All @@ -2419,15 +2418,15 @@ void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout
MarkupSliceBoundaries(
std::pair{ CurPos, EndPos },
CurItem.Annotations | std::views::transform([](const auto Ann) { return std::pair{ Ann.first, Ann.first + Ann.second }; }),
std::back_inserter(HighlightMarkup));
HighlightMarkup);
}
else if (HotkeyPos != string::npos || CurItem.AutoHotkey)
{
const auto HighlightPos = static_cast<int>(HotkeyPos != string::npos? HotkeyPos : CurItem.AutoHotkeyPos);
MarkupSliceBoundaries(
std::pair{ CurPos, EndPos },
std::views::single(std::pair{ HighlightPos, HighlightPos + 1 }),
std::back_inserter(HighlightMarkup));
HighlightMarkup);
}
else
{
Expand All @@ -2444,7 +2443,7 @@ void VMenu::DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout
}

SetColor(ColorIndices.Normal);
Text(string_view{ BlankLine.begin(), BlankLine.begin() + (TextBegin + TextWidth - WhereX()) });
Text(BlankLine.substr(0, TextBegin + TextWidth - WhereX()));

const auto DrawDecorator = [&](const int X, std::tuple<vmenu_colors, wchar_t> ColorAndChar)
{
Expand Down Expand Up @@ -3159,40 +3158,40 @@ TEST_CASE("intersect.segments")

TEST_CASE("markup.slice.boundaries")
{
static struct test_data
static const struct test_data
{
std::pair<int, int> Segment;
std::vector<std::pair<int, int>> Slices;
std::vector<int> Markup;
std::initializer_list<std::pair<int, int>> Slices;
std::initializer_list<int> Markup;
} TestDataPoints[] =
{
{ { 20, 50 }, { { { 10, 15 } } }, { { 50 } } },
{ { 20, 50 }, { { { 10, 20 } } }, { { 50 } } },
{ { 20, 50 }, { { { 10, 30 } } }, { { 20, 30, 50 } } },
{ { 20, 50 }, { { { 10, 50 } } }, { { 20, 50 } } },
{ { 20, 50 }, { { { 10, 70 } } }, { { 20, 50 } } },
{ { 20, 50 }, { { { 20, 30 } } }, { { 20, 30, 50 } } },
{ { 20, 50 }, { { { 20, 50 } } }, { { 20, 50 } } },
{ { 20, 50 }, { { { 20, 70 } } }, { { 20, 50 } } },
{ { 20, 50 }, { { { 30, 40 } } }, { { 30, 40, 50 } } },
{ { 20, 50 }, { { { 30, 50 } } }, { { 30, 50 } } },
{ { 20, 50 }, { { { 30, 70 } } }, { { 30, 50 } } },
{ { 20, 50 }, { { { 50, 50 } } }, { { 50 } } },
{ { 20, 50 }, { { { 50, 70 } } }, { { 50 } } },
{ { 20, 50 }, { { { 60, 70 } } }, { { 50 } } },
{ { 20, 50 }, { { { 40, 30 } } }, { { 50 } } },
{ { 20, 70 }, { { { 30, 40 }, { 50, 60 } } }, { { 30, 40, 50, 60, 70 } } },
{ { 20, 70 }, { { { 30, 40 }, { 40, 60 } } }, { { 30, 40, 40, 60, 70 } } },
{ { 20, 70 }, { { { 30, 50 }, { 40, 60 } } }, { { 30, 50, 50, 60, 70 } } },
{ { 20, 70 }, { { { 50, 60 }, { 30, 40 } } }, { { 50, 60, 70 } } },
{ { 20, 50 }, { { 10, 15 } }, { 50 } },
{ { 20, 50 }, { { 10, 20 } }, { 50 } },
{ { 20, 50 }, { { 10, 30 } }, { 20, 30, 50 } },
{ { 20, 50 }, { { 10, 50 } }, { 20, 50 } },
{ { 20, 50 }, { { 10, 70 } }, { 20, 50 } },
{ { 20, 50 }, { { 20, 30 } }, { 20, 30, 50 } },
{ { 20, 50 }, { { 20, 50 } }, { 20, 50 } },
{ { 20, 50 }, { { 20, 70 } }, { 20, 50 } },
{ { 20, 50 }, { { 30, 40 } }, { 30, 40, 50 } },
{ { 20, 50 }, { { 30, 50 } }, { 30, 50 } },
{ { 20, 50 }, { { 30, 70 } }, { 30, 50 } },
{ { 20, 50 }, { { 50, 50 } }, { 50 } },
{ { 20, 50 }, { { 50, 70 } }, { 50 } },
{ { 20, 50 }, { { 60, 70 } }, { 50 } },
{ { 20, 50 }, { { 40, 30 } }, { 50 } },
{ { 20, 70 }, { { 30, 40 }, { 50, 60 } }, { 30, 40, 50, 60, 70 } },
{ { 20, 70 }, { { 30, 40 }, { 40, 60 } }, { 30, 40, 40, 60, 70 } },
{ { 20, 70 }, { { 30, 50 }, { 40, 60 } }, { 30, 50, 50, 60, 70 } },
{ { 20, 70 }, { { 50, 60 }, { 30, 40 } }, { 50, 60, 70 } },
};

std::vector<int> Markup;

for (const auto& TestDataPoint : TestDataPoints)
{
Markup.clear();
MarkupSliceBoundaries(TestDataPoint.Segment, TestDataPoint.Slices, std::back_inserter(Markup));
MarkupSliceBoundaries(TestDataPoint.Segment, TestDataPoint.Slices, Markup);
REQUIRE(std::ranges::equal(TestDataPoint.Markup, Markup));
}
}
Expand Down
4 changes: 2 additions & 2 deletions far/vmenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class VMenu final: public Modal
void DrawSeparator(size_t CurItemIndex, int BoxType, int Y) const;
void ConnectSeparator(size_t CurItemIndex, string& separator, int BoxType) const;
void ApplySeparatorName(const MenuItemEx& CurItem, string& separator) const;
void DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout, int Y, std::vector<int>& HighlightMarkup, const string& BlankLine) const;
void DrawRegularItem(const MenuItemEx& CurItem, const item_layout& Layout, int Y, std::vector<int>& HighlightMarkup, string_view BlankLine) const;

[[nodiscard]] size_t CalculateMaxLineWidth() const;

Expand All @@ -331,7 +331,7 @@ class VMenu final: public Modal
void UpdateSelectPos();
void EnableFilter(bool Enable);

[[nodiscard]] FarColor GetColor(vmenu_colors ColorIndex) const noexcept { return Colors[std::to_underlying(ColorIndex)]; }
[[nodiscard]] const FarColor& GetColor(vmenu_colors ColorIndex) const noexcept { return Colors[std::to_underlying(ColorIndex)]; }
void SetColor(vmenu_colors ColorIndex) const;
size_t Text(string_view Str) const;
size_t Text(wchar_t Char) const;
Expand Down

0 comments on commit 976304e

Please sign in to comment.