)L i%dZddlmZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z m Z ddlmZmZddlmZd d gZe ddd ZGd d ZGdd Zy)a patch_stdout ============ This implements a context manager that ensures that print statements within it won't destroy the user interface. The context manager will replace `sys.stdout` by something that draws the output above the current prompt, rather than overwriting the UI. Usage:: with patch_stdout(application): ... application.run() ... Multiple applications can run in the body of the context manager, one after the other. ) annotationsN)contextmanager) GeneratorTextIOcast)get_app_sessionrun_in_terminal)Output patch_stdout StdoutProxyc#`Kt|5}tj}tj}t t |t_t t |t_ d|t_|t_ dddy#|t_|t_wxYw#1swYyxYww)a Replace `sys.stdout` by an :class:`_StdoutProxy` instance. Writing to this proxy will make sure that the text appears above the prompt, and that it doesn't destroy the output from the renderer. If no application is curring, the behavior should be identical to writing to `sys.stdout` directly. Warning: If a new event loop is installed using `asyncio.set_event_loop()`, then make sure that the context manager is applied after the event loop is changed. Printing to stdout will be scheduled in the event loop that's active when the context manager is created. :param raw: (`bool`) When True, vt100 terminal escape sequences are not removed/escaped. )rawN)r sysstdoutstderrrr)rproxyoriginal_stdoutoriginal_stderrs a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/patch_stdout.pyr r (s$   )****&%( &%(  ) (CJ(CJ ) ))CJ(CJ ) )s5 B.AB"#B'B"> B.BB""B+'B.ceZdZdZy)_Donez-Sentinel value for stopping the stdout proxy.N)__name__ __module__ __qualname____doc__rrrIs3rrceZdZdZ d ddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zdd ZeddZddZd dZed!dZed!dZy)"r a File-like object, which prints everything written to it, output above the current application/prompt. This class is compatible with other file objects and can be used as a drop-in replacement for `sys.stdout` or can for instance be passed to `logging.StreamHandler`. The current application, above which we print, is determined by looking what application currently runs in the `AppSession` that is active during the creation of this instance. This class can be used as a context manager. In order to avoid having to repaint the prompt continuously for every little write, a short delay of `sleep_between_writes` seconds will be added between writes in order to bundle many smaller writes in a short timespan. c||_||_tj|_g|_t |_|jj|_ tj|_ |j|_d|_yNF)sleep_between_writesr threadingRLock_lock_bufferr app_sessionoutput_outputqueueQueue _flush_queue_start_write_thread _flush_threadclosed)selfr"rs r__init__zStdoutProxy.__init___sr %9!__& "$ +, $//66 7.write_and_flushsP LL ( ( *xx &&t, ""4( LL   rc tdy)NF) in_executor)r )r^srwrite_and_flush_in_loopz=StdoutProxy._write_and_flush..write_and_flush_in_loops O ?rNreturnNone)call_soon_threadsafe)r0rVrRrar^s` ` @rrMzStdoutProxy._write_and_flushs, ! @ <    % %&= >rcd|vr[|jdd\}}|j|dgz}|g|_dj|}|jj |y|jj |y)a Note: print()-statements cause to multiple write calls. (write('line') and write(' ')). Of course we don't want to call `run_in_terminal` for every individual call, because that's too expensive, and as long as the newline hasn't been written, the text itself is again overwritten by the rendering of the input command line. Therefor, we have a little buffer which holds the text until a newline is written to stdout.  rrFN)rsplitr&r<r,r;rI)r0databeforeafterto_writerRs r_writezStdoutProxy._writesr 4<!KKa0MFE||vtn4H!7DL778$D    ! !$ ' LL   %rc~dj|j}g|_|jj|y)NrF)r<r&r,r;r]s r_flushzStdoutProxy._flushs/wwt||$  d#rc|j5|j|dddt|S#1swYt|SxYwr3)r%rmlen)r0ris rr[zStdoutProxy.writes= ZZ  KK  4y 4ys 2Acf|j5|jdddy#1swYyxYw)z( Flush buffered output. N)r%ror4s rr\zStdoutProxy.flush s)ZZ  KKM   s'0cR|jjxstjSr3)r)rr __stdout__r4s rrzStdoutProxy.original_stdouts||""4cnn4rc6|jjSr3)r)filenor4s rrvzStdoutProxy.filenos||""$$rcT|jj}|y|jSr!)r)risatty)r0rs rrxzStdoutProxy.isattys%$$ >}}rc6|jjSr3)r)encodingr4s rrzzStdoutProxy.encoding#s||$$&&rcy)Nstrictrr4s rerrorszStdoutProxy.errors'srN)g?F)r"floatrboolrcrd)rcr )r8objectrcrdrb)rczthreading.Thread)rc asyncio.AbstractEventLoop | None)rVrrRstrrcrd)rirrcrd)rirrcint)rcz TextIO | None)rcr)rcr)rcr)rrrrr1r5r9r7r-rBrLrMrmror[r\propertyrrvrxrzr}rrrr r Ms&'*#  6 #6J "?4"?rsg(#  %**9  ))@44\\r