Skip to content

Commit

Permalink
tldr.py: fix search
Browse files Browse the repository at this point in the history
  • Loading branch information
CleanMachine1 committed Apr 28, 2024
1 parent 41054af commit d13239c
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,43 +529,24 @@ 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)
print(commands)
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:
Expand Down

0 comments on commit d13239c

Please sign in to comment.