From 303015244e311dc0c2a56f349deb4b72975c6e65 Mon Sep 17 00:00:00 2001 From: Ron0Studios <47331292+ron0studios@users.noreply.github.com> Date: Wed, 5 Oct 2022 22:05:44 +0100 Subject: [PATCH] feat: longest match ws-icon fuzzy matching (#2831) * better fuzzy matching Improved fuzzy matching so that it finds the longest matching icon instead of the first possible match * Update CHANGELOG.md * removed debug output * added tests and improved fuzzy finder - added return statements to the fuzzy finder - added tests to check whether the fuzzy finder works. * minor style changes * minor change to iconset.cpp * Delete tasks.json --- CHANGELOG.md | 1 + include/drawtypes/iconset.hpp | 2 +- src/drawtypes/iconset.cpp | 17 +++++++++++++++-- tests/unit_tests/drawtypes/iconset.cpp | 23 +++++++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c557059d..b3f47974 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `internal/fs`: Use `/` as a fallback if no mountpoints are specified ([`#2572`](https://github.com/polybar/polybar/issues/2572), [`#2705`](https://github.com/polybar/polybar/pull/2705)) - `internal/backlight`: Detect backlight if none specified ([`#2572`](https://github.com/polybar/polybar/issues/2572), [`#2728`](https://github.com/polybar/polybar/pull/2728)) - Providing a negative min-width to a token adds right-padding ([`#2789`](https://github.com/polybar/polybar/issues/2789), [`#2801`](https://github.com/polybar/polybar/pull/2801)) by [@VanillaViking](https://github.com/VanillaViking). +- Changed fuzzy match option on i3 and bspwm modules to find longest match instead of the first match ([`#2831`](https://github.com/polybar/polybar/pull/2831), [`#2829`](https://github.com/polybar/polybar/issues/2829)) by [@Ron0Studios](https://github.com/ron0studios/). ### Fixed - Waiting for double click interval on modules that don't have a double click action ([`#2663`](https://github.com/polybar/polybar/issues/2663), [`#2695`](https://github.com/polybar/polybar/pull/2695)) diff --git a/include/drawtypes/iconset.hpp b/include/drawtypes/iconset.hpp index 242f2923..5f6a8036 100644 --- a/include/drawtypes/iconset.hpp +++ b/include/drawtypes/iconset.hpp @@ -21,6 +21,6 @@ namespace drawtypes { }; using iconset_t = shared_ptr; -} // namespace drawtypes +} // namespace drawtypes POLYBAR_NS_END diff --git a/src/drawtypes/iconset.cpp b/src/drawtypes/iconset.cpp index 55426eed..283be114 100644 --- a/src/drawtypes/iconset.cpp +++ b/src/drawtypes/iconset.cpp @@ -1,5 +1,7 @@ #include "drawtypes/iconset.hpp" +#include + POLYBAR_NS namespace drawtypes { @@ -20,11 +22,22 @@ namespace drawtypes { // If fuzzy matching is turned on, try that first before returning the fallback. if (fuzzy_match) { + // works by finding the *longest* matching icon to the given workspace id + size_t max_size = -1; + label_t max_label; + for (auto const& icon : m_icons) { if (id.find(icon.first) != std::string::npos) { - return icon.second; + if (icon.first.length() > max_size || !max_label) { + max_size = icon.first.length(); + max_label = icon.second; + } } } + + if (max_label) { + return max_label; + } } return m_icons.find(fallback_id)->second; @@ -33,6 +46,6 @@ namespace drawtypes { iconset::operator bool() { return !m_icons.empty(); } -} // namespace drawtypes +} // namespace drawtypes POLYBAR_NS_END diff --git a/tests/unit_tests/drawtypes/iconset.cpp b/tests/unit_tests/drawtypes/iconset.cpp index 8cc53d24..31a0e5d8 100644 --- a/tests/unit_tests/drawtypes/iconset.cpp +++ b/tests/unit_tests/drawtypes/iconset.cpp @@ -16,3 +16,26 @@ TEST(IconSet, fuzzyMatchExactMatchFirst) { EXPECT_EQ("10", ret->get()); } + +TEST(IconSet, fuzzyMatchLargestSubstring) { + iconset_t icons = make_shared(); + + icons->add("1", make_shared