o
    3g5                     @  s   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ g dZd$ddZd%d&ddZd'd(ddZd'ddZeedd fddZd)d d!Zd'd"d#ZdS )*z7
Terminal-related utilities
--------------------------
    )annotationsN)local   )Progress)get_terminal_size)readlineaskchoosepromptr   r   r   return	list[str]c                   C  s   t S N)__all__ r   r   L/home/garg/my-data/venv/lib/python3.10/site-packages/plumbum/cli/terminal.py__dir__   s   r    messagestrc                 C  s    t j|  t j  t j S )z*Gets a line of input from the user (stdin))sysstdoutwriteflushstdinr   )r   r   r   r   r       s   

r   questiondefaultbool | Noneboolc                 C  s   |    d  d } |du r| d7 } n|r| d7 } n| d7 } 	 z
t|   }W n ty5   d}Y nw |dv r<dS |dv rBd	S |sJ|durJ|S tjd
 q )a  
    Presents the user with a yes/no question.

    :param question: The question to ask
    :param default: If ``None``, the user must answer. If ``True`` or ``False``, lack of response is
                    interpreted as the default option

    :returns: the user's choice
    ?Nz (y/n) z [Y/n] z [y/N] T>   yyes>   nnoFz#Invalid response, please try again
)rstripr   striplowerEOFErrorr   r   r   )r   r   answerr   r   r   r   '   s(   


r   c                 C  sP  t |dr	| }tj|  d  i }d}t|dD ]9\}}t|tt	fr6t
|dkr6|d }|d }n|}|}|||< |durH||krH|}tjd| d| d q|durk|du rdd	| d
nd	| d
}	nd}		 zt|	 }
W n ty   d}
Y nw |
s|r|S zt|
}
|
|vrt W ||
 S  ty   tjd Y qmw )a  Prompts the user with a question and a set of options, from which the user needs to choose.

    :param question: The question to ask
    :param options: A set of options. It can be a list (of strings or two-tuples, mapping text
                    to returned-object) or a dict (mapping text to returned-object).``
    :param default: If ``None``, the user must answer. Otherwise, lack of response is interpreted
                    as this answer

    :returns: The user's choice

    Example::

        ans = choose("What is your favorite color?", ["blue", "yellow", "green"], default = "yellow")
        # `ans` will be one of "blue", "yellow" or "green"

        ans = choose("What is your favorite color?",
                {"blue" : 0x0000ff, "yellow" : 0xffff00 , "green" : 0x00ff00}, default = 0x00ff00)
        # this will display "blue", "yellow" and "green" but return a numerical value
    items
Nr      r   (z) zChoice [z]: zChoice: Tr   z!Invalid choice, please try again
)hasattrr(   r   r   r   r#   	enumerate
isinstancetuplelistlenr   r$   r&   int
ValueError)r   optionsr   choicesdefindexiitemtextvalmsgchoicer   r   r   r	   G   sH   

"r	   c                 C  s   dS )NTr   )_r   r   r   <lambda>   s    r>   c              
   C  s
  |  d} |tur| d| d7 } | d7 } 	 zt|  }W n ty)   d}Y nw |s3|tur2|S qz||}W n ttfyW } ztj	d| d W Y d	}~qd	}~ww z||}W n tyy } ztj	| d
 W Y d	}~qd	}~ww |stj	d q|S )a  
    Presents the user with a validated question, keeps asking if validation does not pass.

    :param question: The question to ask
    :param type: The type of the answer, defaults to str
    :param default: The default choice
    :param validator: An extra validator called after type conversion, can raise ValueError or return False to trigger a retry.

    :returns: the user's choice
    z 	:z []z: Tr   zInvalid value (z), please try again
Nz, please try again
z/Value not in specified range, please try again
)
r#   NotImplementedr   r$   r&   	TypeErrorr3   r   r   r   )r   typer   	validatoransexvalidr   r   r   r
      s@   
r
      Tc                 #  s    t dr fdd}n fdd}d}d}t| D ]@\}}ddd	 |D }d
dd	 |D }	|r?||kr?d}q|}|rFdV  | d d}
|  dd|
 d|	 V  d}qdS )zConvert the given bytes (or a stream with a buffering ``read()`` method) to hexdump-formatted lines,
    with possible aggregation of identical lines. Returns a generator of formatted lines.
    readc                  3  s    	   } | sd S | V  qr   )rH   )bufbytes_per_linedata_or_streamr   r   
read_chunk   s   
zhexdump.<locals>.read_chunkc                  3  s.    t dt D ]} | |    V  q	d S )Nr   )ranger1   )r7   rJ   r   r   rM      s   NF c                 s  s    | ]	}t |d V  qdS )02xNord.0chr   r   r   	<genexpr>   s    zhexdump.<locals>.<genexpr>r   c                 s  s4    | ]}d t |  krdk rn n|ndV  qdS )       .NrQ   rS   r   r   r   rV      s   2 T*   06xz | z| )r,   r-   joinljust)rL   rK   	aggregaterM   prevskippedr7   chunkhexdr9   
hexd_ljustr   rJ   r   hexdump   s&   
	re   c                 C  s  |st d d }t| dr|  } |jddd}zp| D ]}| d}z|j| |j  W q ty9   Y  nw |j  |	  W t
t |   W d   n1 sXw   Y  |r| du rt
t |  W d   n1 syw   Y  td dS dS dS t
t |   W d   n1 sw   Y  |r| du rt
t |  W d   n1 sw   Y  td w w w )zOpens a pager (e.g., ``less``) to display the given text. Requires a terminal.

    :param rows: a ``bytes`` or a list/iterator of "rows" (``bytes``)
    :param pagercmd: the pager program to run. Defaults to ``less -RSin``
    lessz-RSin
splitlinesN)r   stderrr)   reset)r   r,   rg   popenr   r   r   OSErrorclosewait
contextlibsuppress	Exceptionpoll	terminateossystem)rowspagercmdpgrowliner   r   r   pager   sD   







rz   )r   r   )r   )r   r   r   r   r   )r   r   r   r   r   r   )rG   T)__doc__
__future__r   rn   rs   r   plumbumr   progressr   termsizer   r   r   r   r   r	   r   r@   r
   re   rz   r   r   r   r   <module>   s&    

 ;

1#