Support custom vim command

This commit is contained in:
Balakrishnan Balasubramanian 2022-12-05 20:13:31 -05:00
parent d6a0cc1109
commit 08daac28c5

View File

@ -7,10 +7,15 @@ import itertools
import tempfile import tempfile
import subprocess import subprocess
def star(f):
""" see https://stackoverflow.com/q/21892989 """
return lambda args: f(*args)
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(description="Show diff in vim tab pages") parser = argparse.ArgumentParser(description="Show diff in vim tab pages")
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")
return parser.parse_args() return parser.parse_args()
def get_dir_info(dirname): def get_dir_info(dirname):
@ -29,9 +34,9 @@ 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= lambda item: (item[0].name, item[1])) abItems.sort(key=star(lambda item, tag: (item.name, tag)))
for _, items in itertools.groupby(abItems, lambda item: item[0].name): for _, items in itertools.groupby(abItems, key=star(lambda item, _: item.name)):
items = list(items) 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
@ -51,15 +56,15 @@ def get_file_pairs(a, b):
def main(): def main():
args = parse_args() args = parse_args()
print("Helloworld")
vimCmdFile = tempfile.NamedTemporaryFile(mode='w', delete=False) vimCmdFile = tempfile.NamedTemporaryFile(mode='w', delete=False)
with vimCmdFile: with vimCmdFile:
for a, b in get_file_pairs(args.pathA, args.pathB): for a, b in get_file_pairs(args.pathA, args.pathB):
aPath = str(a) if a else "/dev/null" aPath = a.resolve() if a else "/dev/null"
bPath = str(b) if b else "/dev/null" bPath = b.resolve() if b else "/dev/null"
print(f"tabedit {aPath} | diffthis | vsp {bPath} | diffthis", file=vimCmdFile) print(f"tabedit {aPath} | diffthis | vsp {bPath} | diffthis | diffupdate", file=vimCmdFile)
print("tabrewind | bdelete", file=vimCmdFile)
print(f"""call delete("{vimCmdFile.name}")""", file=vimCmdFile) print(f"""call delete("{vimCmdFile.name}")""", file=vimCmdFile)
subprocess.run(["vim", "-S", vimCmdFile.name]) subprocess.run(args.vim.split() + ["-S", vimCmdFile.name])
if __name__ == '__main__': if __name__ == '__main__':
main() main()