o
    e7                     @   sp  d dl mZmZmZ 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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i dejd	ejd
ejdejdejdejdejdej dej!dej"dej#dej$dej%dej&dej'dej(Z)i dd dddddddd d!d"d#d$d%d&d'dd(d)d*d+d,d-d.d/d0d1d2d3d4d5Z*G d6d7 d7e+Z,G d8d9 d9eZd:d; Z-e-d<Z.d=d> Z/e0d?kr6d dlZd dl1Z1e,d Z2e2e_3e2e_4e25  e26d+d" e27d@ e28dA e9  e9dBe2:  e9dC e;d"D ]Z<e2= Z>e9e>j?e@e>jAeBe>jAe>jC q[2ej,D  dS dS )D    )absolute_importprint_functionunicode_literalsN)	AnsiState)make_keyinfomake_KeyPressmake_KeyPress_from_keydescrmake_keysym)log   )Eventz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darkgray   Zdarkred   red   Z	darkgreen   green
   Z
darkyellow   yellow   Zdarkblueblue	   Zdarkmagenta   magenta   Zdarkcyan   cyan   gray   white   c                   @   s  e Zd ZdZdJddZdd Zdd ZeeeZ[[d	d
 Z	dKddZ
dd ZedZedZedZdLddZi dejdejdejdejdejdejdejdejdejdejdejdejd ejd!ej d"ej!d#ej"Z#dLd$d%Z$dLd&d'Z%d(e&j'v rdLd)d%Z$e$Z%d*d+ Z(d,d- Z)d.d/ Z*dMd1d2Z+dLd3d4Z,d5d6 Z-dMd7d8Z.dMd9d:Z/d;d< Z0d=d> Z1dLd?d@Z2dKdAdBZ3dNdDdEZ4dFdG Z5dHdI Z6dS )O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 )zInitialize 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/   VD:\Projects\ConvertPro\env\Lib\site-packages\pyreadline3/console/ironpython_console.py__init__[   s
   

zConsole.__init__c                 C   s   t jj}td|  |S )NzWindowTop:%sr&   r$   	WindowTopr
   )r.   topr/   r/   r0   _getg   s   zConsole._getc                 C   s   t jj}td||f  d S )NzSet WindowTop:old:%s,new:%sr2   )r.   valuer4   r/   r/   r0   _setl   s   zConsole._setc                 C      dS )z"Cleanup the console when finished.Nr/   r.   r/   r/   r0   __del__s   s   zConsole.__del__Nc                 C   sB   |dur	|t j_nt jj}|dur|t j_||fS t jj}||fS )z Move or query the window cursor.N)r&   r$   Z
CursorLeftZ	CursorTop)r.   xyr/   r/   r0   posx   s   
zConsole.posc                 C   s   |  dd dS )zMove to home.r   N)r=   r9   r/   r/   r0   home   s   zConsole.homez(?\[[0-9;]*m?)z?\[([0-9;]*)m?z	([
	])c                 C   sb  |   \}}|  \}}d}| j|}|D ]}	| |	|}
t|	dkr|	d dkr1d}|d7 }nD|	d dkr:d}n;|	d dkrWdt|d d  }||krV||8 }|d7 }n|	d dkr^n|	d dkrq|d8 }|dk rp|d8 }n|d7 }||krd}|d7 }||kr|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   )r=   sizemotion_char_resplitwrite_colorlenint)r.   textr(   r;   r<   whscrollchunkschunknlr/   r/   r0   write_scrolling   sT   
zConsole.write_scrollingr   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 r"| j}z| jd|@  }| jd|@ d?  }W n ty?   |}Y nw |D ]/}| j	|}|r]t |
d t|
d| j}|t|7 }|tj_|tj_tj| qB|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_escaperF   reprr-   r(   trtable	TypeErrorescape_partsmatchgroup	ansicolorgetrH   r&   r$   r'   r,   Write)	r.   rJ   r(   rN   bgrP   ZfgrO   mr/   r/   r0   rG      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WriteConsoleArH   byref)r.   rJ   r(   rP   r/   r/   r0   write_plain   s   zConsole.write_plainZEMACSc                 C   s,   t d}| | j|t|t|d  t|S )Nr   )r`   	WriteFilerb   rH   rc   )r.   rJ   r(   Zjunkr/   r/   r0   rG      s   c                 C   s   t d|  | |S )Nzwrite("%s"))r
   rG   )r.   rJ   r/   r/   r0   write   s   
zConsole.writec                 C   r8   )NTr/   r9   r/   r/   r0   isatty     zConsole.isattyc                 C   s   d S Nr/   r9   r/   r/   r0   flush	  rh   zConsole.flush c                 C   s   t j  dS )zFill the entire screen.N)r&   r$   Clear)r.   r(   fillr/   r/   r0   page  s   zConsole.pagec                 C   s   |  || | || dS )z!Write text at the given position.N)r=   rG   )r.   r;   r<   rJ   r(   r/   r/   r0   rJ     s   zConsole.textc                 C   st   | j }| j tjj }|  }|  \}}||d  t||d  d d|  d }| |d  | j|  || _ d S )Nr   r   r   rk   )r3   r&   r$   WindowHeightr=   rD   minrG   )r.   oldtoplastliner=   rK   rL   lengthr/   r/   r0   clear_to_end_of_window  s   *

zConsole.clear_to_end_of_windowc                 C   s   | j }|  }|\}}}}	|du r| j}|r#|dd t||  }
ndt||  }
t||	D ]}tj|| | |
| q0| j|  dS )zFill Rectangle.Nr   rk   )	r3   r=   r(   absranger&   r$   ZSetCursorPositionrG   )r.   rectr(   rm   rq   oldposZx0Zy0x1y1Zrowfillr<   r/   r/   r0   	rectangle  s   zConsole.rectanglec                 C   s   t )zScroll a rectangle.)NotImplementedError)r.   rw   ZdxZdyr(   rm   r/   r/   r0   rM   0  s   zConsole.scrollc                 C   s<   | j | }|dk rd}|tjj tjjkrtjj}|| _ dS )z3Scroll the window by the indicated number of lines.r   N)r3   r&   r$   ro   BufferHeight)r.   linesr4   r/   r/   r0   scroll_window4  s   

zConsole.scroll_windowc                 C   sx   t j}	 t jd}|jt jjkr| d n$|jt jjkr$| d nt|j	dkr6t
d|  t| |S t| |S q)z<Return next key press event from the queue, ignoring others.Tr   i zDeadkey: %s)r&   
ConsoleKeyr$   ZReadKeyKeyZPageDownr   ZPageUpr*   KeyCharr
   event)r.   Zcker/   r/   r0   getkeypress=  s   

zConsole.getkeypressc                 C   s   |r|t j_dS t jjS )zSet/get title.N)r&   r$   ZTitle)r.   txtr/   r/   r0   titleL  s   zConsole.titlec                 C   sh   t j}|dur|dur|||_|_n|j|jfS |dur*|dur*|||_|_dS |jd |jd fS )zSet/get window size.Nr   )r&   r$   ZBufferWidthr}   ZWindowWidthro   )r.   widthheightscr/   r/   r0   rD   S  s   zConsole.sizeTc                 C   s   |t j_dS )zSet cursor on or off.N)r&   r$   ZCursorVisible)r.   visiblerD   r/   r/   r0   cursor`  s   zConsole.cursorc                 C   s   t j  d S ri   )r&   r$   ZBeepr9   r/   r/   r0   belld  s   zConsole.bellc                 C   s   |  j d7  _ | j S )zGet next event serial number.r   )r%   r9   r/   r/   r0   next_serialg  s   zConsole.next_serial)r   )NNri   )Nrk   )TN)7__name__
__module____qualname____doc__r1   r5   r7   propertyr3   r:   r=   r>   recompilerT   rX   rE   rR   colorBlackDarkRed	DarkGreen
DarkYellowDarkBlueDarkMagentaDarkCyanGrayDarkGrayRedGreenYellowBlueMagentaCyanWhiterV   rG   rd   osenvironrf   rg   rj   rn   rJ   rt   r{   rM   r   r   r   rD   r   r   r   r/   r/   r/   r0   r$   V   sz    






9









	


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   r;   r<   r*   r   charr   Zkeycode	Modifiersstater
   r	   Zkeysymr   keyinfo)r.   consoleinputr/   r/   r0   r1   p  s   
zevent.__init__N)r   r   r   r   r1   r/   r/   r/   r0   r   m  s    r   c                 C   sH   dd }d|_ tjj|_tjj|_||_t	||}|`t
| }||_|S )Nc                   S   r8   )Nr   r/   r/   r/   r/   r0   r     rh   z'make_event_from_keydescr.<locals>.inputa)r   r&   r   Ar   ZConsoleModifiersZShiftr   r   r   r   r   )Zkeydescrr   r   r   r/   r/   r0   make_event_from_keydescr  s   


r   z	Control-cc                    s0    fddG fdddt j}| t j_d S )Nc               
      sf   z  } W n t y } zd} W Y d }~nd }~w ty"   Y d S w | dd  dkr1| d d S | S )N r?   )KeyboardInterruptEOFError)resr;   )hookr/   r0   	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 ri   r/   )r.   ZautoIndentSizer   r/   r0   ReadLine  s   z4install_readline.<locals>.IronPythonWrapper.ReadLinec                 S      t j| d S ri   )r&   r$   r]   r.   rJ   styler/   r/   r0   r]        z1install_readline.<locals>.IronPythonWrapper.Writec                 S   r   ri   )r&   r$   	WriteLiner   r/   r/   r0   r     r   z5install_readline.<locals>.IronPythonWrapper.WriteLineN)r   r   r   r   r]   r   r/   r   r/   r0   IronPythonWrapper  s    r   )IronPythonConsoleZIConsoleZPythonCommandLineZ	MyConsole)r   r   r/   )r   r   r0   install_readline  s   	r   __main__zhi therezTesting consolerD   z  some printed output)E
__future__r   r   r   r   r   r   r&   Zpyreadline3.console.ansir   Zpyreadline3.keysymsr   r   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   r[   r)   objectr$   r   CTRL_C_EVENTr   r   timecstdoutstderrrn   r=   rf   r   printrD   rv   ir   r   r   chrr   ordr   rl   r/   r/   r/   r0   <module>   s   		
  


"