Skip to content

Instantly share code, notes, and snippets.

@Benhgift
Created June 14, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benhgift/e967428a81ef01ad592c9179128de68c to your computer and use it in GitHub Desktop.
Save Benhgift/e967428a81ef01ad592c9179128de68c to your computer and use it in GitHub Desktop.
agg <pattern> will open the files it finds
#!/usr/bin/env python3
from subprocess import getoutput
import os
import re
import fire
def main(*ag_args):
ag_args = ' '.join(ag_args)
print_matches(ag_args)
files = get_files(ag_args)
if not files[0]: return error()
nums = file_nums_to_open(files)
file_dict = get_file_dict(files)
for num in nums:
os.system('gvim ' + file_dict[num])
def error():
print('no files')
def get_files(ag_args):
return getoutput(f'ag -W 200 -l {ag_args}').split('\n')
def file_nums_to_open(files):
print_files(files)
nums = input('\ntype file numbers to open\n')
return map(int, re.findall(r'\d+', nums))
def print_matches(ag_args):
os.system(f'ag -W 200 {ag_args}')
print()
def print_files(files):
for i, f in enumerate(files):
if i < 10:
print(i, '', f)
else:
print(i, f)
def get_file_dict(files):
return { i: f for i, f in enumerate(files) }
if __name__ == '__main__':
try:
fire.Fire(main)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment