U
    0-e8                     @   s   d Z ddlZddl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k
r   Y nX ejG dd dZdS )zO
Main driver for the selection menu, based on https://github.com/bchao1/bullet
    N   )_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eedddZd&eddd	Zed
ddZ	d'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ed dd Zejdd edD  d d! Zd(ed#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   s.   d| _ || _|| _tjdkr$d| _nd| _d S )Nr   win32*u   ➔ )positionr   r   sysplatform
arrow_char)selfr   r    r   h/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/accelerate/commands/menu/selection_menu.py__init__(   s    
zBulletMenu.__init__ )endc                 C   s2   t jdkrt| j| d| nt| j| | d S )Nr       )r   r   r   r   r	   )r   indexr   r   r   r   write_choice1   s    
zBulletMenu.write_choice)r   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_choice7   s
    
zBulletMenu.print_choicer   )	direction
num_spacesc                 C   s   | j }|tjkr8| j d t| jkr(dS |  j |7  _ n | j d dk rJd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$   Zold_positionr   r   r   move_direction@   s    

zBulletMenu.move_directionupc                 C   s   |  tj d S N)r(   r   UPr   r   r   r   move_upP   s    zBulletMenu.move_upZdownc                 C   s   |  tj d S r*   )r(   r   r%   r,   r   r   r   	move_downT   s    zBulletMenu.move_downnewlinec                 C   s   t t| j| j d | jS Nr%   )r   r&   r   r   r,   r   r   r   selectX   s    zBulletMenu.select	interruptc                 C   s   t t| j| j d td S r0   )r   r&   r   r   KeyboardInterruptr,   r   r   r   r2   ]   s    zBulletMenu.interruptc                 C   s   g | ]}t t| qS r   )r   str).0numberr   r   r   
<listcomp>b   s     zBulletMenu.<listcomp>
   c                 C   sx   t t| j}|| j }|| jkr&d S |t| jk rp| j|krP| tj|  qt| j|k rj| tj	| qtd S nd S d S r*   )
intchrZcurrent_selectionr   r&   r   r(   r   r+   r%   )r   r   Zmovementr   r   r   
select_rowb   s    



zBulletMenu.select_rowr   )default_choicec              
   C   s  | j r2t  t| j d tr(tdd n
tdd || _tt| jD ]}| | td qFt	t| j| j d t
  trztt }W q tk
r   |}Y qX n|  }|dk	r~t  tt| jd D ]}t	dd t  q| |d |W  5 Q R  S q~W 5 Q R X 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+   Nr   )r   r
   r	   in_colabr   ranger&   r   r"   r   r   Zhider9   builtinsr   
ValueErrorZhandle_inputr   r   r    )r   r<   ichoice_r   r   r   runr   s2    




zBulletMenu.run)r   )r   )r   )__name__
__module____qualname____doc__r4   listr   r    r9   r"   r   r(   r   markr   r-   r.   r1   r2   Zmark_multipler?   r;   rE   r   r   r   r   r   "   s    		




r   )rI   r@   r   Zutils.importsr   r   r   r   helpersr   r   r	   r
   r   r   r   Zkeymapr   r>   ModuleNotFoundErrorregisterr   r   r   r   r   <module>   s   $