o b? @sdZddlZddlZddlmZddlmZddlmZddl m Z m Z m Z ddl mZddlmZdd lmZdd lmZmZd Zd Zed \ ZZZZZZZZ Z!Gdddej"Z#e#Z$eedddddddeej%Gddde j&Z'Gddde(Z)Gddde'Z*gdZ+dS)z< Partial in-memory terminal emulator @author: Jp Calderone N) implementer)Version)insults)deferprotocolreactor)Logger)_textattributes) iterbytes) deprecateddeprecatedModuleAttribute( c@sReZdZdZdZejddddeedfddZ e e dddd d d Z d d Z dS)_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 Cs4||_||_||_||_||_||_||_||_dSNr) selfrrrrrrrrr>/usr/lib/python3/dist-packages/twisted/conch/insults/helper.py__init__3s  z_FormattingState.__init__Twisted rcKs|\}}|||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)rkwkvrrrwantOneGs z_FormattingState.wantOnecCsg}|jr |d|jr|tj|jr|tj|jr%|tj|j r.|tj |j t kr;|t |j |jtkrH|t|j|rVddtt|dSdS)Nrz[;m)rappendrrBOLDr UNDERLINErBLINKr REVERSE_VIDEOrWHITE FOREGROUNDrBLACK BACKGROUNDjoinmapstr)rattrsrrrtoVT102Vs$       z_FormattingState.toVT102N)__name__ __module__ __qualname____doc__compareAttributesrG0r0r2rr rr'r8rrrrrs    rrr r!z=Use twisted.conch.insults.text.assembleFormattedText instead.ztwisted.conch.insults.helperCharacterAttributec@seZdZdZdD]ZedZedZeeqdZ dZ dZ dZ d Z eZeZd d Zd d ZddZddZddZddZddZddZdyddZdyddZdydd Zdyd!d"Zd#d$Zd%d&Zd'd(Z d)d*Z!d+d,Z"d-d.Z#d/d0Z$d1d2Z%d3d4Z&d5d6Z'd7d8Z(d9d:Z)d;d<Z*d=d>Z+d?d@Z,dAdBZ-dCdDZ.dEdFZ/dGdHZ0dIdJZ1dKdLZ2dMdNZ3dOdPZ4dQdRZ5dSdTZ6dydUdVZ7dydWdXZ8dydYdZZ9d[d\Z:d]d^Z;d_d`ZdedfZ?dgdhZ@didjZAdkdlZBdmdnZCdodpZDdzdrdsZEdtduZFdvdwZGdxS){TerminalBufferz) An in-memory terminal emulator. )sUP_ARROWs DOWN_ARROWs RIGHT_ARROWs LEFT_ARROWsHOMEsINSERTsDELETEsENDsPGUPsPGDNsF1sF2sF3sF4sF5sF6sF7sF8sF9sF10sF11sF12s = object()ascii P cCs|j||Sr)lines)rxyrrr getCharacterzTerminalBuffer.getCharactercC |dSr)resetrrrrconnectionMade zTerminalBuffer.connectionMadecCs$t|ddD]}||qdS)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)rdatabrrrwrites zTerminalBuffer.writecCst|jfi|jSr)r activeCharsetgraphicRenditionrNrrr_currentFormattingStatesz&TerminalBuffer._currentFormattingStatecCs|dkr d|_dS|dkr|dS|tjdvr[|j|jkr%|||f}|j t jj rI|g|j |j |j|j<|j |j n ||j |j |j<|jd7_dSdS)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.  rrQrAr!N)rH _scrollDownstring printableencodewidthnextLinerYmodesgetrIRMrGrIpop)rrUchrrrrSs    zTerminalBuffer.insertAtCursorcsfddt|DS)Ncsg|] }jfqSr)voidrY.0irNrr sz-TerminalBuffer._emptyLine..)rangerr_rrNr _emptyLinezTerminalBuffer._emptyLinecCsL|jd7_|j|jkr$|jd8_|jd=|j||jdSdS)Nr!r)rIheightrGr+rmr_rNrrrr[s  zTerminalBuffer._scrollDowncCsD|jd8_|jdkr d|_|jd=|jd||jdSdS)Nr!r)rIrGinsertrmr_rNrrr _scrollUps  zTerminalBuffer._scrollUpr!cCtd|j||_dSNr)maxrIrnrrrcursorUprnzTerminalBuffer.cursorUpcCst|jd|j||_dSNr!)minrorIrvrrr cursorDownszTerminalBuffer.cursorDowncCrsrt)rurHrvrrrcursorBackwardrnzTerminalBuffer.cursorBackwardcCst|j|j||_dSr)rzr_rHrvrrr cursorForwardzTerminalBuffer.cursorForwardcCs||_||_dSrrHrI)rcolumnlinerrrcursorPositions zTerminalBuffer.cursorPositioncCs|jj|_|jj|_dSr)homerHrIrNrrr cursorHomes zTerminalBuffer.cursorHomecCrLr)r[rNrrrindexrPzTerminalBuffer.indexcCrLr)rrrNrrr reverseIndexrPzTerminalBuffer.reverseIndexcCsd|_|dS)zW Update the cursor position attributes and scroll down if appropriate. rN)rHr[rNrrrr`s zTerminalBuffer.nextLinecCs|j|jf|_dSr)rHrI _savedCursorrNrrr saveCursorzTerminalBuffer.saveCursorcCs|j\|_|_|`dSr)rrHrIrNrrr restoreCursor szTerminalBuffer.restoreCursorcCs|D]}d|j|<qdS)NT)rarrar)rrrsetModes s zTerminalBuffer.setModesc Cs,|D]}z|j|=WqtyYqwdSr)raKeyErrorrrrr resetModess  zTerminalBuffer.resetModescCs|D]}d|j|<qdS)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) privateModesrrrrsetPrivateModess zTerminalBuffer.setPrivateModesc Cs,|D]}z|j|=WqtyYqwdS)z Disable the given modes. @see: L{setPrivateModes} @see: L{insults.ITerminalTransport.resetPrivateModes} N)rrrrrrresetPrivateModes&s  z TerminalBuffer.resetPrivateModescC d|_dS)Napp keypadModerNrrrapplicationKeypadMode3 z$TerminalBuffer.applicationKeypadModecCr)NnumrrNrrrnumericKeypadMode6rz TerminalBuffer.numericKeypadModecCs||j|<dSr)charsets)rcharSetwhichrrrselectCharacterSet9rKz!TerminalBuffer.selectCharacterSetcC tj|_dSr)rr>rWrNrrrshiftIn<rPzTerminalBuffer.shiftIncCrr)rG1rWrNrrrshiftOut?rPzTerminalBuffer.shiftOutc.jtj_jfdd}|_dS)Nc|`_dSrrSrWrUfoldActiveCharsetrrrrSG z3TerminalBuffer.singleShift2..insertAtCursor)rWrG2rSrrSrrr singleShift2B  zTerminalBuffer.singleShift2cr)NcrrrrrrrrSSrz3TerminalBuffer.singleShift3..insertAtCursor)rWrG3rSrrrr singleShift3NrzTerminalBuffer.singleShift3c Gs|D]}|tjkrddddttd|_q|tjkrd|jd<q|tjkr*d|jd<q|tjkr5d|jd<q|tjkr@d|jd<qzt |}Wnt yW|j j d|d Yqwt |krdt tkrnnn|t |jd <qt|krzttkrnn|t|jd <q|j j d|d qdS) NFrrrrrrTrrrrz-Unknown graphic rendition attribute: {attr!r})attrrr)rNORMALr0r2rXr,r-r.r/int ValueError_logerrorr1N_COLORSr3)r attributesar&rrrselectGraphicRenditionZs@            z%TerminalBuffer.selectGraphicRenditioncCs||j|j|j<dSr)rmr_rGrIrNrrr eraseLine~r~zTerminalBuffer.eraseLinecCs,|j|j}|||j|j|jd<dSr)r_rHrmrGrIrlrrreraseToLineEnds  zTerminalBuffer.eraseToLineEndcCs*||jd|j|jd|jd<dSry)rmrHrGrIrNrrreraseToLineBeginnings*z#TerminalBuffer.eraseToLineBeginningcsfddtjD_dS)Ncg|]}jqSrrmr_rgrNrrrjz/TerminalBuffer.eraseDisplay..)rkrorGrNrrNr eraseDisplayszTerminalBuffer.eraseDisplaycsBjjd}fddt|Djjdd<dS)Nr!crrrrgrNrrrjrz4TerminalBuffer.eraseToDisplayEnd..)rrorIrkrG)rrorrNreraseToDisplayEnds*z TerminalBuffer.eraseToDisplayEndcs0fddtjDjdj<dS)NcrrrrgrNrrrjrz:TerminalBuffer.eraseToDisplayBeginning..)rrkrIrGrNrrNreraseToDisplayBeginnings(z&TerminalBuffer.eraseToDisplayBeginningcCsD|j|j|j|j|=|j|j|t|j|j|dSr)rGrIrHextendrmrzr_rvrrrdeleteCharacters*zTerminalBuffer.deleteCharactercs6fddt|Djjj<jjd=dS)NcrrrrgrNrrrjrz-TerminalBuffer.insertLine..)rkrGrIrorvrrNr insertLines$zTerminalBuffer.insertLinecs6jjj|=jfddt|DdS)NcrrrrgrNrrrjrz-TerminalBuffer.deleteLine..)rGrIrrkrvrrNr deleteLines"zTerminalBuffer.deleteLinecCs |j|jfSrrrNrrrreportCursorPositionrPz#TerminalBuffer.reportCursorPositioncCstdd|_d|_|_i|_i|_|tjjtjj gd|_ tj |_ ddddt td|_tj tjtjtjtjtjtjtji|_|dS)NrrFr)rVectorrrHrIrarr AUTO_WRAP CURSOR_MODE numericKeypadr>rWr0r2rXCS_USrr CS_ALTERNATErCS_ALTERNATE_SPECIALrrrNrrrrMs,   zTerminalBuffer.resetcCstdt|dS)NzCould not handle)printrepr)rbufrrrunhandledControlSequencerz'TerminalBuffer.unhandledControlSequencecCsrg}|jD].}g}d}|D]\}}||jur ||t|}q ||jq |d|d|qd|S)NrrQ)rGrfr+lenfillr4)rrGLrlengthrerrrr __bytes__s      zTerminalBuffer.__bytes__cCtd)Nz%Unimplemented: TerminalBuffer.getHostNotImplementedErrorrNrrrgetHostzTerminalBuffer.getHostcCr)Nz%Unimplemented: TerminalBuffer.getPeerrrNrrrgetPeerrzTerminalBuffer.getPeercCr)Nz,Unimplemented: TerminalBuffer.loseConnectionrrNrrrloseConnectionrzTerminalBuffer.loseConnectioncCr)Nz+Unimplemented: TerminalBuffer.writeSequencerrrTrrr writeSequencerzTerminalBuffer.writeSequencecCr)Nz5Unimplemented: TerminalBuffer.horizontalTabulationSetrrNrrrhorizontalTabulationSetsz&TerminalBuffer.horizontalTabulationSetcCr)Nz-Unimplemented: TerminalBuffer.tabulationClearrrNrrrtabulationClearrzTerminalBuffer.tabulationClearcCr)Nz0Unimplemented: TerminalBuffer.tabulationClearAllrrNrrrtabulationClearAllrz!TerminalBuffer.tabulationClearAllTcCr)Nz.Unimplemented: TerminalBuffer.doubleHeightLiner)rtoprrrdoubleHeightLinerzTerminalBuffer.doubleHeightLinecCr)Nz-Unimplemented: TerminalBuffer.singleWidthLinerrNrrrsingleWidthLinerzTerminalBuffer.singleWidthLinecCr)Nz-Unimplemented: TerminalBuffer.doubleWidthLinerrNrrrdoubleWidthLinerzTerminalBuffer.doubleWidthLineN)r!)T)Hr9r:r;r<keyID execBytesdecodeexecStrexecTAB BACKSPACEr_rorobjectrfrrrJrOrVrYrSrmr[rrrxr{r|r}rrrrr`rrrrrrrrrrrrrrrrrrrrrrrrrMrrrrrrrrrrrrrrrrr@xs          $     r@c@s eZdZdS)ExpectationTimeoutN)r9r:r;rrrrrsrc@sFeZdZdZddZddZddZdd Zd d Zd e fd dZ d S)ExpectableBufferrcCst|g|_dSr)r@rO _expectingrNrrrrO  zExpectableBuffer.connectionMadecCst|||dSr)r@rV_checkExpectedrrrrrV  zExpectableBuffer.writecCst|d|_dSrt)r@r_markrNrrrrrzExpectableBuffer.cursorHomecCs|t|dSr)errbackrr)rdrrr_timeoutExpected rz!ExpectableBuffer._timeoutExpectedcCs||jd}|jrO|jd\}}}|r|s|jd=q ||D]#}|r,||jd=|j|7_||d}||ndS|js dSdSrt)rrractivefinditercancelendcallback)rsexprtimerdeferredmatchrrrrs   zExpectableBuffer._checkExpectedNcCsDt}d}|r|||j|}|jt|||f||Sr) rDeferred callLaterrrr+recompiler)r expressiontimeout schedulerrrrrrexpect"szExpectableBuffer.expect) r9r:r;rrOrVrrrrr rrrrrsr)r?r@r),r<rr\zope.interfacer incrementalrtwisted.conch.insultsrtwisted.internetrrrtwisted.loggerrtwisted.pythonr twisted.python.compatr twisted.python.deprecater r r1r3rkr2REDGREENYELLOWBLUEMAGENTACYANr0r_FormattingStateMixinrr?ITerminalTransportProtocolr@ Exceptionrr__all__rrrrs<      N   /