10
10
from typing import Union , Callable
11
11
12
12
from PySide6 .QtCore import QTimer
13
+ from PySide6 .QtGui import QTextCharFormat
13
14
from PySide6 .QtWidgets import QTextEdit
14
15
15
16
from je_editor .pyside_ui .code .running_process_manager import run_instance_manager
@@ -48,8 +49,8 @@ def __init__(
48
49
self .timer : Union [QTimer , None ] = None
49
50
self .still_run_shell : bool = True
50
51
self .process = None
51
- self .run_output_queue : queue = queue .Queue ()
52
- self .run_error_queue : queue = queue .Queue ()
52
+ self .run_output_queue : queue . Queue = queue .Queue ()
53
+ self .run_error_queue : queue . Queue = queue .Queue ()
53
54
self .program_encoding : str = shell_encoding
54
55
self .program_buffer : int = program_buffer
55
56
self .after_done_function = after_done_function
@@ -75,21 +76,24 @@ def later_init(self) -> None:
75
76
else :
76
77
raise JEditorException (je_editor_init_error )
77
78
78
- def exec_shell (self , shell_command : [str , list ]) -> None :
79
+ def exec_shell (self , shell_command : Union [str , list ]) -> None :
79
80
"""
80
81
:param shell_command: shell command will run
81
82
:return: if error return result and True else return result and False
82
83
"""
83
84
jeditor_logger .info (f"ShellManager exec_shell, shell_command: { shell_command } " )
84
85
try :
85
86
self .exit_program ()
86
- self .code_result .setTextColor (actually_color_dict .get ("normal_output_color" ))
87
87
self .code_result .setPlainText ("" )
88
88
if sys .platform in ["win32" , "cygwin" , "msys" ]:
89
89
args = shell_command
90
90
else :
91
91
args = shlex .split (shell_command )
92
- self .code_result .append (str (args ))
92
+ text_cursor = self .code_result .textCursor ()
93
+ text_format = QTextCharFormat ()
94
+ text_format .setForeground (actually_color_dict .get ("normal_output_color" ))
95
+ text_cursor .insertText (str (args ), text_format )
96
+ text_cursor .insertBlock ()
93
97
self .process = subprocess .Popen (
94
98
args = args ,
95
99
stdout = subprocess .PIPE ,
@@ -116,29 +120,36 @@ def exec_shell(self, shell_command: [str, list]) -> None:
116
120
self .timer .timeout .connect (self .pull_text )
117
121
self .timer .start ()
118
122
except Exception as error :
119
- self .code_result .setTextColor (actually_color_dict .get ("error_output_color" ))
120
- self .code_result .append (str (error ))
121
- self .code_result .setTextColor (actually_color_dict .get ("normal_output_color" ))
123
+ text_cursor = self .code_result .textCursor ()
124
+ text_format = QTextCharFormat ()
125
+ text_format .setForeground (actually_color_dict .get ("error_output_color" ))
126
+ text_cursor .insertText (str (error ), text_format )
127
+ text_cursor .insertBlock ()
122
128
if self .process is not None :
123
129
self .process .terminate ()
124
130
125
131
# tkinter_ui update method
126
132
def pull_text (self ) -> None :
127
133
jeditor_logger .info ("ShellManager pull_text" )
128
134
try :
129
- self .code_result .setTextColor (actually_color_dict .get ("normal_output_color" ))
130
135
if not self .run_output_queue .empty ():
131
136
output_message = self .run_output_queue .get_nowait ()
132
137
output_message = str (output_message ).strip ()
133
138
if output_message :
134
- self .code_result .append (output_message )
135
- self .code_result .setTextColor (actually_color_dict .get ("error_output_color" ))
139
+ text_cursor = self .code_result .textCursor ()
140
+ text_format = QTextCharFormat ()
141
+ text_format .setForeground (actually_color_dict .get ("normal_output_color" ))
142
+ text_cursor .insertText (output_message , text_format )
143
+ text_cursor .insertBlock ()
136
144
if not self .run_error_queue .empty ():
137
145
error_message = self .run_error_queue .get_nowait ()
138
146
error_message = str (error_message ).strip ()
139
147
if error_message :
140
- self .code_result .append (error_message )
141
- self .code_result .setTextColor (actually_color_dict .get ("normal_output_color" ))
148
+ text_cursor = self .code_result .textCursor ()
149
+ text_format = QTextCharFormat ()
150
+ text_format .setForeground (actually_color_dict .get ("error_output_color" ))
151
+ text_cursor .insertText (error_message , text_format )
152
+ text_cursor .insertBlock ()
142
153
except queue .Empty :
143
154
pass
144
155
if self .process .returncode == 0 :
@@ -168,7 +179,11 @@ def exit_program(self) -> None:
168
179
self .print_and_clear_queue ()
169
180
if self .process is not None :
170
181
self .process .terminate ()
171
- self .code_result .append (f"Shell command exit with code { self .process .returncode } " )
182
+ text_cursor = self .code_result .textCursor ()
183
+ text_format = QTextCharFormat ()
184
+ text_format .setForeground (actually_color_dict .get ("normal_output_color" ))
185
+ text_cursor .insertText (f"Shell command exit with code { self .process .returncode } " , text_format )
186
+ text_cursor .insertBlock ()
172
187
self .process = None
173
188
174
189
def print_and_clear_queue (self ) -> None :
0 commit comments