-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththread_output_ctrl.py
47 lines (37 loc) · 1.24 KB
/
thread_output_ctrl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import threading
import wx
from styled_text_ctrl import StyledTextCtrl
class ThreadOutputCtrl(StyledTextCtrl):
def __init__(self, parent, env, auto_scroll=False):
StyledTextCtrl.__init__(self, parent, env)
self.auto_scroll = auto_scroll
self.__lock = threading.Lock()
self.__queue = []
self.__timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.__OnTimer, self.__timer)
def __OnTimer(self, evt):
self.flush()
def flush(self):
with self.__lock:
queue, self.__queue = self.__queue, []
lines = "".join(queue)
if lines:
with self.ModifyReadOnly():
self.AppendText(lines)
self.EmptyUndoBuffer()
if self.auto_scroll:
self.ScrollToLine(self.GetLineCount() - 1)
def start(self, interval=100):
self.SetReadOnly(True)
self.__timer.Start(interval)
def stop(self):
self.__timer.Stop()
self.flush()
self.SetReadOnly(False)
def write(self, s):
with self.__lock:
self.__queue.append(s)
def ClearAll(self):
with self.ModifyReadOnly():
StyledTextCtrl.ClearAll(self)
self.EmptyUndoBuffer()