Created
May 31, 2016 17:34
-
-
Save KristoforMaynard/6e490a0646d9ca8bf23228d7cb9a8885 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from __future__ import print_function | |
import sys | |
import textwrap | |
def format_heading(text, style="-"): | |
return "{0}\n{1}\n".format(text, style * len(text)) | |
def format_table(names, descriptions, style="=", func_heading="Function ", | |
desc_heading="Description", max_desc_len=68, sp=" "): | |
max_name = max(len(func_heading), max(len(name) for name in names)) | |
max_desc = min(max(len(desc_heading), max(len(desc) for desc in descriptions)), | |
max_desc_len) | |
def _format_item(name, desc): | |
desc_lines = textwrap.wrap(desc, max_desc_len) | |
name_lines = [name] + [""] * (len(desc_lines) - 1) | |
ret = "" | |
for nl, dl in zip(name_lines, desc_lines): | |
ret += "{0}{1}{2}{3}\n".format(nl, " " * (max_name - len(nl)), sp, dl) | |
return ret | |
ret = "{0}{1}{2}\n".format(style * max_name, sp, style * max_desc) | |
ret += _format_item(func_heading, desc_heading) | |
ret += "{0}{1}{2}\n".format(style * max_name, sp, style * max_desc) | |
for name, desc in zip(names, descriptions): | |
ret += _format_item(name, desc) | |
ret += "{0}{1}{2}\n".format(style * max_name, sp, style * max_desc) | |
return ret | |
def main(): | |
table_dat = [("name A", "Some really long and unwieldy description that " | |
"just rambles on"), | |
("name B", "Some more managable description")] | |
names = [d[0] for d in table_dat] | |
descriptions = [d[1] for d in table_dat] | |
print(format_table(names, descriptions, max_desc_len=52)) | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment