Skip to content

Instantly share code, notes, and snippets.

@jfinstrom
Created December 25, 2012 02:30
Show Gist options
  • Save jfinstrom/4371405 to your computer and use it in GitHub Desktop.
Save jfinstrom/4371405 to your computer and use it in GitHub Desktop.
def boxtext(msg):
lines = msg.split("\n")
width = len(max(lines, key=len))+4
topline = u"\u2554" + u"\u2550"*width + u"\u2557"+"\n"
side = u"\u2551"
bottom = u"\u255a"+ u"\u2550"*width + u"\u255d"+"\n"
box = topline
for l in lines:
if len(l)<2:
continue
pad = (width - len(l))/2
box += side
box += " "*pad
box += l
if (len(l) % 2) > 0:
box += " " * pad
else:
box += " " * (pad+1)
box += side + "\n"
box += bottom
return box
╔═════════════════════════════════════════════════════════════════════════╗
║ Lorem ipsum dolor sit amet, consectetur adipiscing elit. ║
║ Suspendisse sed magna auctor justo dapibus lacinia in interdum purus. ║
║ Cras ut elit porttitor purus commodo sollicitudin at id metus. ║
║ Vestibulum vehicula sem non nibh cursus rutrum. Nulla nec felis ║
║ gravida ante cursus placerat a vel odio. In quis nulla tristique ║
║ odio tristique porttitor sit amet a enim. Etiam turpis purus, ║
║ laoreet vitae mattis quis, suscipit mattis ante. Duis ac felis dolor. ║
║ In hac habitasse platea dictumst. ║
╚═════════════════════════════════════════════════════════════════════════╝
// On a console there is no gap on the sides...
@nkesquerda
Copy link

Hello, jfinstrom. I'm using this app for one program. I can't find out the way to center the box in the console. Could you help me? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment