Working with vim
This commit is contained in:
parent
00686d7dad
commit
d6a0cc1109
@ -4,7 +4,8 @@ import os,sys
|
|||||||
import argparse
|
import argparse
|
||||||
import pathlib
|
import pathlib
|
||||||
import itertools
|
import itertools
|
||||||
from pathlib import Path
|
import tempfile
|
||||||
|
import subprocess
|
||||||
|
|
||||||
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")
|
||||||
@ -12,11 +13,12 @@ def parse_args():
|
|||||||
parser.add_argument("pathB")
|
parser.add_argument("pathB")
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
def get_dir_info(dirname: Path):
|
def get_dir_info(dirname):
|
||||||
if not dirname:
|
if not dirname:
|
||||||
return [],[]
|
return [],[]
|
||||||
dirs, files = [], []
|
dirs, files = [], []
|
||||||
for p in dirname.iterdir():
|
dirp = pathlib.Path(dirname)
|
||||||
|
for p in dirp.iterdir():
|
||||||
if p.is_dir():
|
if p.is_dir():
|
||||||
dirs.append(p)
|
dirs.append(p)
|
||||||
else:
|
else:
|
||||||
@ -26,14 +28,14 @@ def get_dir_info(dirname: Path):
|
|||||||
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]
|
||||||
items = sorted(aItems + bItems)
|
abItems = aItems + bItems
|
||||||
for _, items in itertools.groupby(items, lambda item: item[0].name):
|
abItems.sort(key= lambda item: (item[0].name, item[1]))
|
||||||
|
for _, items in itertools.groupby(abItems, lambda item: item[0].name):
|
||||||
items = list(items)
|
items = list(items)
|
||||||
if len(items) == 2:
|
if len(items) == 2:
|
||||||
(aItem, _), (bItem, _) = items
|
(aItem, _), (bItem, _) = items
|
||||||
yield aItem, bItem
|
yield aItem, bItem
|
||||||
else:
|
else:
|
||||||
assert(len(items) == 1)
|
|
||||||
(item, tag), = items
|
(item, tag), = items
|
||||||
if tag == 'A':
|
if tag == 'A':
|
||||||
yield item, None
|
yield item, None
|
||||||
@ -41,8 +43,8 @@ def get_pairs(aItems, bItems):
|
|||||||
yield None, item
|
yield None, item
|
||||||
|
|
||||||
def get_file_pairs(a, b):
|
def get_file_pairs(a, b):
|
||||||
aDirs, aFiles = get_dir_info(Path(a))
|
aDirs, aFiles = get_dir_info(a)
|
||||||
bDirs, bFiles = get_dir_info(Path(b))
|
bDirs, bFiles = get_dir_info(b)
|
||||||
yield from get_pairs(aFiles, bFiles)
|
yield from get_pairs(aFiles, bFiles)
|
||||||
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)
|
||||||
@ -50,8 +52,14 @@ def get_file_pairs(a, b):
|
|||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
print("Helloworld")
|
print("Helloworld")
|
||||||
for a, b in get_file_pairs(args.pathA, args.pathB):
|
vimCmdFile = tempfile.NamedTemporaryFile(mode='w', delete=False)
|
||||||
print(f"{a=}, {b=}")
|
with vimCmdFile:
|
||||||
|
for a, b in get_file_pairs(args.pathA, args.pathB):
|
||||||
|
aPath = str(a) if a else "/dev/null"
|
||||||
|
bPath = str(b) if b else "/dev/null"
|
||||||
|
print(f"tabedit {aPath} | diffthis | vsp {bPath} | diffthis", file=vimCmdFile)
|
||||||
|
print(f"""call delete("{vimCmdFile.name}")""", file=vimCmdFile)
|
||||||
|
subprocess.run(["vim", "-S", vimCmdFile.name])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user