pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
Documentation
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! Background worker for executing commands via PTY.

use tokio::sync::mpsc;

use crate::install::{ExecutorOutput, ExecutorRequest};
#[cfg(not(target_os = "windows"))]
use crate::install::{
    build_downgrade_command_for_executor, build_install_command_for_executor,
    build_remove_command_for_executor, build_scan_command_for_executor,
    build_update_command_for_executor,
};

/// What: Handle install request by building command and executing via PTY.
///
/// Inputs:
/// - `items`: Packages to install
/// - `password`: Optional sudo password
/// - `dry_run`: Whether to run in dry-run mode
/// - `res_tx`: Channel sender for output
///
/// Details:
/// - AUR helpers (paru/yay) need sudo for the final installation step
/// - Detects AUR commands and caches sudo credentials when password is provided
/// - Uses the same flow as custom commands for sudo passthrough
#[cfg(not(target_os = "windows"))]
#[allow(clippy::needless_pass_by_value)] // Values are moved into spawn_blocking closure
fn handle_install_request(
    items: Vec<crate::state::PackageItem>,
    password: Option<crate::state::SecureString>,
    dry_run: bool,
    res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    use crate::state::Source;

    tracing::info!(
        "[Runtime] Executor worker received install request: {} items, dry_run={}",
        items.len(),
        dry_run
    );

    // Check if there are AUR packages
    let has_aur = items.iter().any(|item| matches!(item.source, Source::Aur));

    // For official packages: password is piped to sudo (printf '%s\n' password | sudo -S command)
    // For AUR packages: cache sudo credentials first, then run paru/yay (same sudo prompt flow)
    let cmd = match if has_aur {
        // Build AUR command without password embedded
        build_install_command_for_executor(&items, None, dry_run)
    } else {
        // Build official command with password piping
        build_install_command_for_executor(&items, password.as_deref(), dry_run)
    } {
        Ok(c) => c,
        Err(err) => {
            let _ = res_tx.send(ExecutorOutput::Error(err));
            return;
        }
    };

    // For AUR packages, warm up privilege credentials so paru/yay don't re-prompt.
    // Uses the privilege abstraction: sudo gets `sudo -S -v`, doas skips (unsupported).
    // `;` ensures AUR command runs even if warmup returns non-zero.
    let final_cmd = if has_aur && !dry_run && password.is_some() {
        if let Some(ref pass) = password {
            match crate::logic::privilege::active_tool() {
                Ok(tool) => {
                    if let Some(warmup) =
                        crate::logic::privilege::build_credential_warmup(tool, pass)
                    {
                        format!("{warmup} ; {cmd}")
                    } else {
                        cmd
                    }
                }
                Err(err) => {
                    let _ = res_tx.send(ExecutorOutput::Error(err));
                    return;
                }
            }
        } else {
            cmd
        }
    } else {
        cmd
    };

    let res_tx_clone = res_tx;
    tokio::task::spawn_blocking(move || {
        execute_command_pty(&final_cmd, None, res_tx_clone);
    });
}

/// What: Handle remove request by building command and executing via PTY.
///
/// Inputs:
/// - `names`: Package names to remove
/// - `password`: Optional sudo password
/// - `cascade`: Cascade removal mode
/// - `dry_run`: Whether to run in dry-run mode
/// - `res_tx`: Channel sender for output
#[cfg(not(target_os = "windows"))]
#[allow(clippy::needless_pass_by_value)] // Values are moved into spawn_blocking closure
fn handle_remove_request(
    names: Vec<String>,
    password: Option<crate::state::SecureString>,
    cascade: crate::state::modal::CascadeMode,
    dry_run: bool,
    res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    tracing::info!(
        "[Runtime] Executor worker received remove request: {} packages, dry_run={}",
        names.len(),
        dry_run
    );
    let cmd = match build_remove_command_for_executor(&names, password.as_deref(), cascade, dry_run)
    {
        Ok(c) => c,
        Err(err) => {
            let _ = res_tx.send(ExecutorOutput::Error(err));
            return;
        }
    };
    let res_tx_clone = res_tx;
    tokio::task::spawn_blocking(move || {
        execute_command_pty(&cmd, None, res_tx_clone);
    });
}

/// What: Handle downgrade request by building command and executing via PTY.
///
/// Inputs:
/// - `names`: Package names to downgrade
/// - `password`: Optional sudo password
/// - `dry_run`: Whether to run in dry-run mode
/// - `res_tx`: Channel sender for output
#[cfg(not(target_os = "windows"))]
#[allow(clippy::needless_pass_by_value)] // Values are moved into spawn_blocking closure
fn handle_downgrade_request(
    names: Vec<String>,
    password: Option<crate::state::SecureString>,
    dry_run: bool,
    res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    tracing::info!(
        "[Runtime] Executor worker received downgrade request: {} packages, dry_run={}",
        names.len(),
        dry_run
    );
    let cmd = match build_downgrade_command_for_executor(&names, password.as_deref(), dry_run) {
        Ok(c) => c,
        Err(err) => {
            let _ = res_tx.send(ExecutorOutput::Error(err));
            return;
        }
    };
    let res_tx_clone = res_tx;
    tokio::task::spawn_blocking(move || {
        execute_command_pty(&cmd, None, res_tx_clone);
    });
}

/// What: Handle update request by building command and executing via PTY.
///
/// Inputs:
/// - `commands`: Commands to execute
/// - `password`: Optional sudo password
/// - `dry_run`: Whether to run in dry-run mode
/// - `res_tx`: Channel sender for output
#[cfg(not(target_os = "windows"))]
#[allow(clippy::needless_pass_by_value)] // Values are moved into spawn_blocking closure
fn handle_update_request(
    commands: Vec<String>,
    password: Option<crate::state::SecureString>,
    dry_run: bool,
    res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    tracing::info!(
        "[Runtime] Executor worker received update request: {} commands, dry_run={}",
        commands.len(),
        dry_run
    );
    let cmd = match build_update_command_for_executor(&commands, password.as_deref(), dry_run) {
        Ok(c) => c,
        Err(err) => {
            let _ = res_tx.send(ExecutorOutput::Error(err));
            return;
        }
    };
    tracing::debug!("[Runtime] Built update command (length={})", cmd.len());
    let res_tx_clone = res_tx;
    tokio::task::spawn_blocking(move || {
        tracing::debug!("[Runtime] spawn_blocking started for update command");
        execute_command_pty(&cmd, None, res_tx_clone);
        tracing::debug!("[Runtime] spawn_blocking completed for update command");
    });
}

/// What: Handle scan request by building command and executing via PTY.
///
/// Inputs:
/// - `package`: Package name to scan
/// - `do_clamav`/`do_trivy`/`do_semgrep`/`do_shellcheck`/`do_virustotal`/`do_custom`: Scan flags
/// - `dry_run`: Whether to run in dry-run mode
/// - `res_tx`: Channel sender for output
#[cfg(not(target_os = "windows"))]
#[allow(
    clippy::needless_pass_by_value, // Values are moved into spawn_blocking closure
    clippy::too_many_arguments, // Scan configuration requires multiple flags
    clippy::fn_params_excessive_bools // Scan configuration requires multiple bool flags
)]
fn handle_scan_request(
    package: String,
    do_clamav: bool,
    do_trivy: bool,
    do_semgrep: bool,
    do_shellcheck: bool,
    do_virustotal: bool,
    do_custom: bool,
    dry_run: bool,
    res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    tracing::info!(
        "[Runtime] Executor worker received scan request: package={}, dry_run={}",
        package,
        dry_run
    );
    let cmd = build_scan_command_for_executor(
        &package,
        do_clamav,
        do_trivy,
        do_semgrep,
        do_shellcheck,
        do_virustotal,
        do_custom,
        dry_run,
    );
    let res_tx_clone = res_tx;
    tokio::task::spawn_blocking(move || {
        execute_command_pty(&cmd, None, res_tx_clone);
    });
}

/// What: Handle custom command request by building command and executing via PTY.
///
/// Inputs:
/// - `command`: Command string to execute
/// - `password`: Optional sudo password
/// - `dry_run`: Whether to run in dry-run mode
/// - `res_tx`: Channel sender for output
#[cfg(not(target_os = "windows"))]
#[allow(clippy::needless_pass_by_value)] // Values are moved into spawn_blocking closure
fn handle_custom_command_request(
    command: String,
    password: Option<crate::state::SecureString>,
    dry_run: bool,
    res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    tracing::info!(
        "[Runtime] Executor worker received custom command request, dry_run={}",
        dry_run
    );
    let cmd = if dry_run {
        // Properly quote the command to avoid syntax errors with complex shell constructs
        use crate::install::shell_single_quote;
        let quoted = shell_single_quote(&command);
        format!("echo DRY RUN: {quoted}")
    } else {
        // For commands that invoke a privilege tool, provide password via ASKPASS.
        // Only sudo supports SUDO_ASKPASS; doas commands are run directly (doas
        // handles its own terminal prompting and has no askpass equivalent).
        let tool = match crate::logic::privilege::active_tool() {
            Ok(t) => t,
            Err(err) => {
                let _ = res_tx.send(ExecutorOutput::Error(err));
                return;
            }
        };
        let needs_askpass = command.contains(tool.binary_name())
            && tool.capabilities().supports_askpass
            && password.is_some();
        if needs_askpass {
            if let Some(ref pass) = password {
                // Create a temporary script that outputs the password
                // Use printf instead of echo for better security
                use std::fs;
                use std::os::unix::fs::PermissionsExt;
                let temp_dir = std::env::temp_dir();
                #[allow(clippy::uninlined_format_args)]
                // process::id() needs formatting
                let askpass_script =
                    temp_dir.join(format!("pacsea_sudo_askpass_{}.sh", std::process::id()));
                // Use printf with %s to safely output password
                // Escape single quotes in password by replacing ' with '\''
                let escaped_pass = pass.replace('\'', "'\\''");
                #[allow(clippy::uninlined_format_args)] // Need to escape password
                let script_content = format!("#!/bin/sh\nprintf '%s\\n' '{}'\n", escaped_pass);
                if let Err(e) = fs::write(&askpass_script, script_content) {
                    let _ = res_tx.send(ExecutorOutput::Error(format!(
                        "Failed to create sudo askpass script: {e}"
                    )));
                    return;
                }
                // Make script executable
                if let Err(e) =
                    fs::set_permissions(&askpass_script, fs::Permissions::from_mode(0o755))
                {
                    let _ = res_tx.send(ExecutorOutput::Error(format!(
                        "Failed to make askpass script executable: {e}"
                    )));
                    return;
                }
                let askpass_path = askpass_script.to_string_lossy().to_string();
                // Escape the path for shell
                let escaped_path = askpass_path.replace('\'', "'\\''");
                // Need to escape path and use command variable, so can't use inline format
                let final_cmd = format!(
                    "export SUDO_ASKPASS='{escaped_path}'; {command}; rm -f '{escaped_path}'"
                );
                final_cmd
            } else {
                // No password provided, try without SUDO_ASKPASS
                // (might work if passwordless sudo is configured)
                command
            }
        } else {
            command
        }
    };
    let res_tx_clone = res_tx;
    tokio::task::spawn_blocking(move || {
        execute_command_pty(&cmd, password, res_tx_clone);
    });
}

/// What: Spawn background worker for command execution via PTY.
///
/// Inputs:
/// - `executor_req_rx`: Channel receiver for executor requests
/// - `executor_res_tx`: Channel sender for executor output
///
/// Details:
/// - Executes commands in a PTY to capture full terminal output
/// - Streams output line by line to the main event loop
/// - Handles install, remove, downgrade, update, scan, and custom command operations
#[cfg(not(target_os = "windows"))]
pub fn spawn_executor_worker(
    executor_req_rx: mpsc::UnboundedReceiver<ExecutorRequest>,
    executor_res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    let executor_res_tx_bg = executor_res_tx;
    tokio::spawn(async move {
        let mut executor_req_rx = executor_req_rx;
        tracing::info!("[Runtime] Executor worker started, waiting for requests...");
        while let Some(request) = executor_req_rx.recv().await {
            let res_tx = executor_res_tx_bg.clone();
            match request {
                ExecutorRequest::Install {
                    items,
                    password,
                    dry_run,
                } => handle_install_request(items, password, dry_run, res_tx),
                ExecutorRequest::Remove {
                    names,
                    password,
                    cascade,
                    dry_run,
                } => handle_remove_request(names, password, cascade, dry_run, res_tx),
                ExecutorRequest::Downgrade {
                    names,
                    password,
                    dry_run,
                } => handle_downgrade_request(names, password, dry_run, res_tx),
                ExecutorRequest::Update {
                    commands,
                    password,
                    dry_run,
                } => handle_update_request(commands, password, dry_run, res_tx),
                #[cfg(not(target_os = "windows"))]
                ExecutorRequest::Scan {
                    package,
                    do_clamav,
                    do_trivy,
                    do_semgrep,
                    do_shellcheck,
                    do_virustotal,
                    do_custom,
                    dry_run,
                } => handle_scan_request(
                    package,
                    do_clamav,
                    do_trivy,
                    do_semgrep,
                    do_shellcheck,
                    do_virustotal,
                    do_custom,
                    dry_run,
                    res_tx,
                ),
                ExecutorRequest::CustomCommand {
                    command,
                    password,
                    dry_run,
                } => handle_custom_command_request(command, password, dry_run, res_tx),
            }
        }
        tracing::debug!("[Runtime] Executor worker exiting (channel closed)");
    });
}

#[cfg(not(target_os = "windows"))]
/// What: Process text characters and send lines via channel.
///
/// Inputs:
/// - `text`: Text to process
/// - `line_buffer`: Mutable reference to current line buffer
/// - `res_tx`: Channel sender for output lines
///
/// Details:
/// - Handles newlines and carriage returns, strips ANSI codes, and sends complete lines.
fn process_text_chars(
    text: &str,
    line_buffer: &mut String,
    res_tx: &mpsc::UnboundedSender<ExecutorOutput>,
) -> usize {
    let mut lines_sent = 0;
    for ch in text.chars() {
        match ch {
            '\n' => {
                // Newline - send the current line and start a new one
                if !line_buffer.trim().is_empty() {
                    // Strip ANSI escape codes before sending
                    let cleaned = strip_ansi_escapes::strip_str(&*line_buffer);
                    tracing::trace!(
                        "[PTY] Sending line: {}...",
                        &cleaned[..cleaned.len().min(50)]
                    );
                    if res_tx.send(ExecutorOutput::Line(cleaned)).is_ok() {
                        lines_sent += 1;
                    } else {
                        tracing::warn!("[PTY] Failed to send line - channel closed");
                    }
                }
                line_buffer.clear();
            }
            '\r' => {
                // Carriage return - check if this looks like a progress bar update
                // Progress bars typically have patterns like [====>], percentages, or (X/Y) at start
                // If the buffer is empty or doesn't look like a progress bar, treat as new line
                if !line_buffer.trim().is_empty() {
                    let cleaned = strip_ansi_escapes::strip_str(&*line_buffer);
                    // Check if this looks like a progress bar (has progress indicators)
                    // Be conservative: only replace if it has both brackets AND percentage
                    // This avoids replacing regular output like "(1/1) Arming ConditionNeedsUpdate..."
                    let has_progress_brackets = cleaned.contains('[') && cleaned.contains(']');
                    let has_percentage = cleaned.contains('%');
                    let looks_like_progress = has_progress_brackets && has_percentage;

                    if looks_like_progress {
                        // This is a progress bar update - replace the last line
                        if res_tx
                            .send(ExecutorOutput::ReplaceLastLine(cleaned))
                            .is_ok()
                        {
                            lines_sent += 1;
                        }
                    } else {
                        // This is regular output with \r - send as new line
                        if res_tx.send(ExecutorOutput::Line(cleaned)).is_ok() {
                            lines_sent += 1;
                        }
                    }
                }
                line_buffer.clear();
            }
            _ => {
                line_buffer.push(ch);
            }
        }
    }
    lines_sent
}

#[cfg(not(target_os = "windows"))]
/// What: Spawns a reader thread that reads from PTY and sends data to channel.
///
/// Inputs:
/// - `reader`: PTY reader to read from
/// - `data_tx`: Channel sender to send read bytes
///
/// Details:
/// - Reads in 4KB chunks and sends to channel
/// - Sends empty vec on EOF to signal completion
#[cfg(not(target_os = "windows"))]
fn spawn_pty_reader_thread(
    reader: Box<dyn std::io::Read + Send>,
    data_tx: std::sync::mpsc::Sender<Vec<u8>>,
) {
    std::thread::spawn(move || {
        tracing::debug!("[PTY Reader] Reader thread started");
        let mut reader = reader;
        let mut total_bytes_read: usize = 0;
        loop {
            let mut buf = [0u8; 4096];
            match reader.read(&mut buf) {
                Ok(0) => {
                    tracing::debug!(
                        "[PTY Reader] EOF received, total bytes read: {}",
                        total_bytes_read
                    );
                    let _ = data_tx.send(Vec::new());
                    break;
                }
                Ok(n) => {
                    total_bytes_read += n;
                    tracing::trace!(
                        "[PTY Reader] Read {} bytes (total: {})",
                        n,
                        total_bytes_read
                    );
                    if data_tx.send(buf[..n].to_vec()).is_err() {
                        tracing::debug!("[PTY Reader] Receiver dropped, exiting");
                        break;
                    }
                }
                Err(e) => {
                    tracing::debug!(
                        "[PTY Reader] Read error: {}, total bytes: {}",
                        e,
                        total_bytes_read
                    );
                    break;
                }
            }
        }
        tracing::debug!("[PTY Reader] Reader thread exiting");
    });
}

/// What: Process byte buffer handling incomplete UTF-8 sequences.
///
/// Inputs:
/// - `byte_buffer`: Buffer containing raw bytes to process
/// - `line_buffer`: Buffer for accumulating line text
/// - `res_tx`: Channel to send processed output
///
/// Output:
/// - Number of lines sent to channel
///
/// Details:
/// - Handles split UTF-8 sequences at buffer boundaries
/// - Falls back to lossy conversion if needed
#[cfg(not(target_os = "windows"))]
fn process_byte_buffer_utf8(
    byte_buffer: &mut Vec<u8>,
    line_buffer: &mut String,
    res_tx: &mpsc::UnboundedSender<ExecutorOutput>,
) -> usize {
    let mut lines_sent = 0;

    loop {
        // Try to decode the full buffer
        if let Ok(text) = String::from_utf8(byte_buffer.clone()) {
            byte_buffer.clear();
            lines_sent += process_text_chars(&text, line_buffer, res_tx);
            break;
        }

        // Buffer is small - might be incomplete UTF-8 sequence, wait for more
        if byte_buffer.len() < 4 {
            break;
        }

        // Try to find valid UTF-8 by trimming bytes from the end
        let mut found_valid = false;
        for trim_len in 1..=4.min(byte_buffer.len()) {
            let test_len = byte_buffer.len().saturating_sub(trim_len);
            if test_len == 0 {
                break;
            }

            if let Ok(text) = String::from_utf8(byte_buffer[..test_len].to_vec()) {
                lines_sent += process_text_chars(&text, line_buffer, res_tx);
                byte_buffer.drain(..test_len);
                found_valid = true;
                break;
            }
        }

        if !found_valid {
            // Fall back to lossy conversion
            let text = String::from_utf8_lossy(byte_buffer);
            lines_sent += process_text_chars(&text, line_buffer, res_tx);
            byte_buffer.clear();
            break;
        }
    }

    lines_sent
}

/// What: Send remaining line content and finished message.
///
/// Inputs:
/// - `line_buffer`: Any remaining line content to send
/// - `lines_sent`: Current count of lines sent
/// - `exit_code_u32`: Exit code from process
/// - `res_tx`: Channel to send output
/// - `context`: String describing the context (for logging)
#[cfg(not(target_os = "windows"))]
fn send_finish_message(
    line_buffer: &str,
    lines_sent: &mut usize,
    exit_code_u32: u32,
    res_tx: &mpsc::UnboundedSender<ExecutorOutput>,
    context: &str,
) {
    // Send any remaining line
    if !line_buffer.trim().is_empty() {
        let cleaned = strip_ansi_escapes::strip_str(line_buffer);
        if res_tx.send(ExecutorOutput::Line(cleaned)).is_ok() {
            *lines_sent += 1;
        }
    }

    let success = exit_code_u32 == 0;
    let exit_code = i32::try_from(exit_code_u32).ok();
    tracing::info!(
        "[PTY] Process finished{}: success={}, exit_code={:?}, total_lines_sent={}",
        context,
        success,
        exit_code,
        lines_sent
    );
    let _ = res_tx.send(ExecutorOutput::Finished {
        success,
        exit_code,
        failed_command: None,
    });
}

/// What: Execute command in PTY and stream output.
///
/// Inputs:
/// - `cmd`: Command string to execute
/// - `_password`: Optional password (currently unused - password is handled in command builder)
/// - `res_tx`: Channel sender for output lines
///
/// Details:
/// - Creates a PTY, spawns bash to execute the command
/// - Reads output line by line and sends via channel
/// - Strips ANSI escape codes from output using `strip-ansi-escapes` crate
/// - Sends Finished message when command completes
/// - Note: Password is handled in command builder (piped for official packages, credential caching for AUR)
#[cfg(not(target_os = "windows"))]
#[allow(clippy::needless_pass_by_value)] // Pass by value needed for move into closure
fn execute_command_pty(
    cmd: &str,
    _password: Option<crate::state::SecureString>,
    res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    use portable_pty::{CommandBuilder, PtySize, native_pty_system};
    use std::sync::mpsc as std_mpsc;

    tracing::debug!("[PTY] Starting execute_command_pty");
    tracing::debug!("[PTY] Command length: {} chars", cmd.len());

    // Open PTY
    let pty_system = native_pty_system();
    let pty_size = PtySize {
        rows: 24,
        cols: 80,
        pixel_width: 0,
        pixel_height: 0,
    };

    tracing::debug!("[PTY] Opening PTY");
    let pty = match pty_system.openpty(pty_size) {
        Ok(pty) => pty,
        Err(e) => {
            tracing::error!("[PTY] Failed to open PTY: {e}");
            let _ = res_tx.send(ExecutorOutput::Error(format!("Failed to open PTY: {e}")));
            return;
        }
    };

    // Spawn command
    let mut cmd_builder = CommandBuilder::new("bash");
    cmd_builder.arg("-c");
    cmd_builder.arg(cmd);

    tracing::debug!("[PTY] Spawning bash command");
    let mut child = match pty.slave.spawn_command(cmd_builder) {
        Ok(child) => child,
        Err(e) => {
            tracing::error!("[PTY] Failed to spawn command: {e}");
            let _ = res_tx.send(ExecutorOutput::Error(format!("Failed to spawn: {e}")));
            return;
        }
    };

    // Setup reader thread
    let reader = pty
        .master
        .try_clone_reader()
        .expect("Failed to clone reader");
    let _master = pty.master; // Keep master alive
    let (data_tx, data_rx) = std_mpsc::channel::<Vec<u8>>();
    spawn_pty_reader_thread(reader, data_tx);

    // Process output
    let mut byte_buffer = Vec::new();
    let mut line_buffer = String::new();
    let mut lines_sent: usize = 0;

    tracing::debug!("[PTY] Entering main processing loop");
    loop {
        // Check if child has exited
        match child.try_wait() {
            Ok(Some(status)) => {
                tracing::debug!("[PTY] Child exited with code: {:?}", status.exit_code());
                drain_remaining_data(
                    &data_rx,
                    &mut byte_buffer,
                    &mut line_buffer,
                    &mut lines_sent,
                    &res_tx,
                );
                send_finish_message(
                    &line_buffer,
                    &mut lines_sent,
                    status.exit_code(),
                    &res_tx,
                    "",
                );
                return;
            }
            Ok(None) => {} // Still running
            Err(e) => {
                tracing::error!("[PTY] Error checking child status: {e}");
                let _ = res_tx.send(ExecutorOutput::Error(format!("Process error: {e}")));
                return;
            }
        }

        // Receive data with timeout
        match data_rx.recv_timeout(std::time::Duration::from_millis(50)) {
            Ok(data) if data.is_empty() => {
                tracing::debug!("[PTY] Got EOF signal");
                break;
            }
            Ok(data) => {
                byte_buffer.extend_from_slice(&data);
                lines_sent += process_byte_buffer_utf8(&mut byte_buffer, &mut line_buffer, &res_tx);
            }
            Err(std_mpsc::RecvTimeoutError::Timeout) => {}
            Err(std_mpsc::RecvTimeoutError::Disconnected) => {
                tracing::debug!("[PTY] Read thread disconnected");
                break;
            }
        }
    }

    // Wait for process and send finish (EOF path)
    tracing::debug!("[PTY] Waiting for child process after EOF");
    match child.wait() {
        Ok(status) => {
            send_finish_message(
                &line_buffer,
                &mut lines_sent,
                status.exit_code(),
                &res_tx,
                " (post-loop)",
            );
        }
        Err(e) => {
            tracing::error!("[PTY] Child wait error: {e}");
            let _ = res_tx.send(ExecutorOutput::Error(format!("Wait error: {e}")));
        }
    }
}

/// What: Drain remaining data from channel after child exits.
///
/// Inputs:
/// - `data_rx`: Channel receiver
/// - `byte_buffer`: Buffer to accumulate bytes
/// - `line_buffer`: Buffer for line text
/// - `lines_sent`: Counter for lines sent
/// - `res_tx`: Channel to send output
#[cfg(not(target_os = "windows"))]
fn drain_remaining_data(
    data_rx: &std::sync::mpsc::Receiver<Vec<u8>>,
    byte_buffer: &mut Vec<u8>,
    line_buffer: &mut String,
    lines_sent: &mut usize,
    res_tx: &mpsc::UnboundedSender<ExecutorOutput>,
) {
    while let Ok(data) = data_rx.recv_timeout(std::time::Duration::from_millis(100)) {
        if data.is_empty() {
            break;
        }
        byte_buffer.extend_from_slice(&data);
        if let Ok(text) = String::from_utf8(byte_buffer.clone()) {
            byte_buffer.clear();
            *lines_sent += process_text_chars(&text, line_buffer, res_tx);
        }
    }
}

#[cfg(target_os = "windows")]
/// What: Placeholder executor worker for Windows (unsupported).
///
/// Inputs:
/// - `executor_req_rx`: Channel receiver for executor requests
/// - `executor_res_tx`: Channel sender for executor output
///
/// Details:
/// - Windows is not supported for PTY-based execution
pub fn spawn_executor_worker(
    mut executor_req_rx: mpsc::UnboundedReceiver<ExecutorRequest>,
    executor_res_tx: mpsc::UnboundedSender<ExecutorOutput>,
) {
    tokio::spawn(async move {
        while let Some(_request) = executor_req_rx.recv().await {
            let _ = executor_res_tx.send(ExecutorOutput::Error(
                "PTY execution is not supported on Windows".to_string(),
            ));
        }
    });
}