a
    IDg^&                     @   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 n8 tyb } z td| td W Y d}~n
d}~0 0 g }d}d}tdd | D }| D ] \}}|d	v r|}q|d
kr|t	
t| q|dkr|t	
t| q|dv r|t	
t| q|dv r|t	
t| q|dv r.|t	
t| q|dv rN|t	
t|| q|dkr^d}q|dv r||t	
t| q|dkr|t q|dv rtt  dS q|r|t	
t| |stt dS |D ]}	|	  qd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%   b/mounts/lovelace/software/anaconda3/envs/paleomix/lib/python3.9/site-packages/humanfriendly/cli.py	<genexpr>       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	delimiterZshould_format_tabler"   optionvaluer4   r%   r%   r)   r      sR    $
 





r   c                 C   s   t  }ddttj|  }t||d:}t| }|  |	  |
 dur4qTq4W d   n1 sh0    Y  t|j dS )zHRun an external command and show a spinner while the command is running.zWaiting for command: %s )labeltimerN)r   joinmappipesquoter   
subprocessPopenstepsleeppollr-   r0   
returncode)Zcommand_liner?   Zspinner_labelspinnerprocessr%   r%   r)   r       s    
"r    c                 C   s.   d| v rt tt|  n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   s   t tt|  dS )z/Print large numbers in a human readable format.N)r   r   rM   rO   r%   r%   r)   r      s    r   c                 C   s   t tt| |d dS )zPrint a human readable size.)r"   N)r   r   rN   )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rstripr2   splitr   r	   )r:   dataliner%   r%   r)   r      s
    
r   c                 C   s   t tt|  dS )z Print a human readable timespan.N)r   r   rM   rO   r%   r%   r)   r      s    r   c                 C   s   t t|  dS )z=Parse a human readable length and print the number of metres.N)r   r   rO   r%   r%   r)   r      s    r   c                 C   s   t t|  dS )z?Parse a human readable data size and print the number of bytes.N)r   r   rO   r%   r%   r)   r      s    r   c                     s>  t dtddd ddg} | t  t| D ]D}ttd}|dkrNd||< |dd		 }t d
t|fi | q0dD ]\ }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 qzt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))rZ   zForeground colors)
backgroundzBackground colors)rX   r\   r   faint)r]   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 )r\   ZXXXXXXz      )r   dictlistitemsr&   r>   kw)
color_name
color_typer%   r)   
<listcomp>   s   z:demonstrate_ansi_formatting.<locals>.<listcomp>.<listcomp>r%   )r&   re   Zintensities)rd   r)   rf      s   z/demonstrate_ansi_formatting.<locals>.<listcomp>Colorc                 S   s   g | ]\}}|  qS r%   )
capitalizerb   r%   r%   r)   rf     s   )Zcolumn_names   zstandard colors      zhigh-intensity colors      z
216 colors      zgray scale colorsN)r   r   extendr   keyssortedr_   r   replaceri   insertr
   r   r   )styles
style_namer6   Zstyle_labelZcolor_labelr%   rg   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rnt | npt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)
r^   TrU    c                 s   s"   | ]}d t t||d V  qdS )r=   rY   N)r   strr&   nr%   r%   r)   r*     r+   z)demonstrate_256_colors.<locals>.<genexpr>r!   c                    s"   g | ]}t t| |d qS )rY   )r   rz   rjustr{   )widthr%   r)   rf   !  r+   z*demonstrate_256_colors.<locals>.<listcomp>c                    s   g | ]} ||  qS r%   r%   r{   )colorscolors_per_liner%   r)   rf   "  r+   r   c                 s   s   | ]}d  |V  qdS )ry   N)r@   )r&   br%   r%   r)   r*   #  r+   N)	r   r   r@   ranger   lenr   rz   rN   )ijgroupr>   Zsingle_linelinescolumnsblocksr%   )r   r   r~   r)   r     s    

 r   )N)*r5   r3   r,   rB   rD   r-   Zhumanfriendlyr   r   r   r   r   r   r   Zhumanfriendly.tablesr	   r
   Zhumanfriendly.terminalr   r   r   r   r   r   r   r   r   r   Zhumanfriendly.terminal.spinnersr   __all__r   r    r   r   r   r   r   r   r   r   r   r%   r%   r%   r)   <module>   s*   I$	01	'