fission-command-ui 0.14.1

Terminal UI application for the fission command
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
use super::state::UiState;
use fission_command_core::Target;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process::{Command, ExitStatus, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

pub const DEFAULT_SCROLLBACK_LINES: usize = 100_000;
const MAX_SCROLLBACK_LINE_CHARS: usize = 4096;
pub type CommandSessionId = u64;

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum UiCommand {
    InitProject,
    AddTarget(Target),
    DoctorAll,
    DoctorTarget(Target),
    Refresh,
    RunSelected,
    BuildSelected,
    TestSelected,
    SiteBuild,
    SiteCheck,
    SiteRoutes,
    SiteServe,
    LogsSnapshot,
    LogsFollow,
}

impl UiCommand {
    pub fn label(&self) -> String {
        match self {
            Self::InitProject => "initialise this project".to_string(),
            Self::AddTarget(target) => format!("add the {} target", target.as_str()),
            Self::DoctorAll => "run doctor checks".to_string(),
            Self::DoctorTarget(target) => format!("run {} doctor checks", target.as_str()),
            Self::Refresh => "refresh project state".to_string(),
            Self::RunSelected => "run the selected target".to_string(),
            Self::BuildSelected => "build the selected target".to_string(),
            Self::TestSelected => "test the selected target".to_string(),
            Self::SiteBuild => "build the static site".to_string(),
            Self::SiteCheck => "check the static site".to_string(),
            Self::SiteRoutes => "list static site routes".to_string(),
            Self::SiteServe => "serve the static site".to_string(),
            Self::LogsSnapshot => "read logs".to_string(),
            Self::LogsFollow => "follow logs".to_string(),
        }
    }

    pub fn confirmation_message(&self) -> String {
        match self {
            Self::InitProject => {
                "This writes missing Fission project files only when they are absent.".to_string()
            }
            Self::AddTarget(target) => format!(
                "This scaffolds or updates the {} platform files for this project.",
                target.as_str()
            ),
            Self::RunSelected => {
                "This builds, launches, and attaches output for the selected target unless detach is enabled.".to_string()
            }
            Self::BuildSelected => "This starts a build and streams compiler output.".to_string(),
            Self::TestSelected => "This runs the generated target smoke test.".to_string(),
            Self::SiteServe => {
                "This starts a local site server and keeps its output in a command tab.".to_string()
            }
            Self::LogsFollow => {
                "This starts a log follower and keeps streaming output into a command tab.".to_string()
            }
            Self::Refresh => "This refreshes project, target, and device state.".to_string(),
            Self::DoctorAll | Self::DoctorTarget(_) => {
                "This checks local tooling and reports missing setup.".to_string()
            }
            Self::SiteBuild | Self::SiteCheck | Self::SiteRoutes | Self::LogsSnapshot => {
                "This runs the selected workflow and stores the result in a command tab.".to_string()
            }
        }
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CommandRecord {
    pub title: String,
    pub status: CommandStatus,
    pub output: ScrollbackBuffer,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum CommandStatus {
    #[default]
    Ready,
    Running,
    Ok,
    Failed,
}

#[derive(Clone, Debug)]
pub struct ScrollbackBuffer {
    inner: Arc<Mutex<ScrollbackBufferData>>,
}

#[derive(Debug, Eq, PartialEq)]
struct ScrollbackBufferData {
    limit: usize,
    dropped_lines: usize,
    lines: VecDeque<String>,
}

impl Default for ScrollbackBuffer {
    fn default() -> Self {
        Self::new(DEFAULT_SCROLLBACK_LINES)
    }
}

impl ScrollbackBuffer {
    pub fn new(limit: usize) -> Self {
        Self {
            inner: Arc::new(Mutex::new(ScrollbackBufferData {
                limit: limit.max(1),
                dropped_lines: 0,
                lines: VecDeque::new(),
            })),
        }
    }

    pub fn from_text(limit: usize, text: impl AsRef<str>) -> Self {
        let mut buffer = Self::new(limit);
        for line in text.as_ref().lines() {
            buffer.push_line(line);
        }
        if buffer.display_line_count() == 0 {
            buffer.push_line("");
        }
        buffer
    }

    pub fn set_limit(&mut self, limit: usize) {
        let mut data = self.inner.lock().expect("scrollback lock poisoned");
        data.limit = limit.max(1);
        data.trim_to_limit();
    }

    pub fn push_line(&mut self, line: &str) {
        let mut data = self.inner.lock().expect("scrollback lock poisoned");
        data.lines.push_back(truncate_line(line));
        data.trim_to_limit();
    }

    pub fn display_line_count(&self) -> usize {
        let data = self.inner.lock().expect("scrollback lock poisoned");
        data.lines.len() + usize::from(data.dropped_lines > 0)
    }

    pub fn visible_lines(&self, start: usize, count: usize) -> Vec<String> {
        let mut visible = Vec::new();
        if count == 0 {
            return visible;
        }
        let data = self.inner.lock().expect("scrollback lock poisoned");
        let marker_lines = usize::from(data.dropped_lines > 0);
        if data.dropped_lines > 0 && start == 0 {
            visible.push(format!(
                "... {} older lines discarded by scrollback limit ...",
                data.dropped_lines
            ));
        }
        let first_buffer_line = start.saturating_sub(marker_lines);
        let remaining = count.saturating_sub(visible.len());
        visible.extend(
            data.lines
                .iter()
                .skip(first_buffer_line)
                .take(remaining)
                .cloned(),
        );
        visible
    }
}

impl PartialEq for ScrollbackBuffer {
    fn eq(&self, other: &Self) -> bool {
        if Arc::ptr_eq(&self.inner, &other.inner) {
            return true;
        }
        let data = self.inner.lock().expect("scrollback lock poisoned");
        let other_data = other.inner.lock().expect("scrollback lock poisoned");
        *data == *other_data
    }
}

impl Eq for ScrollbackBuffer {}

impl ScrollbackBufferData {
    fn trim_to_limit(&mut self) {
        while self.lines.len() > self.limit {
            self.lines.pop_front();
            self.dropped_lines = self.dropped_lines.saturating_add(1);
        }
    }
}

impl CommandStatus {
    pub fn label(self) -> &'static str {
        match self {
            Self::Ready => "Ready",
            Self::Running => "Running",
            Self::Ok => "OK",
            Self::Failed => "Failed",
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct CommandSnapshot {
    pub id: CommandSessionId,
    pub revision: u64,
    pub record: CommandRecord,
    pub finished: bool,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct CommandRuntimeSnapshot {
    pub active_session_id: Option<CommandSessionId>,
    pub sessions: Vec<CommandSnapshot>,
}

#[derive(Clone, Default)]
pub struct CommandRuntime {
    inner: Arc<Mutex<CommandRuntimeState>>,
}

impl std::fmt::Debug for CommandRuntime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CommandRuntime").finish_non_exhaustive()
    }
}

impl PartialEq for CommandRuntime {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

#[derive(Default)]
struct CommandRuntimeState {
    next_session_id: CommandSessionId,
    active_session_id: Option<CommandSessionId>,
    sessions: Vec<CommandSnapshot>,
}

impl CommandRuntime {
    fn begin(&self, mut record: CommandRecord, limit: usize) -> CommandSessionId {
        let mut state = self.inner.lock().expect("command runtime lock poisoned");
        state.next_session_id = state.next_session_id.saturating_add(1);
        let session_id = state.next_session_id;
        record.output.set_limit(limit);
        state.active_session_id = Some(session_id);
        state.sessions.push(CommandSnapshot {
            id: session_id,
            revision: 0,
            record,
            finished: false,
        });
        session_id
    }

    fn update(
        &self,
        session_id: CommandSessionId,
        update: impl FnOnce(&mut CommandRecord, &mut bool),
    ) {
        let mut state = self.inner.lock().expect("command runtime lock poisoned");
        let Some(snapshot) = state.sessions.iter_mut().find(|item| item.id == session_id) else {
            return;
        };
        update(&mut snapshot.record, &mut snapshot.finished);
        snapshot.revision = snapshot.revision.saturating_add(1);
    }

    pub fn snapshot(&self) -> CommandRuntimeSnapshot {
        let state = self.inner.lock().expect("command runtime lock poisoned");
        CommandRuntimeSnapshot {
            active_session_id: state.active_session_id,
            sessions: state.sessions.clone(),
        }
    }

    pub fn set_active(&self, session_id: CommandSessionId) {
        let mut state = self.inner.lock().expect("command runtime lock poisoned");
        if state.sessions.iter().any(|item| item.id == session_id) {
            state.active_session_id = Some(session_id);
        }
    }

    pub fn record_completed(
        &self,
        mut record: CommandRecord,
        limit: usize,
        finished: bool,
    ) -> CommandSessionId {
        let mut state = self.inner.lock().expect("command runtime lock poisoned");
        state.next_session_id = state.next_session_id.saturating_add(1);
        let session_id = state.next_session_id;
        record.output.set_limit(limit);
        state.active_session_id = Some(session_id);
        state.sessions.push(CommandSnapshot {
            id: session_id,
            revision: 0,
            record,
            finished,
        });
        session_id
    }

    pub fn set_limit(&self, limit: usize) {
        let mut state = self.inner.lock().expect("command runtime lock poisoned");
        for snapshot in &mut state.sessions {
            snapshot.record.output.set_limit(limit);
            snapshot.revision = snapshot.revision.saturating_add(1);
        }
    }
}

struct CommandPlan {
    title: String,
    args: Vec<String>,
    mode: CommandMode,
}

#[derive(Clone, Copy)]
enum CommandMode {
    Capture,
}

pub fn execute_ui_command(state: &mut UiState, command: UiCommand) {
    if matches!(command, UiCommand::Refresh) {
        state.refresh();
        let record = CommandRecord {
            title: "Refresh".to_string(),
            status: CommandStatus::Ok,
            output: ScrollbackBuffer::from_text(
                state.scrollback_limit,
                "Project, target, and device state refreshed.",
            ),
        };
        state
            .command_runtime
            .record_completed(record, state.scrollback_limit, true);
        state.sync_command_sessions();
        return;
    }

    let Some(plan) = command_plan(state, command) else {
        let record = CommandRecord {
            title: "Action unavailable".to_string(),
            status: CommandStatus::Failed,
            output: ScrollbackBuffer::from_text(
                state.scrollback_limit,
                "Select a target or device before running this action.",
            ),
        };
        state
            .command_runtime
            .record_completed(record, state.scrollback_limit, true);
        state.sync_command_sessions();
        return;
    };

    let record = match plan.mode {
        CommandMode::Capture => start_capture_command(state, plan),
    };
    if matches!(record.status, CommandStatus::Failed) {
        state
            .command_runtime
            .record_completed(record, state.scrollback_limit, true);
    }
    state.sync_command_sessions();
}

fn command_plan(state: &UiState, command: UiCommand) -> Option<CommandPlan> {
    let project_dir = state.project_dir.display().to_string();
    match command {
        UiCommand::InitProject => {
            let mut args = vec!["init".into(), project_dir];
            push_optional_flag(&mut args, "--name", &state.init_name);
            push_optional_flag(&mut args, "--app-id", &state.init_app_id);
            push_optional_flag(&mut args, "--local-path", &state.init_local_path);
            Some(CommandPlan {
                title: "Initialise project".to_string(),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::AddTarget(target) => Some(CommandPlan {
            title: format!("Add {} target", target.as_str()),
            args: vec![
                "add-target".into(),
                target.as_str().into(),
                "--project-dir".into(),
                project_dir,
            ],
            mode: CommandMode::Capture,
        }),
        UiCommand::DoctorAll => Some(CommandPlan {
            title: "Doctor".to_string(),
            args: {
                let mut args = vec!["doctor".into(), "--project-dir".into(), project_dir];
                if state.strict {
                    args.push("--strict".into());
                }
                args
            },
            mode: CommandMode::Capture,
        }),
        UiCommand::DoctorTarget(target) => Some(CommandPlan {
            title: format!("Doctor {}", target.as_str()),
            args: {
                let mut args = vec![
                    "doctor".into(),
                    target.as_str().into(),
                    "--project-dir".into(),
                    project_dir,
                ];
                if state.strict {
                    args.push("--strict".into());
                }
                args
            },
            mode: CommandMode::Capture,
        }),
        UiCommand::RunSelected => {
            let target = state.selected_target?;
            let mut args = vec![
                "run".into(),
                "--target".into(),
                target.as_str().into(),
                "--project-dir".into(),
                project_dir,
                "--host".into(),
                state.host.clone(),
                "--port".into(),
                state.port.clone(),
            ];
            push_common_run_flags(state, &mut args);
            Some(CommandPlan {
                title: format!("Run {}", target.as_str()),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::BuildSelected => {
            let target = state.selected_target?;
            let mut args = vec![
                "build".into(),
                "--target".into(),
                target.as_str().into(),
                "--project-dir".into(),
                project_dir,
            ];
            if state.release {
                args.push("--release".into());
            }
            Some(CommandPlan {
                title: format!("Build {}", target.as_str()),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::TestSelected => {
            let target = state.selected_target?;
            let mut args = vec![
                "test".into(),
                "--target".into(),
                target.as_str().into(),
                "--project-dir".into(),
                project_dir,
            ];
            if state.headless {
                args.push("--headless".into());
            }
            Some(CommandPlan {
                title: format!("Test {}", target.as_str()),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::SiteBuild => {
            let mut args = vec![
                "site".into(),
                "build".into(),
                "--project-dir".into(),
                project_dir,
            ];
            if state.release {
                args.push("--release".into());
            }
            Some(CommandPlan {
                title: "Build static site".to_string(),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::SiteCheck => {
            let mut args = vec![
                "site".into(),
                "check".into(),
                "--project-dir".into(),
                project_dir,
            ];
            if state.release {
                args.push("--release".into());
            }
            Some(CommandPlan {
                title: "Check static site".to_string(),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::SiteRoutes => Some(CommandPlan {
            title: "List static site routes".to_string(),
            args: vec![
                "site".into(),
                "routes".into(),
                "--project-dir".into(),
                project_dir,
            ],
            mode: CommandMode::Capture,
        }),
        UiCommand::SiteServe => {
            let mut args = vec![
                "site".into(),
                "serve".into(),
                "--project-dir".into(),
                project_dir,
                "--host".into(),
                state.host.clone(),
                "--port".into(),
                state.port.clone(),
            ];
            if state.release {
                args.push("--release".into());
            }
            if state.no_open {
                args.push("--no-open".into());
            }
            Some(CommandPlan {
                title: "Serve static site".to_string(),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::LogsSnapshot => {
            let target = state.selected_target?;
            let mut args = vec![
                "logs".into(),
                "--target".into(),
                target.as_str().into(),
                "--project-dir".into(),
                project_dir,
            ];
            if let Some(device) = selected_device_arg(state) {
                args.extend(["--device".into(), device]);
            }
            Some(CommandPlan {
                title: format!("Logs {}", target.as_str()),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::LogsFollow => {
            let target = state.selected_target?;
            let mut args = vec![
                "logs".into(),
                "--target".into(),
                target.as_str().into(),
                "--project-dir".into(),
                project_dir,
                "--follow".into(),
            ];
            if let Some(device) = selected_device_arg(state) {
                args.extend(["--device".into(), device]);
            }
            Some(CommandPlan {
                title: format!("Follow {} logs", target.as_str()),
                args,
                mode: CommandMode::Capture,
            })
        }
        UiCommand::Refresh => None,
    }
}

fn push_common_run_flags(state: &UiState, args: &mut Vec<String>) {
    if let Some(device) = selected_device_arg(state) {
        args.extend(["--device".into(), device]);
    }
    if state.detach {
        args.push("--detach".into());
    }
    if state.release {
        args.push("--release".into());
    }
    if state.no_open {
        args.push("--no-open".into());
    }
    if state.headless {
        args.push("--headless".into());
    }
}

fn push_optional_flag(args: &mut Vec<String>, name: &str, value: &str) {
    let value = value.trim();
    if !value.is_empty() {
        args.extend([name.to_string(), value.to_string()]);
    }
}

fn selected_device_arg(state: &UiState) -> Option<String> {
    let selected = state.selected_device.as_ref()?;
    if selected == "auto" {
        None
    } else {
        Some(selected.clone())
    }
}

fn start_capture_command(state: &UiState, plan: CommandPlan) -> CommandRecord {
    let command_line = format_command_line(&plan.args);
    let initial_record = CommandRecord {
        title: plan.title.clone(),
        status: CommandStatus::Running,
        output: ScrollbackBuffer::from_text(
            state.scrollback_limit,
            format!("Running `{command_line}`..."),
        ),
    };
    let generation = state
        .command_runtime
        .begin(initial_record.clone(), state.scrollback_limit);
    let runtime = state.command_runtime.clone();
    let mut command = command_base(state);
    command
        .args(&plan.args)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    thread::spawn(move || run_capture_command(runtime, generation, command));
    initial_record
}

fn run_capture_command(runtime: CommandRuntime, generation: u64, mut command: Command) {
    let mut child = match command.spawn() {
        Ok(child) => child,
        Err(error) => {
            runtime.update(generation, |record, finished| {
                record.status = CommandStatus::Failed;
                record
                    .output
                    .push_line(&format!("Failed to start command: {error}"));
                *finished = true;
            });
            return;
        }
    };

    runtime.update(generation, |record, _| {
        record
            .output
            .push_line(&format!("Started process {}.", child.id()));
    });

    let (tx, rx) = std::sync::mpsc::channel::<String>();
    if let Some(stdout) = child.stdout.take() {
        pipe_lines(stdout, tx.clone());
    }
    if let Some(stderr) = child.stderr.take() {
        pipe_lines(stderr, tx.clone());
    }
    drop(tx);

    let status = loop {
        drain_output(&runtime, generation, &rx);
        match child.try_wait() {
            Ok(Some(status)) => break status,
            Ok(None) => thread::sleep(Duration::from_millis(50)),
            Err(error) => {
                runtime.update(generation, |record, finished| {
                    record.status = CommandStatus::Failed;
                    record
                        .output
                        .push_line(&format!("Failed to wait for command: {error}"));
                    *finished = true;
                });
                return;
            }
        }
    };
    drain_output(&runtime, generation, &rx);
    finish_capture_command(&runtime, generation, status);
}

fn pipe_lines<R>(reader: R, tx: std::sync::mpsc::Sender<String>)
where
    R: std::io::Read + Send + 'static,
{
    thread::spawn(move || {
        for line in BufReader::new(reader).lines() {
            match line {
                Ok(line) => {
                    if tx.send(line).is_err() {
                        break;
                    }
                }
                Err(error) => {
                    let _ = tx.send(format!("Failed to read command output: {error}"));
                    break;
                }
            }
        }
    });
}

fn drain_output(runtime: &CommandRuntime, generation: u64, rx: &std::sync::mpsc::Receiver<String>) {
    let mut lines = Vec::new();
    while let Ok(line) = rx.try_recv() {
        lines.push(line);
    }
    if lines.is_empty() {
        return;
    }
    runtime.update(generation, |record, _| {
        for line in lines {
            record.output.push_line(&line);
        }
    });
}

fn finish_capture_command(runtime: &CommandRuntime, generation: u64, status: ExitStatus) {
    runtime.update(generation, |record, finished| {
        record.status = if status.success() {
            CommandStatus::Ok
        } else {
            CommandStatus::Failed
        };
        record.output.push_line(&format!(
            "Command exited with {}.",
            status
                .code()
                .map(|code| format!("status {code}"))
                .unwrap_or_else(|| "no status code".to_string())
        ));
        *finished = true;
    });
}

fn command_base(state: &UiState) -> Command {
    let exe = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("fission"));
    let mut command = Command::new(exe);
    if state.project_dir.exists() {
        command.current_dir(&state.project_dir);
    } else if let Some(parent) = state.project_dir.parent().filter(|parent| parent.exists()) {
        command.current_dir(parent);
    }
    command
}

fn format_command_line(args: &[String]) -> String {
    let exe = std::env::current_exe()
        .ok()
        .and_then(|path| {
            path.file_name()
                .map(|name| name.to_string_lossy().into_owned())
        })
        .unwrap_or_else(|| "fission".to_string());
    std::iter::once(exe)
        .chain(args.iter().map(|arg| shell_word(arg)))
        .collect::<Vec<_>>()
        .join(" ")
}

fn truncate_line(line: &str) -> String {
    if line.chars().count() <= MAX_SCROLLBACK_LINE_CHARS {
        return line.to_string();
    }
    let mut truncated = line
        .chars()
        .take(MAX_SCROLLBACK_LINE_CHARS)
        .collect::<String>();
    truncated.push_str(" ...");
    truncated
}

fn shell_word(value: &str) -> String {
    if value
        .chars()
        .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.' | '/' | ':' | '='))
    {
        value.to_string()
    } else {
        format!("'{}'", value.replace('\'', "'\\''"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn scrollback_buffer_discards_oldest_lines_at_limit() {
        let mut buffer = ScrollbackBuffer::new(3);
        for line in ["one", "two", "three", "four", "five"] {
            buffer.push_line(line);
        }

        assert_eq!(buffer.display_line_count(), 4);
        assert_eq!(
            buffer.visible_lines(0, 4),
            vec![
                "... 2 older lines discarded by scrollback limit ...".to_string(),
                "three".to_string(),
                "four".to_string(),
                "five".to_string()
            ]
        );
    }

    #[test]
    fn scrollback_buffer_limit_can_be_reduced() {
        let mut buffer = ScrollbackBuffer::new(5);
        for line in ["one", "two", "three", "four"] {
            buffer.push_line(line);
        }

        buffer.set_limit(2);

        assert_eq!(
            buffer.visible_lines(0, 3),
            vec![
                "... 2 older lines discarded by scrollback limit ...".to_string(),
                "three".to_string(),
                "four".to_string()
            ]
        );
    }

    #[test]
    fn command_runtime_keeps_independent_sessions() {
        let runtime = CommandRuntime::default();
        let first = runtime.record_completed(
            CommandRecord {
                title: "Doctor".to_string(),
                status: CommandStatus::Ok,
                output: ScrollbackBuffer::from_text(10, "doctor output"),
            },
            10,
            true,
        );
        let second = runtime.record_completed(
            CommandRecord {
                title: "Serve".to_string(),
                status: CommandStatus::Running,
                output: ScrollbackBuffer::from_text(10, "serve output"),
            },
            10,
            false,
        );

        let snapshot = runtime.snapshot();
        assert_eq!(snapshot.active_session_id, Some(second));
        assert_eq!(snapshot.sessions.len(), 2);
        assert_eq!(snapshot.sessions[0].id, first);
        assert_eq!(snapshot.sessions[1].id, second);
        assert_eq!(
            snapshot.sessions[0].record.output.visible_lines(0, 1),
            vec!["doctor output".to_string()]
        );
        assert_eq!(
            snapshot.sessions[1].record.output.visible_lines(0, 1),
            vec!["serve output".to_string()]
        );
    }
}