Skip to content

Instantly share code, notes, and snippets.

@yjcrocks
Created September 25, 2018 14:02
Show Gist options
  • Save yjcrocks/15f2d1cc8e422b04e6af1c67f3b3d248 to your computer and use it in GitHub Desktop.
Save yjcrocks/15f2d1cc8e422b04e6af1c67f3b3d248 to your computer and use it in GitHub Desktop.
Draw box around the text. Great for debugging.
def print_box(text, line_width=2, s="#"):
text = [t.strip() for t in text.strip().split("\n")]
max_len = max(len(t) for t in text)
text = [t.ljust(max_len) for t in text]
cols = max_len + (line_width + 2 * line_width) * 2
wrap = lambda out, s: s + out + s[::-1]
gutter = s * 2 * line_width + " " * line_width
blank_line = wrap(" " * max_len, gutter) + "\n"
# generate boxed text
out = "\n".join(wrap(t, gutter) for t in text)
for _ in range(line_width // 2):
out = wrap(out, blank_line)
for _ in range(line_width):
out = wrap(out, s * cols + "\n")
print(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment