Print copy-paste-ready parentheses for balancing

Though capped at 80 parentheses per type, if you don't supply --all.
This commit is contained in:
Joscha 2025-10-24 02:01:40 +02:00
parent c96717455c
commit 0f35fa3701

View file

@ -7,9 +7,25 @@ PARENS = list("()[]{}<>")
SMILEYS = [":D", "D:"]
def main():
def print_a_bunch(what: str, how_much: int, all: bool) -> None:
while how_much > 0:
print(what * min(80, how_much))
if not all:
break
how_much -= 80
def balance(count: Counter[str], a: str, b: str, all: bool) -> None:
if count[a] > count[b]:
print_a_bunch(b, count[a] - count[b], all)
if count[b] > count[a]:
print_a_bunch(a, count[b] - count[a], all)
def main() -> None:
parser = ArgumentParser()
parser.add_argument("path", type=Path)
parser.add_argument("--all", "-a", action="store_true")
args = parser.parse_args()
count = Counter[str]()
@ -26,6 +42,11 @@ def main():
for smiley in SMILEYS:
print(f"{smiley} - {count[smiley]:6}")
balance(count, "(", ")", args.all)
balance(count, "[", "]", args.all)
balance(count, "{", "}", args.all)
balance(count, "<", ">", args.all)
if __name__ == "__main__":
main()