U e([V"@sjddlmZddlmZddlmZddlmZdZddZ d d Z dddZ Gddde Z ddZdS))contextmanager)get_terminal_size) split_opt)term_lenNcCsTi}|D]0}t|D]"\}}t||dt|||<qqtddt|DS)Nrcss|]\}}|VqdS)N).0xyrr/formatting.py sz measure_table..) enumeratemaxgetrtuplesorteditems)rowswidthsrowidxcolrrr measure_table s rccs,|D]"}t|}|d|t|VqdS)N))rlen)rZ col_countrrrr iter_rowssrNrFc sddlm}|}||||dd}|s2||Sggdfdd}|D]B}|sj|dqVdkrt|} |}| t||qV|g} D]J\} }|d,| r| | |n| ||W5QRXqd | S) aA helper function that intelligently wraps text. By default, it assumes that it operates on a single paragraph of text but if the `preserve_paragraphs` parameter is provided it will intelligently handle paragraphs (defined by two empty lines). If paragraphs are handled, a paragraph can be prefixed with an empty line containing the ``\b`` character (``\x08``) to indicate that no rewrapping should happen in that block. :param text: the text that should be rewrapped. :param width: the maximum width for the text. :param initial_indent: the initial indent that should be placed on the first line as a string. :param subsequent_indent: the indent string that should be placed on each consecutive line. :param preserve_paragraphs: if this flag is set then the wrapping will intelligently handle paragraphs. r) TextWrapperF)initial_indentsubsequent_indentZreplace_whitespaceNc sdsdSddkr<p"dddddfnpFdddfdd=dS)NrT rF )stripappendjoinrZbufindentprr _flush_par9s $zwrap_text.._flush_parr"z ) Z _textwrapr expandtabsZfill splitlinesrlstripr$Z extra_indentZ indent_onlyr%) textwidthrrpreserve_paragraphsrwrapperr)lineZorig_lenrvrawrr&r wrap_texts:     r4c@s~eZdZdZdddZddZdd Zd d Zd ddZddZ ddZ ddZ d!ddZ e ddZe ddZddZdS)" HelpFormatteraThis class helps with formatting text-based help pages. It's usually just needed for very special internal cases, but it's also exposed so that developers can write their own fancy outputs. At present, it always writes into memory. :param indent_increment: the additional increment for each level. :param width: the width for the text. This defaults to the terminal width clamped to a maximum of 78. NcCsV||_|dkrd}|dkr@t}|dkr@tttd|dd}||_d|_g|_dS)NPrr62)indent_increment FORCED_WIDTHrminrr.current_indentbuffer)selfr9r.Z max_widthrrr __init__eszHelpFormatter.__init__cCs|j|dS)z1Writes a unicode string into the internal buffer.N)r=r$)r>stringrrr writeqszHelpFormatter.writecCs|j|j7_dS)zIncreases the indentation.Nr<r9r>rrr r'uszHelpFormatter.indentcCs|j|j8_dS)zDecreases the indentation.NrBrCrrr dedentyszHelpFormatter.dedentrUsage: cCsd|j||f}|j|j}|t|dkrPdt|}|t||||dnB|||ddt|jt|d}|t||||d|ddS)zWrites a usage line into the buffer. :param prog: the program name. :param args: whitespace separated list of arguments. :param prefix: the prefix for the first line. z%*s%s r")rrr!N)r<r.rrAr4r)r>progargsprefixZ usage_prefix text_widthr'rrr write_usage}s        zHelpFormatter.write_usagecCs|d|jd|fdS)z!Writes a heading into the buffer.z%*s%s: rN)rAr<)r>Zheadingrrr write_headingszHelpFormatter.write_headingcCs|jr|ddS)z#Writes a paragraph into the buffer.r!N)r=rArCrrr write_paragraphszHelpFormatter.write_paragraphc CsBt|j|jd}d|j}|t||||dd|ddS)zaWrites re-indented text into the buffer. This rewraps and preserves paragraphs. r"T)rrr/r!N)rr.r<rAr4)r>r-rKr'rrr write_texts  zHelpFormatter.write_textc Cs,t|}t|}t|dkr$tdt|d||}t|t|D]\}}|d|jd|f|sr|dqDt|||kr|d|t|n|d|d||jt |j |dd}t t || } | r|t| d| D]} |d ||jd| fqqD|dqDd S) anWrites a definition list into the buffer. This is how options and commands are usually formatted. :param rows: a list of two item tuples for the terms and values. :param col_max: the maximum width of the first column. :param col_spacing: the number of spaces between the first and second column. r6z(Expected two columns for definition listrz%*s%srr!r" z%*s%s N)listrr TypeErrorr;rrAr<rrr.iterr4r+next) r>rZcol_maxZ col_spacingrZ first_colfirstsecondrKlinesr1rrr write_dls2     zHelpFormatter.write_dlccs4||||z dVW5|XdS)zHelpful context manager that writes a paragraph, a heading, and the indents. :param name: the section name that is written as heading. N)rNrMr'rD)r>namerrr sections   zHelpFormatter.sectionccs"|z dVW5|XdS)z1A context manager that increases the indentation.N)r'rDrCrrr indentations zHelpFormatter.indentationcCs d|jS)zReturns the buffer contents.r)r%r=rCrrr getvalueszHelpFormatter.getvalue)r6NN)rrE)rQr6)__name__ __module__ __qualname____doc__r?rAr'rDrLrMrNrPrZrr\r]r^rrrr r5Ys   %  r5cCshg}d}|D].}t|d}|dkr(d}|t||fq |jddddd d |D}||fS) a Given a list of option strings this joins them in the most appropriate way and returns them in the form ``(formatted_string, any_prefix_is_slash)`` where the second item in the tuple is a flag that indicates if any of the option prefixes was a slash. Fr/TcSs|dS)Nrr)r rrr zjoin_options..)keyz, css|]}|dVqdS)rNr)rr rrr r szjoin_options..)rr$rsortr%)Zoptionsr2Zany_prefix_is_slashoptrJrrr join_optionss ri)rrrF) contextlibrZtermuirparserrZ_compatrr:rrr4objectr5rirrrr s     @