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

tldr.py: fix search #244

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 14 additions & 37 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,44 +529,21 @@ def main() -> None:
output(open_file.read().encode('utf-8').splitlines(),
plain=options.markdown)
elif options.search:
command = '-'.join(options.command)
page = None
maxprob = 0
searchquery = options.search.split(' ')

platforms = get_platform_list()
for i in get_commands(platforms):
if i.startswith(command):
for p in platforms:
result = load_page_from_cache(i, p, options.language)
if result is not None and have_recent_cache(i, p, options.language):
break
if result is None:
raise SystemExit("Please update cache")
result = result.decode("utf-8")
result = result.split()
for word in searchquery:
prob = 0
for line in result:
line = line.lower()
if word in line:
prob += 1
if line.startswith("#"):
prob += 7
elif line.startswith(">"):
prob += 5
elif line.startswith("-"):
prob += 2
if prob > maxprob:
maxprob = prob
page = i

if page:
result = get_page(page, None, options.platform, options.language)
output(result, plain=options.markdown)
search_term = options.search.lower()
commands = get_commands(options.platform, options.language)
if not commands:
print("Update cache, no commands to check from.")
return
similar_commands = []
for command in commands:
if search_term in command.lower():
similar_commands.append(command)
if similar_commands:
print("Similar commands found:")
print('\n'.join(similar_commands))
return
else:
print("No results found")

print("No commands matched your search term.")
else:
try:
command = '-'.join(options.command).lower()
Expand Down