diff --git a/vimtabdiff.py b/vimtabdiff.py index 81ca92c..0b092ab 100755 --- a/vimtabdiff.py +++ b/vimtabdiff.py @@ -7,20 +7,24 @@ import itertools import tempfile import subprocess + def star(f): """ see https://stackoverflow.com/q/21892989 """ return lambda args: f(*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("pathB") parser.add_argument("--vim", help="vim command to run", default="vim") return parser.parse_args() + def get_dir_info(dirname): if not dirname: - return [],[] + return [], [] dirs, files = [], [] dirp = pathlib.Path(dirname) for p in dirp.iterdir(): @@ -30,13 +34,16 @@ def get_dir_info(dirname): files.append(p) return dirs, files + def get_pairs(aItems, bItems): aItems = [(item, 'A') for item in aItems] bItems = [(item, 'B') for item in bItems] abItems = aItems + bItems abItems.sort(key=star(lambda item, tag: (item.name, tag))) - for _, items in itertools.groupby(abItems, key=star(lambda item, _: item.name)): - items = list(items) # NOTE: python 3.10's match expression can make this better + for _, items in itertools.groupby(abItems, + key=star(lambda item, _: item.name)): + items = list(items) + # NOTE: python 3.10's match expression can make this better if len(items) == 2: (aItem, _), (bItem, _) = items yield aItem, bItem @@ -47,6 +54,7 @@ def get_pairs(aItems, bItems): else: yield None, item + def get_file_pairs(a, b): aDirs, aFiles = get_dir_info(a) bDirs, bFiles = get_dir_info(b) @@ -54,6 +62,7 @@ def get_file_pairs(a, b): for aDir, bDir in get_pairs(aDirs, bDirs): yield from get_file_pairs(aDir, bDir) + def main(): args = parse_args() vimCmdFile = tempfile.NamedTemporaryFile(mode='w', delete=False) @@ -61,7 +70,9 @@ def main(): for a, b in get_file_pairs(args.pathA, args.pathB): aPath = a.resolve() if a 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""" tabfirst | tabclose call delete("{vimCmdFile.name}") @@ -69,6 +80,6 @@ def main(): print(cmds, file=vimCmdFile) subprocess.run(args.vim.split() + ["-S", vimCmdFile.name]) + if __name__ == '__main__': main() -