Skip to content

Commit 0318102

Browse files
gotlougitCleanMachine1MasterOdin
authored
Add search functionality (#173)
Co-authored-by: CleanMachine1 <78213164+CleanMachine1@users.noreply.github.com> Co-authored-by: Matthew Peveler <matt.peveler@gmail.com>
1 parent dfcba08 commit 0318102

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

tldr.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,11 @@ def create_parser() -> ArgumentParser:
419419
)
420420
)
421421

422+
parser.add_argument("--search",
423+
metavar='"KEYWORDS"',
424+
type=str,
425+
help="Search for a specific command from a query")
426+
422427
parser.add_argument('-u', '--update_cache',
423428
action='store_true',
424429
help="Update the local cache of pages and exit")
@@ -495,7 +500,6 @@ def main() -> None:
495500
elif len(sys.argv) == 1:
496501
parser.print_help(sys.stderr)
497502
sys.exit(1)
498-
499503
if options.list:
500504
print(get_commands(options.platform))
501505
elif options.render:
@@ -504,6 +508,39 @@ def main() -> None:
504508
with open(command, encoding='utf-8') as open_file:
505509
output(open_file.read().encode('utf-8').splitlines(),
506510
plain=options.markdown)
511+
elif options.search:
512+
command = '-'.join(options.command)
513+
page = None
514+
maxprob = 0
515+
searchquery = options.search.split(' ')
516+
517+
platforms = get_platform_list()
518+
for i in get_commands(platforms):
519+
if i.startswith(command):
520+
for p in platforms:
521+
result = load_page_from_cache(i, p, options.language)
522+
if result is not None and have_recent_cache(i, p, options.language):
523+
break
524+
if result is None:
525+
raise SystemExit("Please update cache")
526+
result = result.decode("utf-8")
527+
result = result.split()
528+
for word in searchquery:
529+
prob = 0
530+
for line in result:
531+
if word in line and \
532+
(line.startswith('-') or line.startswith('>')):
533+
prob += 1
534+
if prob > maxprob:
535+
maxprob = prob
536+
page = i
537+
538+
if page:
539+
result = get_page(page, None, options.platform, options.language)
540+
output(result, plain=options.markdown)
541+
else:
542+
print("No results found")
543+
507544
else:
508545
try:
509546
command = '-'.join(options.command).lower()

0 commit comments

Comments
 (0)