a
    ñ»jl7  ã                   @   sÖ  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZ ddlmZ d dlZd dlZe ej¡ ejZejejejejejejejejejejejejej ej!ej"ej#dœZ$d dd	d
dddddddddddddœZ%G dd„ de&ƒZ'G dd„ deƒZdd„ Z(e(dƒZ)dd„ Z*e+d k�rÒd dlZd dl,Z,e'd ƒZ-e-e_.e-e_/e- 0¡  e- 1dd¡ e- 2d!¡ e- 3d"¡ e4ƒ  e4d#e- 5¡ ƒ e4d$ƒ e6dƒD ],Z7e- 8¡ Z9e4e9j:e;e9j<ƒe=e9j<ƒe9j>ƒ �q˜[-ej' ?¡  dS )%é    N)Ú	AnsiState)Úmake_key_pressÚ#make_key_press_from_key_descriptionÚmake_key_symbol)Úlogé   )ÚEvent)z0;30z0;31z0;32z0;33z0;34z0;35z0;36z0;37z1;30z1;31z1;32z1;33z1;34z1;35z1;36z1;37é   é   é   é   é
   é   é   é	   é   é   é   é   é   é   )ÚblackZdarkgrayZdarkredÚredZ	darkgreenÚgreenZ
darkyellowÚyellowZdarkblueÚblueZdarkmagentaÚmagentaZdarkcyanÚcyanÚgrayÚwhitec                   @   sr  e Zd ZdZd<dd„Zdd„ Zdd„ ZeeeƒZ[[d	d
„ Z	d=dd„Z
dd„ Ze d¡Ze d¡Ze d¡Zd>dd„Zejejejejejejejejejejejejejej ej!ej"dœZ#d?dd„Z$d@dd„Z%de&j'v rèdAdd„Z$e$Z%dd„ Z(dd„ Z)d d!„ Z*dBd#d$„Z+dCd%d&„Z,d'd(„ Z-dDd)d*„Z.dEd+d,„Z/d-d.„ Z0d/d0„ Z1dFd1d2„Z2dGd3d4„Z3dHd6d7„Z4d8d9„ Z5d:d;„ Z6dS )IÚConsolezConsole driver for Windows.r   c                 C   sB   d| _ tjj| _tttjjƒ ¡  | _tjj	| _
td| j ƒ dS )zŒInitialize the Console object.

        newbuffer=1 will allocate a new buffer so the old content will be restored
        on exit.
        r   zinitial attr=%sN)ÚserialÚSystemr    ÚForegroundColorÚattrÚwinattrÚstrÚlowerZsaveattrÚBackgroundColorÚsavebgr   )ÚselfZ	newbuffer© r+   úLE:\soccer\ml_env\Lib\site-packages\pyreadline3/console/ironpython_console.pyÚ__init__a   s
    

zConsole.__init__c                 C   s   t jj}td| ƒ |S )NzWindowTop:%s©r"   r    Ú	WindowTopr   )r*   Útopr+   r+   r,   Ú_getm   s    zConsole._getc                 C   s   t jj}td||f ƒ d S )NzSet WindowTop:old:%s,new:%sr.   )r*   Úvaluer0   r+   r+   r,   Ú_setr   s    zConsole._setc                 C   s   dS )z"Cleanup the console when finished.Nr+   ©r*   r+   r+   r,   Ú__del__y   s    zConsole.__del__Nc                 C   s<   |dur|t j_nt jj}|dur,|t j_nt jj}||fS )z Move or query the window cursor.N)r"   r    Z
CursorLeftZ	CursorTop)r*   ÚxÚyr+   r+   r,   Úpos~   s    

zConsole.posc                 C   s   |   dd¡ dS )zMove to home.r   N)r8   r4   r+   r+   r,   ÚhomeŠ   s    zConsole.homez(?\[[0-9;]*m?)z?\[([0-9;]*)m?z	([
	])c                 C   sj  |   ¡ \}}|  ¡ \}}d}| j |¡}|D �]6}	|  |	|¡}
t|	ƒdk�r |	d dkrfd}|d7 }nˆ|	d dkrxd}nv|	d dkr²dt|d ƒd  }||krî||8 }|d7 }n<|	d dkrÀn.|	d dkræ|d8 }|dk rî|d8 }n|d7 }||k�rd}|d7 }||k�rd|d7 }|d }q,||
7 }t|| ƒ}|| }||7 }||kr,||| d 7 }|d }q,|S )	ah  write text at current cursor position while watching for scrolling.

        If the window scrolls because you are at the bottom of the screen
        buffer, all positions that you are storing will be shifted by the
        scroll amount. For example, I remember the cursor position of the
        prompt so that I can redraw the line but if the window scrolls,
        the remembered position is off.

        This variant of write tries to keep track of the cursor position
        so that it will know when the screen buffer is scrolled. It
        returns the number of lines that the buffer scrolled.

        r   r   Ú
úú	r	   úú)r8   ÚsizeÚmotion_char_reÚsplitÚwrite_colorÚlenÚint)r*   Útextr$   r6   r7   ÚwÚhÚscrollÚchunksÚchunkÚnÚlr+   r+   r,   Úwrite_scrolling—   sL    







zConsole.write_scrolling)r   r
   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   c           	      C   sè   t d||f ƒ | j |¡}t dt|ƒ ƒ | j}d}|du rD| j}z$| jd|@  }| jd|@ d?  }W n ty~   |}Y n0 |D ]^}| j 	|¡}|rºt | 
d¡ƒ t | 
d¡| j¡}|t|ƒ7 }|tj_|tj_tj |¡ q„|S )	z}write text at current cursor position and interpret color escapes.

        return the number of characters written.
        zwrite_color("%s", %s)z	chunks=%sr   Nr   éð   r
   r   )r   Úterminal_escaperA   Úreprr)   r$   ÚtrtableÚ	TypeErrorÚescape_partsÚmatchÚgroupÚ	ansicolorÚgetrC   r"   r    r#   r(   ÚWrite)	r*   rE   r$   rI   ÚbgrK   ZfgrJ   Úmr+   r+   r,   rB   ã   s,    
zConsole.write_colorc                 C   sX   t d||f ƒ |du r| j}tdƒ}|  | j|¡ |  | j|t|ƒt|ƒd¡ t|ƒS )z&write text at current cursor position.zwrite("%s", %s)Nr   )r   r$   Úc_intÚSetConsoleTextAttributeÚhoutZWriteConsoleArC   Úbyref)r*   rE   r$   rK   r+   r+   r,   Úwrite_plain   s    zConsole.write_plainZEMACSc                 C   s,   t dƒ}|  | j|t|ƒt|ƒd ¡ t|ƒS )Nr   )r[   Ú	WriteFiler]   rC   r^   )r*   rE   r$   Zjunkr+   r+   r,   rB     s    c                 C   s   t d| ƒ |  |¡S )Nzwrite("%s"))r   rB   )r*   rE   r+   r+   r,   Úwrite  s    zConsole.writec                 C   s   dS )NTr+   r4   r+   r+   r,   Úisatty  s    zConsole.isattyc                 C   s   d S ©Nr+   r4   r+   r+   r,   Úflush  s    zConsole.flushú c                 C   s   t j ¡  dS )zFill the entire screen.N)r"   r    ÚClear)r*   r$   Úfillr+   r+   r,   Úpage   s    zConsole.pagec                 C   s   |   ||¡ |  ||¡ dS )z!Write text at the given position.N)r8   rB   )r*   r6   r7   rE   r$   r+   r+   r,   rE   $  s    zConsole.textc                 C   st   | j }| j tjj }|  ¡ }|  ¡ \}}||d  t||d  d dƒ|  d }|  |d ¡ | j|Ž  || _ d S )Nr   r   r   re   )r/   r"   r    ÚWindowHeightr8   r?   ÚminrB   )r*   ÚoldtopÚlastliner8   rF   rG   Úlengthr+   r+   r,   Úclear_to_end_of_window)  s    *
zConsole.clear_to_end_of_windowc                 C   sŽ   | j }|  ¡ }|\}}}}	|du r(| j}|rF|dd… t|| ƒ }
ndt|| ƒ }
t||	ƒD ]}tj ||¡ |  |
|¡ q`| j|Ž  dS )zFill Rectangle.Nr   re   )	r/   r8   r$   ÚabsÚranger"   r    ZSetCursorPositionrB   )r*   Úrectr$   rg   rk   ÚoldposZx0Zy0Úx1Úy1Zrowfillr7   r+   r+   r,   Ú	rectangle3  s    zConsole.rectanglec                 C   s   t ‚dS )zScroll a rectangle.N)ÚNotImplementedError)r*   rq   ZdxZdyr$   rg   r+   r+   r,   rH   D  s    zConsole.scrollc                 C   s<   | j | }|dk rd}|tjj tjjkr2tjj}|| _ dS )z3Scroll the window by the indicated number of lines.r   N)r/   r"   r    ri   ÚBufferHeight)r*   Úlinesr0   r+   r+   r,   Úscroll_windowH  s    
zConsole.scroll_windowc                 C   sz   t j}t j d¡}|jt jjkr,|  d¡ q|jt jjkrF|  d¡ qt|j	ƒdkrjt
d| ƒ t| |ƒS t| |ƒS qdS )z<Return next key press event from the queue, ignoring others.Tr   iôÿÿÿú zDeadkey: %sN)r"   Ú
ConsoleKeyr    ZReadKeyÚKeyZPageDownry   ZPageUpr&   ÚKeyCharr   Úevent)r*   ZckÚer+   r+   r,   ÚgetkeypressQ  s    
zConsole.getkeypressc                 C   s   |r|t j_nt jjS dS )zSet/get title.N)r"   r    ZTitle)r*   Útxtr+   r+   r,   Útitle`  s    
zConsole.titlec                 C   sj   t j}|dur&|dur&|| |_|_n|j|jfS |durR|durR|| |_|_n|jd |jd fS dS )zSet/get window size.Nr   )r"   r    ZBufferWidthrw   ZWindowWidthri   )r*   ÚwidthÚheightÚscr+   r+   r,   r?   g  s    zConsole.sizeTc                 C   s   |t j_dS )zSet cursor on or off.N)r"   r    ZCursorVisible)r*   Úvisibler?   r+   r+   r,   Úcursort  s    zConsole.cursorc                 C   s   t j ¡  d S rc   )r"   r    ZBeepr4   r+   r+   r,   Úbellx  s    zConsole.bellc                 C   s   |  j d7  _ | j S )zGet next event serial number.r   )r!   r4   r+   r+   r,   Únext_serial{  s    zConsole.next_serial)r   )NN)N)N)N)N)Nre   )N)Nre   )Nre   )N)NN)TN)7Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r1   r3   Úpropertyr/   r5   r8   r9   ÚreÚcompilerO   rS   r@   rM   ÚcolorÚBlackÚDarkRedÚ	DarkGreenÚ
DarkYellowÚDarkBlueÚDarkMagentaÚDarkCyanÚGrayÚDarkGrayÚRedÚGreenÚYellowÚBlueÚMagentaÚCyanÚWhiterQ   rB   r_   ÚosÚenvironra   rb   rd   rh   rE   rn   ru   rH   ry   r€   r‚   r?   r‡   rˆ   r‰   r+   r+   r+   r,   r    ^   sd   






:ð









	


r    c                   @   s   e Zd ZdZdd„ ZdS )r~   z"Represent events from the console.c                 C   s†   d| _ | ¡ | _d| _d| _d| _d| _t|jƒ| _	|j
| _|j| _td|j|j
|jf ƒ d| _ t| jƒ| _t| j	| j| jƒ| _dS )z5Initialize an event from the Windows input structure.z??r   z%s,%s,%sZ
KeyReleaseN)Útyper‰   r!   rƒ   r„   r6   r7   r&   r}   Úcharr|   ZkeycodeÚ	ModifiersÚstater   r   Zkeysymr   Úkeyinfo)r*   ÚconsoleÚinputr+   r+   r,   r-   „  s    
zevent.__init__N)rŠ   r‹   rŒ   r�   r-   r+   r+   r+   r,   r~   �  s   r~   c                 C   sH   dd„ }d|_ tjj|_tjj|_||_t	||ƒ}|`t
| ƒ}||_|S )Nc                   S   s   dS )Nr   r+   r+   r+   r+   r,   rª   –  s    z.make_event_from_key_description.<locals>.inputÚa)r}   r"   r{   ÚAr|   ZConsoleModifiersZShiftr¦   r‰   r~   r   r¨   )Zkey_descriptionrª   r   r¨   r+   r+   r,   Úmake_event_from_key_description•  s    


r­   z	Control-cc                    s0   ‡ fdd„‰G ‡fdd„dt jƒ}|ƒ t j_d S )Nc               
      sj   z
ˆ ƒ } W n: t y2 } zd} W Y d }~nd }~0  tyD   Y d S 0 | dd … dkrb| d d… S | S d S )NÚ éÿÿÿÿr:   )ÚKeyboardInterruptÚEOFError)Úresr6   )Úhookr+   r,   Ú	hook_wrap¨  s    
z#install_readline.<locals>.hook_wrapc                       s(   e Zd Z‡ fdd„Zdd„ Zdd„ ZdS )z+install_readline.<locals>.IronPythonWrapperc                    s   ˆ ƒ S rc   r+   )r*   ZautoIndentSize©r´   r+   r,   ÚReadLineµ  s    z4install_readline.<locals>.IronPythonWrapper.ReadLinec                 S   s   t j |¡ d S rc   )r"   r    rX   ©r*   rE   Ústyler+   r+   r,   rX   ¸  s    z1install_readline.<locals>.IronPythonWrapper.Writec                 S   s   t j |¡ d S rc   )r"   r    Ú	WriteLiner·   r+   r+   r,   r¹   »  s    z5install_readline.<locals>.IronPythonWrapper.WriteLineN)rŠ   r‹   rŒ   r¶   rX   r¹   r+   rµ   r+   r,   ÚIronPythonWrapper´  s   rº   )ÚIronPythonConsoleZIConsoleZPythonCommandLineZ	MyConsole)r³   rº   r+   )r³   r´   r,   Úinstall_readline§  s    
r¼   Ú__main__zhi therezTesting consoler?   z  some printed output)@r¢   r�   r»   r"   Zpyreadline3.console.ansir   Zpyreadline3.keysymsr   r   r   Zpyreadline3.loggerr   r~   r   ÚsysZclrZAddReferenceToFileAndPathÚ
executableZConsoleColorr‘   r’   r“   r”   r•   r–   r—   r˜   rš   r™   r›   rœ   r�   rž   rŸ   r    r¡   rV   r%   Úobjectr    r­   ÚCTRL_C_EVENTr¼   rŠ   ÚtimeÚcÚstdoutÚstderrrh   r8   ra   r‚   Úprintr?   rp   Úir€   r   r|   Úchrr}   Úordr¦   rf   r+   r+   r+   r,   Ú<module>   sŒ   ðð  %


"