77from pathlib import Path
88from queue import Queue
99from threading import Thread
10+ from typing import Union
1011
1112from PySide6 .QtCore import QTimer
13+ from PySide6 .QtGui import QTextCharFormat
1214from je_editor .pyside_ui .main_ui .save_settings .user_color_setting_file import actually_color_dict
1315from je_editor .utils .venv_check .check_venv import check_and_choose_venv
1416
@@ -27,15 +29,15 @@ def __init__(
2729 super ().__init__ ()
2830 self .compiler_path = None
2931 # ite_instance param
30- self .read_program_error_output_from_thread : [threading .Thread , None ] = None
31- self .read_program_output_from_thread : [threading .Thread , None ] = None
32+ self .read_program_error_output_from_thread : Union [threading .Thread , None ] = None
33+ self .read_program_output_from_thread : Union [threading .Thread , None ] = None
3234 self .main_window : CodeWindow = main_window
3335 self .timer : QTimer = QTimer (self .main_window )
3436 self .still_run_program : bool = True
3537 self .program_encoding : str = program_encoding
3638 self .run_output_queue : Queue = Queue ()
3739 self .run_error_queue : Queue = Queue ()
38- self .process : [subprocess .Popen , None ] = None
40+ self .process : Union [subprocess .Popen , None ] = None
3941
4042 self .task_done_trigger_function : typing .Callable = task_done_trigger_function
4143 self .error_trigger_function : typing .Callable = error_trigger_function
@@ -98,19 +100,24 @@ def start_test_process(self, package: str, exec_str: str):
98100 # Pyside UI update method
99101 def pull_text (self ):
100102 try :
101- self .main_window .code_result .setTextColor (actually_color_dict .get ("normal_output_color" ))
102103 if not self .run_output_queue .empty ():
103104 output_message = self .run_output_queue .get_nowait ()
104105 output_message = str (output_message ).strip ()
105106 if output_message :
106- self .main_window .code_result .append (output_message )
107- self .main_window .code_result .setTextColor (actually_color_dict .get ("error_output_color" ))
107+ text_cursor = self .main_window .code_result .textCursor ()
108+ text_format = QTextCharFormat ()
109+ text_format .setForeground (actually_color_dict .get ("normal_output_color" ))
110+ text_cursor .insertText (output_message , text_format )
111+ text_cursor .insertBlock ()
108112 if not self .run_error_queue .empty ():
109113 error_message = self .run_error_queue .get_nowait ()
110114 error_message = str (error_message ).strip ()
111115 if error_message :
112- self .main_window .code_result .append (error_message )
113- self .main_window .code_result .setTextColor (actually_color_dict .get ("normal_output_color" ))
116+ text_cursor = self .main_window .code_result .textCursor ()
117+ text_format = QTextCharFormat ()
118+ text_format .setForeground (actually_color_dict .get ("error_output_color" ))
119+ text_cursor .insertText (error_message , text_format )
120+ text_cursor .insertBlock ()
114121 except queue .Empty :
115122 pass
116123 if self .process is not None :
@@ -139,7 +146,11 @@ def exit_program(self):
139146 self .print_and_clear_queue ()
140147 if self .process is not None :
141148 self .process .terminate ()
142- self .main_window .code_result .append (f"Task exit with code { self .process .returncode } " )
149+ text_cursor = self .main_window .code_result .textCursor ()
150+ text_format = QTextCharFormat ()
151+ text_format .setForeground (actually_color_dict .get ("normal_output_color" ))
152+ text_cursor .insertText (f"Task exit with code { self .process .returncode } " , text_format )
153+ text_cursor .insertBlock ()
143154 self .process = None
144155
145156 def print_and_clear_queue (self ):
@@ -149,15 +160,17 @@ def print_and_clear_queue(self):
149160 def read_program_output_from_process (self ):
150161 while self .still_run_program :
151162 self .process : subprocess .Popen
152- program_output_data = self .process .stdout .read (self .program_buffer_size )
163+ program_output_data = self .process .stdout .readline (self .program_buffer_size )\
164+ .decode ("utf-8" , "replace" )
153165 if self .process :
154166 self .process .stdout .flush ()
155167 if program_output_data .strip () != "" :
156168 self .run_output_queue .put (program_output_data )
157169
158170 def read_program_error_output_from_process (self ):
159171 while self .still_run_program :
160- program_error_output_data = self .process .stderr .read (self .program_buffer_size )
172+ program_error_output_data = self .process .stderr .readline (self .program_buffer_size )\
173+ .decode ("utf-8" , "replace" )
161174 if self .process :
162175 self .process .stderr .flush ()
163176 if program_error_output_data .strip () != "" :
0 commit comments