Last active
November 11, 2022 02:08
-
-
Save teshanshanuka/b74c40bf98eaf319054759d5614b0ba4 to your computer and use it in GitHub Desktop.
Draw unicode box around text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Teshan Liyanage <[email protected]> | |
def box_text(text, fmt="regular", padding=(0, 0)): | |
""" Put text into a box in unicode | |
:param text: Text | |
:param fmt: Options - [regular, bold, rounded] | |
:param padding: horizontal and vertical padding | |
:Example: | |
>>> ┌─────────────┐ ╭─────────╮ ┏━━━━━━┓ | |
│ I'm a box! ├─────┤ Rounded ┝━━━━━┫ Bold ┃ | |
└──────┬──────┘ ╰─────────╯ ┗━━━━━━┛ | |
│ | |
┌─────┴────┐ ┌──────────┐ | |
│ Me too! ├──────┤ Me Three │ | |
└──────────┘ └──────────┘ | |
.. Idea and the characters from: | |
https://gist.github.com/carldunham/cf6738683c53be4510041a6ebbb42207 | |
""" | |
hl, vl = '─', '│' | |
if fmt == "regular": | |
tl, tr, bl, br = '┌', '┐', '└', '┘' | |
elif fmt == "rounded": | |
tl, tr, bl, br = '╭', '╮', '╰', '╯' | |
elif fmt == "bold": | |
hl, vl = '━', '┃' | |
tl, tr, bl, br = '┏', '┓', '┗', '┛' | |
else: | |
raise NotImplementedError | |
h_len = len(text) + 2 * padding[0] | |
s = tl + hl * h_len + tr + '\n' | |
for i in range(padding[1]): | |
s += vl + ' ' * h_len + vl + '\n' | |
s += vl + ' ' * padding[0] + text + ' ' * padding[0] + vl + '\n' | |
for i in range(padding[1]): | |
s += vl + ' ' * h_len + vl + '\n' | |
s += bl + hl * h_len + br | |
return s | |
if __name__ == '__main__': | |
print(box_text("tesHan")) | |
print(box_text("Teshan SJ")) | |
print(box_text("TESHAN", padding=(1, 1))) | |
print(box_text("teshaN", padding=(0, 1), fmt="bold")) | |
print(box_text("Teshan", padding=(2, 0))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment