simonw / datasette-ripgrep

Web interface for searching your code using ripgrep, built as a Datasette plugin
https://ripgrep.datasette.io
Apache License 2.0
72 stars 1 forks source link

Show context around search matches #13

Closed simonw closed 3 years ago

simonw commented 3 years ago

I can use the -C 2 flag for this. Here's the resulting JSON:

(datasette-ripgrep) datasette-ripgrep % rg pytest -C 2 --json
{"type":"begin","data":{"path":{"text":"README.md"}}}
{"type":"context","data":{"path":{"text":"README.md"},"lines":{"text":"To run the tests:\n"},"line_number":61,"absolute_offset":2099,"submatches":[]}}
{"type":"context","data":{"path":{"text":"README.md"},"lines":{"text":"\n"},"line_number":62,"absolute_offset":2117,"submatches":[]}}
{"type":"match","data":{"path":{"text":"README.md"},"lines":{"text":"    pytest\n"},"line_number":63,"absolute_offset":2118,"submatches":[{"match":{"text":"pytest"},"start":4,"end":10}]}}
{"type":"end","data":{"path":{"text":"README.md"},"binary_offset":null,"stats":{"elapsed":{"secs":0,"nanos":687238,"human":"0.000687s"},"searches":1,"searches_with_match":1,"bytes_searched":2129,"bytes_printed":527,"matched_lines":1,"matches":1}}}
{"type":"begin","data":{"path":{"text":"setup.py"}}}
{"type":"context","data":{"path":{"text":"setup.py"},"lines":{"text":"    package_data={\"datasette_ripgrep\": [\"templates/*.html\"]},\n"},"line_number":31,"absolute_offset":936,"submatches":[]}}
{"type":"context","data":{"path":{"text":"setup.py"},"lines":{"text":"    install_requires=[\"datasette\"],\n"},"line_number":32,"absolute_offset":998,"submatches":[]}}
{"type":"match","data":{"path":{"text":"setup.py"},"lines":{"text":"    extras_require={\"test\": [\"pytest\", \"pytest-asyncio\", \"httpx\"]},\n"},"line_number":33,"absolute_offset":1034,"submatches":[{"match":{"text":"pytest"},"start":30,"end":36},{"match":{"text":"pytest"},"start":40,"end":46}]}}