o
    0i_                     @   s   d Z ddlZddlZddlmZ ddl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 dd	lmZ d
ZzedZW n	 eyG   Y nw e	jG dd dZdS )zO
Main driver for the selection menu, based on https://github.com/bchao1/bullet
    N)Optional   )_is_package_available   )cursorinput)	Direction
clear_line
forceWrite	linebreakmove_cursorreset_cursor
writeColor)KEYMAPFzgoogle.colabc                   @   s   e Zd ZdZdg fdee defddZd(defd	d
Zde	fddZ
d)dede	fddZeed dd Zeed dd Zeed dd Zeed dd Zejdd  ed!D  d"d# Zd*d%e	fd&d'ZdS )+
BulletMenuzR
    A CLI menu to select a choice from a list of choices using the keyboard.
    Npromptchoicesc                 C   s0   d| _ || _|| _tjdkrd| _d S d| _d S )Nr   win32*u   ➔ )positionr   r   sysplatform
arrow_char)selfr   r    r   i/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/accelerate/commands/menu/selection_menu.py__init__*   s   


zBulletMenu.__init__ endc                 C   s4   t jdkrt| j| d| d S t| j| | d S )Nr       )r   r   r   r   r
   )r   indexr   r   r   r   write_choice3   s   
zBulletMenu.write_choicer    c                 C   sF   || j krtd| j d | | n
td| j|   t  dS )z$Prints the choice at the given index z    N)r   r
   r   r!   r   r   )r   r    r   r   r   print_choice9   s
   

zBulletMenu.print_choicer   	direction
num_spacesc                 C   s   | j }|tjkr| j d t| jkrdS |  j |7  _ n| j d dk r%dS |  j |8  _ t  | | t||j | | j  dS )zLShould not be directly called, used to move a direction of either up or downr   Nr   )	r   r   DOWNlenr   r	   r#   r   name)r   r$   r%   old_positionr   r   r   move_directionB   s   

zBulletMenu.move_directionupc                 C      |  tj d S N)r*   r   UPr   r   r   r   move_upR      zBulletMenu.move_updownc                 C   r,   r-   )r*   r   r&   r/   r   r   r   	move_downV   r1   zBulletMenu.move_downnewlinec                 C   s   t t| j| j d | jS Nr&   )r   r'   r   r   r/   r   r   r   selectZ   s   zBulletMenu.select	interruptc                 C   s   t t| j| j d tr5   )r   r'   r   r   KeyboardInterruptr/   r   r   r   r7   _   s   zBulletMenu.interruptc                 C   s   g | ]}t t| qS r   )r   str).0numberr   r   r   
<listcomp>d   s    zBulletMenu.<listcomp>
   c                 C   sv   t t| j}|| j }|| jkrd S |t| jk r9| j|kr)| tj|  d S | j|k r7| tj	| d S d S d S r-   )
intchrcurrent_selectionr   r'   r   r*   r   r.   r&   )r   r    movementr   r   r   
select_rowd   s   



zBulletMenu.select_rowr   default_choicec              	   C   s  | j rt  t| j d trtdd ntdd || _tt| jD ]}| | td q#t	t| j| j d t
 G 	 trVztt }W n tyU   |}Y nw |  }|durt  tt| jd D ]
}t	dd t  qj| |d |W  d   S q@1 sw   Y  dS )z-Start the menu and return the selected choice
z>Please input a choice index (starting from 0), and press enterzOPlease select a choice using the arrow or number keys, and selecting with enterr.   TNr   )r   r   r
   in_colabr   ranger'   r   r#   r   r   hider>   builtinsr   
ValueErrorhandle_inputr   r	   r!   )r   rC   ichoice_r   r   r   runt   s<   




zBulletMenu.run)r   )r   )r   )__name__
__module____qualname____doc__r   r9   listr   r!   r>   r#   r   r*   r   markr   r0   r3   r6   r7   mark_multiplerF   rB   rN   r   r   r   r   r   $   s"    		




r   )rR   rH   r   typingr   utils.importsr   r   r   r   helpersr   r	   r
   r   r   r   r   keymapr   rE   ModuleNotFoundErrorregisterr   r   r   r   r   <module>   s    $