def wrap_text_in_a_box(body='<STR_LIT>', title='<STR_LIT>', style='<STR_LIT>', **args):
def _wrap_row(row, max_col, break_long):<EOL><INDENT>"""<STR_LIT>"""<EOL>spaces = _RE_BEGINNING_SPACES.match(row).group()<EOL>row = row[len(spaces):]<EOL>spaces = spaces.expandtabs()<EOL>return textwrap.wrap(row, initial_indent=spaces,<EOL>subsequent_indent=spaces, width=max_col,<EOL>break_long_words=break_long)<EOL><DEDENT>def _clean_newlines(text):<EOL><INDENT>text = _RE_LONELY_NEWLINES.sub('<STR_LIT>', text)<EOL>return _RE_NEWLINES_CLEANER.sub(lambda x: x.group()[:-<NUM_LIT:1>], text)<EOL><DEDENT>body = unicode(body, '<STR_LIT:utf-8>')<EOL>title = unicode(title, '<STR_LIT:utf-8>')<EOL>astyle = dict(CFG_WRAP_TEXT_IN_A_BOX_STYLES['<STR_LIT>'])<EOL>if style in CFG_WRAP_TEXT_IN_A_BOX_STYLES:<EOL><INDENT>astyle.update(CFG_WRAP_TEXT_IN_A_BOX_STYLES[style])<EOL><DEDENT>astyle.update(args)<EOL>horiz_sep = astyle['<STR_LIT>']<EOL>border = astyle['<STR_LIT>']<EOL>tab_str = astyle['<STR_LIT>'] * astyle['<STR_LIT>']<EOL>max_col = max(astyle['<STR_LIT>'] -<EOL>len(border[<NUM_LIT:3>]) - len(border[<NUM_LIT:4>]) - len(tab_str), <NUM_LIT:1>)<EOL>min_col = astyle['<STR_LIT>']<EOL>prefix = astyle['<STR_LIT>']<EOL>suffix = astyle['<STR_LIT>']<EOL>force_horiz = astyle['<STR_LIT>']<EOL>break_long = astyle['<STR_LIT>']<EOL>body = _clean_newlines(body)<EOL>tmp_rows = [_wrap_row(row, max_col, break_long)<EOL>for row in body.split('<STR_LIT:\n>')]<EOL>body_rows = []<EOL>for rows in tmp_rows:<EOL><INDENT>if rows:<EOL><INDENT>body_rows += rows<EOL><DEDENT>else:<EOL><INDENT>body_rows.append('<STR_LIT>')<EOL><DEDENT><DEDENT>if not '<STR_LIT>'.join(body_rows).strip():<EOL><INDENT>body_rows = []<EOL><DEDENT>title = _clean_newlines(title)<EOL>tmp_rows = [_wrap_row(row, max_col, break_long)<EOL>for row in title.split('<STR_LIT:\n>')]<EOL>title_rows = []<EOL>for rows in tmp_rows:<EOL><INDENT>if rows:<EOL><INDENT>title_rows += rows<EOL><DEDENT>else:<EOL><INDENT>title_rows.append('<STR_LIT>')<EOL><DEDENT><DEDENT>if not '<STR_LIT>'.join(title_rows).strip():<EOL><INDENT>title_rows = []<EOL><DEDENT>max_col = max([len(row) for row in body_rows + title_rows] + [min_col])<EOL>mid_top_border_len = max_col +len(border[<NUM_LIT:3>]) + len(border[<NUM_LIT:4>]) - len(border[<NUM_LIT:0>]) - len(border[<NUM_LIT:2>])<EOL>mid_bottom_border_len = max_col +len(border[<NUM_LIT:3>]) + len(border[<NUM_LIT:4>]) - len(border[<NUM_LIT:5>]) - len(border[<NUM_LIT:7>])<EOL>top_border = border[<NUM_LIT:0>] +(border[<NUM_LIT:1>] * mid_top_border_len)[:mid_top_border_len] + border[<NUM_LIT:2>]<EOL>bottom_border = border[<NUM_LIT:5>] +(border[<NUM_LIT:6>] * mid_bottom_border_len)[:mid_bottom_border_len] +border[<NUM_LIT:7>]<EOL>if isinstance(horiz_sep, tuple) and len(horiz_sep) == <NUM_LIT:3>:<EOL><INDENT>horiz_line = horiz_sep[<NUM_LIT:0>] +(horiz_sep[<NUM_LIT:1>] * (max_col + <NUM_LIT:2>))[:(max_col + <NUM_LIT:2>)] + horiz_sep[<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>horiz_line = border[<NUM_LIT:3>] + (horiz_sep * max_col)[:max_col] + border[<NUM_LIT:4>]<EOL><DEDENT>title_rows = [tab_str + border[<NUM_LIT:3>] + row +<EOL>'<STR_LIT:U+0020>' * (max_col - len(row)) +<EOL>border[<NUM_LIT:4>] for row in title_rows]<EOL>body_rows = [tab_str + border[<NUM_LIT:3>] + row +<EOL>'<STR_LIT:U+0020>' * (max_col - len(row)) + border[<NUM_LIT:4>] for row in body_rows]<EOL>ret = []<EOL>if top_border:<EOL><INDENT>ret += [tab_str + top_border]<EOL><DEDENT>ret += title_rows<EOL>if title_rows or force_horiz:<EOL><INDENT>ret += [tab_str + horiz_line]<EOL><DEDENT>ret += body_rows<EOL>if bottom_border:<EOL><INDENT>ret += [tab_str + bottom_border]<EOL><DEDENT>return (prefix + '<STR_LIT:\n>'.join(ret) + suffix).encode('<STR_LIT:utf-8>')<EOL>
r"""Return a nicely formatted text box.

        e.g.
       ******************
       **  title       **
       **--------------**
       **  body        **
       ******************

    Indentation and newline are respected.

    :param body: the main text
    :param title: an optional title
    :param style: the name of one of the style in CFG_WRAP_STYLES. By default
        the double_star style is used.

    You can further tune the desired style by setting various optional
    parameters:

        :param horiz_sep: a string that is repeated in order to produce a
            separator row between the title and the body (if needed)
            or a tuple of three characters in the form (l, c, r)
        :param max_col: the maximum number of coulmns used by the box
            (including indentation)
        :param min_col: the symmetrical minimum number of columns
        :param tab_str: a string to represent indentation
        :param tab_num: the number of leveles of indentations
        :param border: a tuple of 8 element in the form
            (tl, t, tr, l, r, bl, b, br) of strings that represent the
            different corners and sides of the box
        :param prefix: a prefix string added before the box
        :param suffix: a suffix string added after the box
        :param break_long: wethever to break long words in order to respect
            max_col
        :param force_horiz: True in order to print the horizontal line even when
            there is no title

    e.g.:
    print wrap_text_in_a_box(title='prova',
        body='  123 prova.\n    Vediamo come si indenta',
        horiz_sep='-', style='no_border', max_col=20, tab_num=1)

        prova
        ----------------
        123 prova.
            Vediamo come
            si indenta