|
| 1 | +import sys |
| 2 | +import subprocess |
| 3 | + |
| 4 | +from rich.console import RenderableType |
| 5 | + |
| 6 | +from textual import on |
1 | 7 | from textual.app import ComposeResult
|
| 8 | +from textual.events import ScreenResume |
2 | 9 | from textual.containers import Container
|
3 | 10 | from textual.screen import Screen
|
4 |
| -from textual.widgets import Footer, Header, Static, RichLog |
| 11 | +from textual.widgets import Header, Static, RichLog, ProgressBar, LoadingIndicator, Button |
5 | 12 |
|
6 | 13 | class CompileScreen(Screen):
|
| 14 | + """Compile screen.""" |
| 15 | + |
| 16 | + child_process = None |
| 17 | + |
| 18 | + def print_output(self, renderable: RenderableType) -> None: |
| 19 | + self.query_one(RichLog).write(renderable) |
| 20 | + |
| 21 | + def compile_libs(self) -> None: |
| 22 | + print("Starting compilation process") |
| 23 | + |
| 24 | + arduino_path = "" |
| 25 | + |
| 26 | + if len(sys.argv) > 1: |
| 27 | + #Custom Arduino path |
| 28 | + arduino_path = sys.argv[1] |
| 29 | + else: |
| 30 | + #Default Arduino path |
| 31 | + arduino_path = "/arduino-esp32" |
| 32 | + |
| 33 | + label = self.query_one("#compile-title", Static) |
| 34 | + progress_bar = self.query_one(ProgressBar) |
| 35 | + target_list = [k for k, v in self.app.target_dict.items() if v == True] |
| 36 | + self.child_process = None |
| 37 | + |
| 38 | + if target_list: |
| 39 | + print("Targets selected:") |
| 40 | + print(target_list) |
| 41 | + # At least one target selected |
| 42 | + progress_bar.update(total=len(target_list), progress=0) |
| 43 | + for target in target_list: |
| 44 | + print("Compiling for " + target.upper()) |
| 45 | + label.update("Compiling for " + target.upper()) |
| 46 | + self.print_output("\n======== Compiling for " + target.upper() + " ========\n") |
| 47 | + #self.child_process = subprocess.Popen(["build.sh", "-c", arduino_path, "-t", target], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) |
| 48 | + self.child_process = subprocess.Popen(["./build.sh", "--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 49 | + while True: |
| 50 | + output = self.child_process.stdout.readline() |
| 51 | + if output == '' and self.child_process.poll() is not None: |
| 52 | + break |
| 53 | + if output: |
| 54 | + self.print_output(output.strip()) # Update RichLog widget with subprocess output |
| 55 | + self.child_process.stdout.close() |
| 56 | + if self.child_process.returncode != 0: |
| 57 | + print("Compilation failed for " + target.upper()) |
| 58 | + print("Return code: " + str(self.child_process.returncode)) |
| 59 | + label.update("Compilation failed for " + target.upper()) |
| 60 | + break |
| 61 | + progress_bar.advance(1) |
| 62 | + else: |
| 63 | + # No targets selected |
| 64 | + print("No targets selected") |
| 65 | + label.update("No targets selected") |
| 66 | + progress_bar.update(total=1, progress=1) |
| 67 | + |
| 68 | + def on_button_pressed(self, event: Button.Pressed) -> None: |
| 69 | + """Event handler called when a button is pressed.""" |
| 70 | + if self.child_process: |
| 71 | + print("Terminating child process") |
| 72 | + self.child_process.terminate() |
| 73 | + self.child_process.wait() |
| 74 | + self.dismiss() |
7 | 75 |
|
8 | 76 | def compose(self) -> ComposeResult:
|
9 | 77 | """Compose our UI."""
|
10 | 78 | yield Header()
|
| 79 | + with Container(id="compile-container"): |
| 80 | + yield Static("Compiling for ...", id="compile-title") |
| 81 | + yield ProgressBar(id="compile-progress", show_eta=True) |
| 82 | + yield Button("Back", id="compile-back-button", classes="compile-button") |
11 | 83 | with Container():
|
12 |
| - yield Static("Compile", id="compile-title") |
13 |
| - |
14 |
| - def on_mount(self) -> None: |
15 |
| - pass |
16 |
| - |
| 84 | + yield RichLog(markup=True) |
17 | 85 |
|
| 86 | + @on(ScreenResume) |
| 87 | + def on_resume(self) -> None: |
| 88 | + print("Compile screen resumed") |
| 89 | + log = self.query_one(RichLog) |
| 90 | + log.clear() |
| 91 | + log.focus() |
| 92 | + self.compile_libs() |
0 commit comments