o
    °ƒðj^&  ã                   @   sþ   d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZmZmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZ dZdd	„ Zd
d„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(ddd„Z)dS )aN  
Usage: humanfriendly [OPTIONS]

Human friendly input/output (text formatting) on the command
line based on the Python package with the same name.

Supported options:

  -c, --run-command

    Execute an external command (given as the positional arguments) and render
    a spinner and timer while the command is running. The exit status of the
    command is propagated.

  --format-table

    Read tabular data from standard input (each line is a row and each
    whitespace separated field is a column), format the data as a table and
    print the resulting table to standard output. See also the --delimiter
    option.

  -d, --delimiter=VALUE

    Change the delimiter used by --format-table to VALUE (a string). By default
    all whitespace is treated as a delimiter.

  -l, --format-length=LENGTH

    Convert a length count (given as the integer or float LENGTH) into a human
    readable string and print that string to standard output.

  -n, --format-number=VALUE

    Format a number (given as the integer or floating point number VALUE) with
    thousands separators and two decimal places (if needed) and print the
    formatted number to standard output.

  -s, --format-size=BYTES

    Convert a byte count (given as the integer BYTES) into a human readable
    string and print that string to standard output.

  -b, --binary

    Change the output of -s, --format-size to use binary multiples of bytes
    (base-2) instead of the default decimal multiples of bytes (base-10).

  -t, --format-timespan=SECONDS

    Convert a number of seconds (given as the floating point number SECONDS)
    into a human readable timespan and print that string to standard output.

  --parse-length=VALUE

    Parse a human readable length (given as the string VALUE) and print the
    number of metres to standard output.

  --parse-size=VALUE

    Parse a human readable data size (given as the string VALUE) and print the
    number of bytes to standard output.

  --demo

    Demonstrate changing the style and color of the terminal font using ANSI
    escape sequences.

  -h, --help

    Show this message and exit.
é    N)ÚTimerÚformat_lengthÚformat_numberÚformat_sizeÚformat_timespanÚparse_lengthÚ
parse_size)Úformat_pretty_tableÚformat_smart_table)
ÚANSI_COLOR_CODESÚANSI_TEXT_STYLESÚHIGHLIGHT_COLORÚ
ansi_stripÚ	ansi_wrapÚenable_ansi_supportÚfind_terminal_sizeÚoutputÚusageÚwarning)ÚSpinner)Údemonstrate_256_colorsÚdemonstrate_ansi_formattingÚmainÚprint_formatted_lengthÚprint_formatted_numberÚprint_formatted_sizeÚprint_formatted_tableÚprint_formatted_timespanÚprint_parsed_lengthÚprint_parsed_sizeÚrun_commandc            
   
   C   sÖ  t ƒ  zt tjdd… dg d¢¡\} }W n ty1 } ztd|ƒ t d¡ W Y d}~nd}~ww g }d}d}tdd„ | D ƒƒ}| D ]Š\}}|d	v rN|}qC|d
kr\| t	 
t|¡¡ qC|dkrj| t	 
t|¡¡ qC|dv rx| t	 
t|¡¡ qC|dv r†| t	 
t|¡¡ qC|dv r”| t	 
t|¡¡ qC|dv r£| t	 
t||¡¡ qC|dkrªd}qC|dv r¸| t	 
t|¡¡ qC|dkrÂ| t¡ qC|dv rÍttƒ  dS qC|rÙ| t	 
t|¡¡ |sáttƒ dS |D ]}	|	ƒ  qãdS )z9Command line interface for the ``humanfriendly`` program.é   Nzcd:l:n:s:bt:h)zrun-commandzformat-tablez
delimiter=zformat-length=zformat-number=zformat-size=Úbinaryzformat-timespan=zparse-length=zparse-size=ÚdemoÚhelpz	Error: %sFc                 s   s   � | ]	\}}|d v V  qdS ))z-bz--binaryN© )Ú.0ÚoÚvr%   r%   ú“/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/humanfriendly/cli.pyÚ	<genexpr>Ž   s   € zmain.<locals>.<genexpr>)z-dz--delimiterz--parse-sizez--parse-length)z-cz--run-command)z-lz--format-length)z-nz--format-number)z-sz--format-sizez--format-tableT)z-tz--format-timespanz--demo)z-hz--help)r   ÚgetoptÚsysÚargvÚ	Exceptionr   ÚexitÚanyÚappendÚ	functoolsÚpartialr   r   r    r   r   r   r   r   r   Ú__doc__r   )
ÚoptionsÚ	argumentsÚeÚactionsÚ	delimiterÚshould_format_tabler"   ÚoptionÚvaluer3   r%   r%   r)   r      sZ   $
€þþÿr   c                 C   s„   t ƒ }dd ttj| ƒ¡ }t||d��}t | ¡}	 | ¡  | 	¡  | 
¡ dur*nqW d  ƒ n1 s5w   Y  t |j¡ dS )zHRun an external command and show a spinner while the command is running.zWaiting for command: %sú )ÚlabelÚtimerTN)r   ÚjoinÚmapÚpipesÚquoter   Ú
subprocessÚPopenÚstepÚsleepÚpollr,   r/   Ú
returncode)Úcommand_liner?   Úspinner_labelÚspinnerÚprocessr%   r%   r)   r    °   s   
üúr    c                 C   s0   d| v rt tt| ƒƒƒ dS t tt| ƒƒƒ dS )zPrint a human readable length.Ú.N)r   r   ÚfloatÚint©r<   r%   r%   r)   r   ¾   s   r   c                 C   ó   t tt| ƒƒƒ dS )z/Print large numbers in a human readable format.N)r   r   rO   rQ   r%   r%   r)   r   Æ   ó   r   c                 C   s   t tt| ƒ|d�ƒ dS )zPrint a human readable size.)r"   N)r   r   rP   )r<   r"   r%   r%   r)   r   Ë   s   r   c                 C   s8   g }t jD ]}| ¡ }| | | ¡¡ qtt|ƒƒ dS )z8Read tabular data from standard input and print a table.N)r,   ÚstdinÚrstripr1   Úsplitr   r	   )r9   ÚdataÚliner%   r%   r)   r   Ð   s
   
r   c                 C   rR   )z Print a human readable timespan.N)r   r   rO   rQ   r%   r%   r)   r   Ù   rS   r   c                 C   ó   t t| ƒƒ dS )z=Parse a human readable length and print the number of metres.N)r   r   rQ   r%   r%   r)   r   Þ   ó   r   c                 C   rY   )z?Parse a human readable data size and print the number of bytes.N)r   r   rQ   r%   r%   r)   r   ã   rZ   r   c                     s>  t dtddd�ƒ ddg} |  t ¡ ¡ t| ƒD ]"}ttd�}|dkr'd||< | dd	¡ 	¡ }t d
t|fi |¤Žƒ qdD ]G\‰ }dtƒ fdtdd�fg‰ˆ dkr\ˆ 
ddtdd�f¡ t dtd| dd� ƒ t t‡ ‡fdd„tt ¡ ƒD ƒdgdd„ ˆD ƒ d�ƒ q=tdddƒ tdddƒ tdddƒ td d!d"ƒ d#S )$z-Demonstrate the use of ANSI escape sequences.z%szText styles:T©ÚboldÚnormalÚbright©ÚcolorÚ_r=   z - %s))r`   zForeground colors)Ú
backgroundzBackground colors)r^   rb   r   Úfaint)rc   z
%sú%s:c                    s&   g | ]‰ ˆ g‡ ‡fd d„ˆD ƒ ‘qS )c              	      sB   g | ]\}}t ˆd krdndfi tt| ¡ ƒˆˆ fg ƒ¤Ž‘qS )rb   ÚXXXXXXz      )r   ÚdictÚlistÚitems©r&   r>   Úkw)Ú
color_nameÚ
color_typer%   r)   Ú
<listcomp>ÿ   s    ýÿþÿz:demonstrate_ansi_formatting.<locals>.<listcomp>.<listcomp>r%   )r&   ©rl   Úintensities)rk   r)   rm   þ   s    ûüÿz/demonstrate_ansi_formatting.<locals>.<listcomp>ÚColorc                 S   s   g | ]\}}|  ¡ ‘qS r%   )Ú
capitalizeri   r%   r%   r)   rm     s    ÿ)Úcolumn_namesé   zstandard colorsé   é   zhigh-intensity colorsé   éç   z
216 colorséè   éÿ   zgray scale colorsN)r   r   Úextendr   ÚkeysÚsortedrf   r   Úreplacerq   Úinsertr
   r   r   )ÚstylesÚ
style_namer5   Ústyle_labelÚcolor_labelr%   rn   r)   r   è   s6   
þ
ú
ÿ
ùr   c                    sä   d}|r
|d| 7 }t dtd| dd� ƒ d dd	„ t| |d
 ƒD ƒ¡}tƒ \}}|tt|ƒƒkr8t |ƒ dS tt|ƒƒd
 ‰t|ˆ ƒ‰‡fdd„t| |d
 ƒD ƒ‰ ‡ ‡fdd„tdtˆ ƒˆƒD ƒ}t d dd	„ |D ƒ¡ƒ dS )z#Demonstrate 256 color mode support.z256 color modez (%s)Ú
rd   Tr[   Ú c                 s   s$   � | ]}d t t|ƒ|d� V  qdS )r=   r_   N)r   Ústr©r&   Únr%   r%   r)   r*     s   €" z)demonstrate_256_colors.<locals>.<genexpr>r!   c                    s"   g | ]}t t|ƒ ˆ ¡|d �‘qS )r_   )r   r…   Úrjustr†   )Úwidthr%   r)   rm   !  s   " z*demonstrate_256_colors.<locals>.<listcomp>c                    s   g | ]
}ˆ ||ˆ … ‘qS r%   r%   r†   )ÚcolorsÚcolors_per_liner%   r)   rm   "  s    r   c                 s   s   � | ]}d   |¡V  qdS )r„   N)r@   )r&   Úbr%   r%   r)   r*   #  s   € N)	r   r   r@   Úranger   Úlenr   r…   rP   )ÚiÚjÚgroupr>   Úsingle_lineÚlinesÚcolumnsÚblocksr%   )rŠ   r‹   r‰   r)   r     s   
 r   )N)*r4   r2   r+   rB   rD   r,   Úhumanfriendlyr   r   r   r   r   r   r   Úhumanfriendly.tablesr	   r
   Úhumanfriendly.terminalr   r   r   r   r   r   r   r   r   r   Úhumanfriendly.terminal.spinnersr   Ú__all__r   r    r   r   r   r   r   r   r   r   r   r%   r%   r%   r)   Ú<module>   s,   I$	01	'