bin/boxify @ 1273bba3a35a

More
author Steve Losh <steve@stevelosh.com>
date Mon, 19 Aug 2024 08:55:42 -0400
parents 5390fa62ede7
children (none)
#!/usr/bin/env python3

import sys

lines = sys.stdin.readlines()

def ch(row, col):
    if row < 0 or row >= len(lines):
        return None
    line = lines[row]
    if col < 0 or col >= len(line):
        return None
    return line[col]

for row, line in enumerate(lines):
    for col, char in enumerate(line):
        c = char
        if char == '-':
            c = '—'
        elif char == '|':
            c = '│'
        elif char == '+':
            u = ch(row-1, col) == '|'
            d = ch(row+1, col) == '|'
            l = ch(row, col-1) == '-'
            r = ch(row, col+1) == '-'
            if u and d and l and r: c = '┼'
            elif u and d and l and not r: c = '┤'
            elif u and d and not l and r: c = '├'
            elif u and not d and l and r: c = '┴'
            elif not u and d and l and r: c = '┬'
            elif u and r: c = '└'
            elif u and l: c = '┘'
            elif d and l: c = '┐'
            elif d and r: c = '┌'
            elif u and d: c = '│'
            elif l and r: c = '─'
            else:
                print(u, d, l, r,)
                assert False

        sys.stdout.write(c)