Skip to content

Instantly share code, notes, and snippets.

@scillidan
Created April 11, 2024 06:23
Show Gist options
  • Save scillidan/143108cb6e3d72eb220c89637f30ab9e to your computer and use it in GitHub Desktop.
Save scillidan/143108cb6e3d72eb220c89637f30ab9e to your computer and use it in GitHub Desktop.
import sys
import os
import subprocess
DEBUG_MODE = False
def debug_log(message):
if DEBUG_MODE:
print(f"DEBUG: {message}")
def process_md(file_path):
debug_log(f"Processing file: {file_path}")
# Load markdown file into a list of lists (each sublist is a row)
with open(file_path, 'r') as f:
lines = f.readlines()
table = [line.strip().split("|") for line in lines[2:] if line.strip()] # Skip the first 2 lines
# Find the index of the 'url' column
url_index = [col.strip() for col in lines[0].split("|")].index("url")
# Clean up the url column and join into one line
one_line = ' '.join([row[url_index].replace('file:///', '').strip() for row in table])
debug_log(f"One line: {one_line}")
return one_line
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python script.py <search_term> <file_path> [--debug]")
sys.exit(1)
arg1 = sys.argv[1]
arg2 = sys.argv[2]
if not os.path.isfile(arg2):
print(f"ERROR: File {arg2} does not exist.")
sys.exit(1)
if "--debug" in sys.argv:
DEBUG_MODE = True
one_line = process_md(arg2)
# Run the rg -S %1 one_line command
cmd = f"rg -S {arg1} {one_line}"
debug_log(f"Running command: {cmd}")
subprocess.run(cmd, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment