47 lines
1.9 KiB
Python
47 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Translate scoped ripgrep searches into the existing bounded source reader."""
|
|
import argparse
|
|
import sys
|
|
|
|
import fast_source_search
|
|
|
|
|
|
def translate(argv):
|
|
parser = argparse.ArgumentParser(description=__doc__, allow_abbrev=False)
|
|
parser.add_argument("-n", "--line-number", action="store_true")
|
|
parser.add_argument("-g", "--glob", action="append", default=[])
|
|
parser.add_argument("-F", "--fixed-strings", action="store_true")
|
|
parser.add_argument("-i", "--ignore-case", action="store_true")
|
|
parser.add_argument("-S", "--smart-case", action="store_true")
|
|
parser.add_argument("-C", "--context", type=int, default=0)
|
|
parser.add_argument("-m", "--max-count", type=int, default=160)
|
|
parser.add_argument("-e", "--regexp", action="append", default=[])
|
|
parser.add_argument("terms", nargs="+")
|
|
args = parser.parse_intermixed_args(argv)
|
|
patterns = args.regexp or [args.terms.pop(0)]
|
|
if not args.terms:
|
|
parser.error("explicit source paths are required")
|
|
if args.fixed_strings and len(patterns) != 1:
|
|
parser.error("use one fixed-string pattern per bounded search")
|
|
pattern = patterns[0] if len(patterns) == 1 else "|".join(f"(?:{value})" for value in patterns)
|
|
if args.ignore_case:
|
|
if args.fixed_strings:
|
|
import re
|
|
pattern = re.escape(pattern)
|
|
args.fixed_strings = False
|
|
pattern = "(?i)" + pattern
|
|
command = ["fast_source_search.py", "--pattern", pattern, "--context", str(args.context),
|
|
"--max-count", str(min(240, max(1, args.max_count)))]
|
|
for path in args.terms:
|
|
command.extend(["--path", path])
|
|
for glob in args.glob:
|
|
command.extend(["--glob", glob])
|
|
if args.fixed_strings:
|
|
command.append("--fixed-strings")
|
|
return command
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.argv = translate(sys.argv[1:])
|
|
raise SystemExit(fast_source_search.main())
|