Ϫf? dZddlZddlZddlmZddlmZddlmZddl m Z m Z m Z ddl mZddlmZdd lmZdd lmZmZd Zd Zed \ ZZZZZZZZ Z!GddejDZ#e#Z$eedddddddeejJGdde jLZ'Gdde(Z)Gdde'Z*gdZ+y)z< Partial in-memory terminal emulator @author: Jp Calderone N) implementer)Version)insults)deferprotocolreactor)Logger)_textattributes) iterbytes) deprecateddeprecatedModuleAttribute( cteZdZdZdZej ddddeedfdZ e e ddddd Z d Z y ) _FormattingStatez Represents the formatting state/attributes of a single character. Character set, intensity, underlinedness, blinkitude, video reversal, as well as foreground and background colors made up a character's attributes. charsetbold underlineblink reverseVideo foreground background _subtractingFc t||_||_||_||_||_||_||_||_yNr) selfrrrrrrrrs >/usr/lib/python3/dist-packages/twisted/conch/insults/helper.py__init__z_FormattingState.__init__3s>  " ($$(Twisted rc L|j\}}|j||S)a< Add a character attribute to a copy of this formatting state. @param kw: An optional attribute name and value can be provided with a keyword argument. @return: A formatting state instance with the new attribute. @see: L{DefaultFormattingState._withAttribute}. )popitem_withAttribute)rkwkvs rwantOnez_FormattingState.wantOneGs&zz|1""1a((r!cg}|jr|jd|jr|jtj|j r|jtj |jr|jtj|jr|jtj|jtk7r"|jt|jz|jtk7r"|jt |jz|r%ddj#t%t&|zdzSy)Nrz[;m)rappendrrBOLDr UNDERLINErBLINKr REVERSE_VIDEOrWHITE FOREGROUNDrBLACK BACKGROUNDjoinmapstr)rattrss rtoVT102z_FormattingState.toVT102Vs    LLO 99 LL & >> LL** + :: LL '    LL.. / ??e # LLdoo5 6 ??e # LLdoo5 6 SXXc#uo66< <r!N)__name__ __module__ __qualname____doc__compareAttributesrG0r5r7r r rr+r=r!rrrs[  )( 2q!,- ). )r!rr"r#r$z=Use twisted.conch.insults.text.assembleFormattedText instead.ztwisted.conch.insults.helperCharacterAttributeceZdZdZdD] ZedzZej dZee"dZ dZ dZ dZ d Z eZeZd Zd Zd Zd ZdZdZdZdZdAdZdAdZdAdZdAdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'd Z(d!Z)d"Z*d#Z+d$Z,d%Z-d&Z.d'Z/d(Z0d)Z1d*Z2d+Z3d,Z4d-Z5d.Z6dAd/Z7dAd0Z8dAd1Z9d2Z:d3Z;d4Zd7Z?d8Z@d9ZAd:ZBd;ZCd<ZDdBd=ZEd>ZFd?ZGy@)CTerminalBufferz) An in-memory terminal emulator. )sUP_ARROWs DOWN_ARROWs RIGHT_ARROWs LEFT_ARROWsHOMEsINSERTsDELETEsENDsPGUPsPGDNsF1sF2sF3sF4sF5sF6sF7sF8sF9sF10sF11sF12s = object()ascii P c&|j||Sr)lines)rxys r getCharacterzTerminalBuffer.getCharacterszz!}Qr!c$|jyr)resetrs rconnectionMadezTerminalBuffer.connectionMades  r!cft|jddD]}|j|y)z Add the given printable bytes to the terminal. Line feeds in L{bytes} will be replaced with carriage return / line feed pairs.  s N)r replaceinsertAtCursor)rdatabs rwritezTerminalBuffer.writes14<<w78 #A    " #r!cBt|jfi|jSr)r activeCharsetgraphicRenditionrUs r_currentFormattingStatez&TerminalBuffer._currentFormattingStates 2 2Ld6K6KLLr!c|dk(rd|_y|dk(r|jy|tjj dvr|j|j k\r|j ||jf}|jjtjjrY|g|j|j|j|j|j|jjn&||j|j|j<|xjdz c_yy)a Add one byte to the terminal at the cursor and make consequent state updates. If b is a carriage return, move the cursor to the beginning of the current row. If b is a line feed, move the cursor to the next row or scroll down if the cursor is already in the last row. Otherwise, if b is printable, put it at the cursor position (inserting or overwriting as dictated by the current mode) and move the cursor.  rrXrHr$N)rP _scrollDownstring printableencodewidthnextLineramodesgetrIRMrOrQpop)rr\chs rrZzTerminalBuffer.insertAtCursors :DF %Z     &""))'2 2vv# T1134Bzz~~gmm//079d 466"466DFF3 466"&&(-/ 466"466* FFaKF3r!crt|Dcgc]}|j|jf c}Scc}wr)rangevoidra)rrhis r _emptyLinezTerminalBuffer._emptyLines,EJ5\RD88:;RRRs#4c|xjdz c_|j|jk\rW|xjdzc_|jd=|jj|j |j yy)Nr$r)rQheightrOr0rsrhrUs rrdzTerminalBuffer._scrollDowns[ !  66T[[ FFaKF 1 JJ  doodjj9 : !r!c|xjdzc_|jdkrJd|_|jd=|jjd|j|jyy)Nr$r)rQrOinsertrsrhrUs r _scrollUpzTerminalBuffer._scrollUpsR !  66A:DF 2 JJ  a!< = r!c@td|j|z |_yNr)maxrQrns rcursorUpzTerminalBuffer.cursorUpQ #r!cZt|jdz |j|z|_yNr$)minrurQr}s r cursorDownzTerminalBuffer.cursorDowns T[[1_dffqj1r!c@td|j|z |_yr{)r|rPr}s rcursorBackwardzTerminalBuffer.cursorBackwardrr!cTt|j|j|z|_yr)rrhrPr}s r cursorForwardzTerminalBuffer.cursorForwardsTZZ!,r!c ||_||_yrrPrQ)rcolumnlines rcursorPositionzTerminalBuffer.cursorPositionsr!cp|jj|_|jj|_yr)homerPrQrUs r cursorHomezTerminalBuffer.cursorHomes r!c$|jyr)rdrUs rindexzTerminalBuffer.indexs r!c$|jyr)ryrUs r reverseIndexzTerminalBuffer.reverseIndexs  r!c2d|_|jy)zW Update the cursor position attributes and scroll down if appropriate. rN)rPrdrUs rrizTerminalBuffer.nextLines r!c>|j|jf|_yr)rPrQ _savedCursorrUs r saveCursorzTerminalBuffer.saveCursors!VVTVV,r!c:|j\|_|_|`yr)rrPrQrUs r restoreCursorzTerminalBuffer.restoreCursor s**  r!c0|D]}d|j|<y)NT)rjrrjr.s rsetModeszTerminalBuffer.setModes s !A DJJqM !r!cL|D]} |j|=y#t$rYwxYwr)rjKeyErrorrs r resetModeszTerminalBuffer.resetModess5 A JJqM     ##c0|D]}d|j|<y)aH Enable the given modes. Track which modes have been enabled so that the implementations of other L{insults.ITerminalTransport} methods can be properly implemented to respect these settings. @see: L{resetPrivateModes} @see: L{insults.ITerminalTransport.setPrivateModes} TN) privateModesrs rsetPrivateModeszTerminalBuffer.setPrivateModess# (A#'D  a  (r!cL|D]} |j|=y#t$rYwxYw)z Disable the given modes. @see: L{setPrivateModes} @see: L{insults.ITerminalTransport.resetPrivateModes} N)rrrs rresetPrivateModesz TerminalBuffer.resetPrivateModes&s: A %%a(   rcd|_y)Napp keypadModerUs rapplicationKeypadModez$TerminalBuffer.applicationKeypadMode3 r!cd|_y)NnumrrUs rnumericKeypadModez TerminalBuffer.numericKeypadMode6rr!c"||j|<yr)charsets)rcharSetwhichs rselectCharacterSetz!TerminalBuffer.selectCharacterSet9s& er!c.tj|_yr)rrCr_rUs rshiftInzTerminalBuffer.shiftIn<$ZZr!c.tj|_yr)rG1r_rUs rshiftOutzTerminalBuffer.shiftOut?rr!cjtj_jfd}|_y)Nc(|`_yrrZr_r\foldActiveCharsetrs rrZz3TerminalBuffer.singleShift2..insertAtCursorG aD#!1D r!)r_rG2rZrrZrrs` @@r singleShift2zTerminalBuffer.singleShift2B5--$ZZ    2 -r!cjtj_jfd}|_y)Nc(|`_yrrrs rrZz3TerminalBuffer.singleShift3..insertAtCursorSrr!)r_rG3rZrs` @@r singleShift3zTerminalBuffer.singleShift3Nrr!c|D]K}|tjk(rddddttd|_.|tj k(rd|jd<Q|tj k(rd|jd<t|tjk(rd|jd<|tjk(rd|jd< t|}t|cxkrttzkrnn|tz |jd<t|cxkrttzkrnn|tz |jd </|jjd | Ny#t$r!|jjd | YywxYw) NFrrrrrrTrrrrrrz-Unknown graphic rendition attribute: {attr!r})attr)rNORMALr5r7r`r1r2r3r4intr6N_COLORSr8_logerror ValueError)r attributesar*s rselectGraphicRenditionz%TerminalBuffer.selectGraphicRenditionZsQ! AGNN"!!&"$)"'"' )%gll"04%%f-g'''59%%k2gmm#15%%g.g+++8<%%n5AA "Q?*x*??>?*n--l;#qAJ,AA>?*n--l; KRS(?! *"IIOOGa$s> E&E=<E=ch|j|j|j|j<yr)rsrhrOrQrUs r eraseLinezTerminalBuffer.eraseLine~s!!__TZZ8 466r!c|j|jz }|j||j|j|jdyr)rhrPrsrOrQ)rrhs reraseToLineEndzTerminalBuffer.eraseToLineEnds: TVV#'+u'= 4664668$r!c|j|jdz|j|jd|jdzyr)rsrPrOrQrUs reraseToLineBeginningz#TerminalBuffer.eraseToLineBeginnings4+/??466A:+F 466r?r@rAkeyID execBytesdecodeexecStrexecTAB BACKSPACErhrurobjectrqr rrRrVr]rarZrsrdryrrrrrrrrrirrrrrrrrrrrrrrrrrrrrrrrrrTrrrrrr r rrrrrrDr!rrGrGxs0N* ""7+ W 58 CI E F D 8D 8D #M:S;>$2$--! (   '(( - -"H9>GOX TP&K 4- !KKRQ SVTSSr!rGc eZdZy)ExpectationTimeoutN)r>r?r@rDr!rr#r#sr!r#c:eZdZdZdZdZdZdZdZde fdZ y) ExpectableBufferrc<tj|g|_yr)rGrV _expectingrUs rrVzExpectableBuffer.connectionMades%%d+r!cPtj|||jyr)rGr]_checkExpectedr s rr]zExpectableBuffer.writesT4( r!c<tj|d|_yr{)rGr_markrUs rrzExpectableBuffer.cursorHomes!!$' r!cV|jt|jyr)errbackr#r))rds r_timeoutExpectedz!ExpectableBuffer._timeoutExpected s $&' r!c|j|jd}|jr|jd\}}}|r|js|jd=?|j |D]i}|r|j |jd=|xj|j z c_||j d}|j|ny|jryyr{)rr+r'activefinditercancelendcallback)rsexprtimerdeferredmatchs rr)zExpectableBuffer._checkExpecteds NN TZZ\ *oo$(OOA$6 !D%U\\^OOA&q) LLNOOA& eiik) eiikm$!!%( oor!Nctj}d}|r|j||j|}|jj t j|||f|j|Sr) rDeferred callLaterr/r'r0recompiler))r expressiontimeout schedulerr.r8s rexpectzExpectableBuffer.expect"sc NN  ''1F1FJE  : 6qAB r!) r>r?r@r+rVr]rr/r)rrCrDr!rr%r%s, E$*.r!r%)rErGr%),rAr>rezope.interfacer incrementalrtwisted.conch.insultsrtwisted.internetrrrtwisted.loggerr twisted.pythonr twisted.python.compatr twisted.python.deprecater r r6r8rpr7REDGREENYELLOWBLUEMAGENTACYANr5r_FormattingStateMixinrrEITerminalTransportProtocolrG Exceptionr#r%__all__rDr!rrWs  &)55!*+J   BG(?sE64$xK<<K\& Ir1a C"  W ' '(}SX&&}S)}S@   ,~,^ Gr!