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
This commit is contained in:
Ron0Studios 2022-10-05 22:05:44 +01:00 committed by GitHub
parent 03987b19a5
commit 303015244e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 3 deletions

View File

@ -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))

View File

@ -21,6 +21,6 @@ namespace drawtypes {
};
using iconset_t = shared_ptr<iconset>;
} // namespace drawtypes
} // namespace drawtypes
POLYBAR_NS_END

View File

@ -1,5 +1,7 @@
#include "drawtypes/iconset.hpp"
#include <algorithm>
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

View File

@ -16,3 +16,26 @@ TEST(IconSet, fuzzyMatchExactMatchFirst) {
EXPECT_EQ("10", ret->get());
}
TEST(IconSet, fuzzyMatchLargestSubstring) {
iconset_t icons = make_shared<iconset>();
icons->add("1", make_shared<label>("1"));
icons->add("10", make_shared<label>("10"));
label_t ret = icons->get("10a", "", true);
EXPECT_EQ("10", ret->get());
}
TEST(IconSet, fuzzyMatchFallback) {
iconset_t icons = make_shared<iconset>();
icons->add("1", make_shared<label>("1"));
icons->add("10", make_shared<label>("10"));
icons->add("fallback_id", make_shared<label>("fallback_label"));
label_t ret = icons->get("b", "fallback_id", true);
EXPECT_EQ("fallback_label", ret->get());
}