do format

This commit is contained in:
Balakrishnan Balasubramanian 2022-12-09 19:05:14 -05:00
parent 6808c82c9b
commit 6b37b6e50f

View File

@ -7,17 +7,21 @@ import itertools
import tempfile import tempfile
import subprocess import subprocess
def star(f): def star(f):
""" see https://stackoverflow.com/q/21892989 """ """ see https://stackoverflow.com/q/21892989 """
return lambda args: f(*args) return lambda args: f(*args)
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(description="Show diff of files from two directories in vim tabs") parser = argparse.ArgumentParser(
description="Show diff of files from two directories in vim tabs")
parser.add_argument("pathA") parser.add_argument("pathA")
parser.add_argument("pathB") parser.add_argument("pathB")
parser.add_argument("--vim", help="vim command to run", default="vim") parser.add_argument("--vim", help="vim command to run", default="vim")
return parser.parse_args() return parser.parse_args()
def get_dir_info(dirname): def get_dir_info(dirname):
if not dirname: if not dirname:
return [], [] return [], []
@ -30,13 +34,16 @@ def get_dir_info(dirname):
files.append(p) files.append(p)
return dirs, files return dirs, files
def get_pairs(aItems, bItems): def get_pairs(aItems, bItems):
aItems = [(item, 'A') for item in aItems] aItems = [(item, 'A') for item in aItems]
bItems = [(item, 'B') for item in bItems] bItems = [(item, 'B') for item in bItems]
abItems = aItems + bItems abItems = aItems + bItems
abItems.sort(key=star(lambda item, tag: (item.name, tag))) abItems.sort(key=star(lambda item, tag: (item.name, tag)))
for _, items in itertools.groupby(abItems, key=star(lambda item, _: item.name)): for _, items in itertools.groupby(abItems,
items = list(items) # NOTE: python 3.10's match expression can make this better key=star(lambda item, _: item.name)):
items = list(items)
# NOTE: python 3.10's match expression can make this better
if len(items) == 2: if len(items) == 2:
(aItem, _), (bItem, _) = items (aItem, _), (bItem, _) = items
yield aItem, bItem yield aItem, bItem
@ -47,6 +54,7 @@ def get_pairs(aItems, bItems):
else: else:
yield None, item yield None, item
def get_file_pairs(a, b): def get_file_pairs(a, b):
aDirs, aFiles = get_dir_info(a) aDirs, aFiles = get_dir_info(a)
bDirs, bFiles = get_dir_info(b) bDirs, bFiles = get_dir_info(b)
@ -54,6 +62,7 @@ def get_file_pairs(a, b):
for aDir, bDir in get_pairs(aDirs, bDirs): for aDir, bDir in get_pairs(aDirs, bDirs):
yield from get_file_pairs(aDir, bDir) yield from get_file_pairs(aDir, bDir)
def main(): def main():
args = parse_args() args = parse_args()
vimCmdFile = tempfile.NamedTemporaryFile(mode='w', delete=False) vimCmdFile = tempfile.NamedTemporaryFile(mode='w', delete=False)
@ -61,7 +70,9 @@ def main():
for a, b in get_file_pairs(args.pathA, args.pathB): for a, b in get_file_pairs(args.pathA, args.pathB):
aPath = a.resolve() if a else os.devnull aPath = a.resolve() if a else os.devnull
bPath = b.resolve() if b else os.devnull bPath = b.resolve() if b else os.devnull
print(f"tabedit {aPath} | diffthis | vsp {bPath} | diffthis | diffupdate", file=vimCmdFile) print(
f"tabedit {aPath} | diffthis | vsp {bPath} | diffthis | diffupdate",
file=vimCmdFile)
cmds = f""" cmds = f"""
tabfirst | tabclose tabfirst | tabclose
call delete("{vimCmdFile.name}") call delete("{vimCmdFile.name}")
@ -69,6 +80,6 @@ def main():
print(cmds, file=vimCmdFile) print(cmds, file=vimCmdFile)
subprocess.run(args.vim.split() + ["-S", vimCmdFile.name]) subprocess.run(args.vim.split() + ["-S", vimCmdFile.name])
if __name__ == '__main__': if __name__ == '__main__':
main() main()