Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QS: Fuzzy search strings #964

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions floss/qs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import functools
import itertools
import contextlib
from rapidfuzz import fuzz
from collections import defaultdict
from typing import Set, Dict, List, Union, Tuple, Literal, Callable, Iterable, Optional, Sequence
from dataclasses import field, dataclass
Expand Down Expand Up @@ -1080,7 +1081,7 @@ def has_visible_successors(layout: Layout) -> bool:


def render_strings(
console: Console, layout: Layout, tag_rules: TagRules, depth: int = 0, name_hint: Optional[str] = None
console: Console, layout: Layout, tag_rules: TagRules, depth: int = 0, name_hint: Optional[str] = None, search_string: Optional[str] = None
):
if not is_visible(layout):
return
Expand All @@ -1093,7 +1094,7 @@ def render_strings(
# for example:
#
# rsrc: BINARY/102/0 (pe)
return render_strings(console, layout.children[0], tag_rules, depth, name_hint=layout.name)
return render_strings(console, layout.children[0], tag_rules, depth, name_hint=layout.name, search_string=search_string)

BORDER_STYLE = MUTED_STYLE

Expand Down Expand Up @@ -1130,6 +1131,17 @@ def render_string_line(console: Console, tag_rules: TagRules, string: TaggedStri
line.append_text(Span("┃" * (depth + 1), style=BORDER_STYLE))
console.print(line)

if search_string:
layout.strings = [
string
for string in layout.strings
if any(
fuzz.ratio(token, search_token) >= 50
for token in string.string.string.split(' ')
for search_token in search_string.split(' ')
)
]

if not layout.children:
# for string in layout.strings[:4]:
for string in layout.strings:
Expand All @@ -1149,7 +1161,7 @@ def render_string_line(console: Console, tag_rules: TagRules, string: TaggedStri
for string in strings_before_child:
render_string_line(console, tag_rules, string, depth)

render_strings(console, child, tag_rules, depth + 1)
render_strings(console, child, tag_rules, depth + 1, search_string=search_string)

# render strings after last child
strings_after_children = list(filter(lambda s: child.end < s.offset < layout.end, layout.strings))
Expand Down Expand Up @@ -1181,6 +1193,12 @@ def main():
default=MIN_STR_LEN,
help="minimum string length",
)
parser.add_argument(
"-s",
"--search-string",
dest="search_string",
type=str,
help="string to fuzzy search")
logging_group = parser.add_argument_group("logging arguments")
logging_group.add_argument("-d", "--debug", action="store_true", help="enable debugging output on STDERR")
logging_group.add_argument(
Expand Down Expand Up @@ -1251,7 +1269,7 @@ def main():
hide_strings_by_rules(layout, tag_rules)

console = Console()
render_strings(console, layout, tag_rules)
render_strings(console, layout, tag_rules, search_string=args.search_string)

return 0

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"dnfile==0.13.0",
"colorama==0.4.6",
"msgspec==0.14.2",
"rapidfuzz==3.6.2",
"python-lancelot==0.8.7",
],
"dev": [
Expand Down
Loading