1919from pathlib import Path
2020
2121from flet import (
22+ UserControl ,
2223 Column ,
2324 ElevatedButton ,
2425 Row ,
@@ -78,32 +79,20 @@ def check_advanced_switch(e):
7879 if self .advanced_switch .value :
7980 logger .info ("Enable advanced output." )
8081 self .state .advanced = True
81- self .terminal_box .visible = True
82- # add terminal box if enabled
83- self .right_view .update ()
82+ self .terminal_box .toggle_visibility ()
8483 else :
8584 logger .info ("Disable advanced output." )
8685 self .state .advanced = False
87- self .terminal_box .visible = False
88- self .right_view .update ()
86+ self .terminal_box .toggle_visibility ()
8987
9088 self .advanced_switch = Switch (
9189 label = "Advanced output" ,
9290 on_change = check_advanced_switch ,
9391 disabled = False ,
9492 )
9593 # text box for terminal output
96- self .terminal_box = Container (
97- content = Column (scroll = "auto" , expand = True ),
98- margin = 10 ,
99- padding = 10 ,
100- alignment = alignment .top_left ,
101- bgcolor = colors .BLACK38 ,
102- height = 300 ,
103- border_radius = 2 ,
104- expand = True ,
105- visible = False
106- )
94+ self .terminal_box = TerminalBox (expand = True )
95+
10796 # main controls
10897 self .right_view .controls = [
10998 get_title (f"{ self .step .title } " ),
@@ -184,7 +173,7 @@ def call_to_phone(self, e, command: str):
184173 self .call_button .disabled = True
185174 # reset terminal output
186175 if self .state .advanced :
187- self .terminal_box .content . controls = []
176+ self .terminal_box .clear ()
188177 # display a progress bar to show something is happening
189178 progress_bar = Row (
190179 [ProgressBar (width = 600 , color = "#00d886" , bgcolor = "#eeeeee" , bar_height = 16 )],
@@ -205,71 +194,40 @@ def call_to_phone(self, e, command: str):
205194 # run the right command
206195 if command in cmd_mapping .keys ():
207196 for line in cmd_mapping .get (command )(bin_path = self .state .bin_path ):
208- if (type (line ) == str ) and line .strip ():
209- self .terminal_box .content .controls .append (
210- Text (f">{ line .strip ()} " , selectable = True )
211- )
212- self .terminal_box .update ()
213- success = line
197+ self .terminal_box .write_line (line )
214198 elif command == "adb_sideload" :
215199 for line in adb_sideload (
216200 bin_path = self .state .bin_path , target = self .state .image_path
217201 ):
218- if (type (line ) == str ) and line .strip ():
219- self .terminal_box .content .controls .append (
220- Text (f">{ line .strip ()} " , selectable = True )
221- )
222- self .terminal_box .update ()
223- success = line
202+ self .terminal_box .write_line (line )
224203 elif command == "adb_twrp_wipe_and_install" :
225204 for line in adb_twrp_wipe_and_install (
226205 bin_path = self .state .bin_path ,
227206 target = self .state .image_path ,
228- config_path = self .state .config_path .joinpath (
229- Path (f"{ self .state .config .metadata .get ('devicecode' )} .yaml" )
230- ),
207+ config_path = self .state .config_path ,
231208 ):
232- if (type (line ) == str ) and line .strip ():
233- self .terminal_box .content .controls .append (
234- Text (f">{ line .strip ()} " , selectable = True )
235- )
236- self .terminal_box .update ()
237- success = line
209+ self .terminal_box .write_line (line )
238210 elif command == "fastboot_flash_recovery" :
239211 for line in fastboot_flash_recovery (
240212 bin_path = self .state .bin_path , recovery = self .state .recovery_path
241213 ):
242- if (type (line ) == str ) and line .strip ():
243- self .terminal_box .content .controls .append (
244- Text (f">{ line .strip ()} " , selectable = True )
245- )
246- self .terminal_box .update ()
247- success = line
214+ self .terminal_box .write_line (line )
248215 elif command == "fastboot_unlock_with_code" :
249216 for line in fastboot_unlock_with_code (
250217 bin_path = self .state .bin_path , unlock_code = self .inputtext .value
251218 ):
252- if (type (line ) == str ) and line .strip ():
253- self .terminal_box .content .controls .append (
254- Text (f">{ line .strip ()} " , selectable = True )
255- )
256- self .terminal_box .update ()
257- success = line
219+ self .terminal_box .write_line (line )
258220 elif command == "heimdall_flash_recovery" :
259221 for line in heimdall_flash_recovery (
260222 bin_path = self .state .bin_path , recovery = self .state .recovery_path
261223 ):
262- if (type (line ) == str ) and line .strip ():
263- self .terminal_box .content .controls .append (
264- Text (f">{ line .strip ()} " , selectable = True )
265- )
266- self .terminal_box .update ()
267- success = line
224+ self .terminal_box .write_line (line )
268225 else :
269226 msg = f"Unknown command type: { command } . Stopping."
270227 logger .error (msg )
271228 self .error_text .value = msg
272229 raise Exception (msg )
230+ success = line # the last element of the iterable is a boolean encoding success/failure
273231
274232 # update the view accordingly
275233 if not success :
@@ -288,3 +246,45 @@ def call_to_phone(self, e, command: str):
288246 self .confirm_button .disabled = False
289247 self .call_button .disabled = True
290248 self .view .update ()
249+
250+
251+ class TerminalBox (UserControl ):
252+
253+ def __init__ (self , expand : bool = True ):
254+ super ().__init__ (expand = expand )
255+
256+ def build (self ):
257+ self .box = Container (
258+ content = Column (scroll = "auto" , expand = True ),
259+ margin = 10 ,
260+ padding = 10 ,
261+ alignment = alignment .top_left ,
262+ bgcolor = colors .BLACK38 ,
263+ height = 300 ,
264+ border_radius = 2 ,
265+ expand = True ,
266+ visible = False
267+ )
268+ return self .box
269+
270+ def write_line (self , line : str ):
271+ """
272+ Write the line to the window box and update.
273+
274+ Ignores empty lines.
275+ """
276+ if (type (line ) == str ) and line .strip ():
277+ self .box .content .controls .append (
278+ Text (f">{ line .strip ()} " , selectable = True )
279+ )
280+ self .box .update ()
281+
282+ def toggle_visibility (self ):
283+ """Toogle the visibility of the terminal box."""
284+ self .box .visible = not self .box .visible
285+ self .box .update ()
286+
287+ def clear (self ):
288+ """Clear terminal output."""
289+ self .box .content .controls = []
290+ self .box .update ()
0 commit comments