xonecas.ioDon't click! Sean Caetano Martin Sign in
cast / src / exec / view.rs 212 lines6.6 kB Raw
1use crate::app::App;
2use crate::exec::machine::{Control, Phase, Status};
3use crate::exec::run::Run;
4use crate::paths;
5use crate::ui::theme::{self, pal};
6use crate::ui::widgets::{Kind, bar, btn, lbl, mono, stat};
7use crate::workflow::dag::{self, Look};
8use crate::workflow::details;
9use rune::{El, col, row, text};
10
11fn phase_label(p: Phase) -> (&'static str, rune::Color) {
12    let c = pal();
13    match p {
14        Phase::Ready => ("ready", c.muted),
15        Phase::Running => ("running", c.ok),
16        Phase::Pausing => ("pausing", c.accent),
17        Phase::Paused => ("paused", c.accent),
18        Phase::Failed => ("failed", c.danger),
19        Phase::Cancelled => ("cancelled", c.danger),
20        Phase::Done => ("done", c.ok),
21    }
22}
23
24pub fn view(s: &App, stem: &str) -> El {
25    let p = pal();
26    let Some(r) = s.runs.iter().find(|r| r.wf.prompt == stem) else {
27        return col();
28    };
29    let phase = r.machine.phase();
30    let ctl = |label: &str, kind: Kind, c: Control, on: bool| {
31        let st = stem.to_string();
32        btn(
33            label,
34            kind,
35            on.then_some(move |s: &mut App| s.control(&st, c)),
36        )
37        .key(format!("ctl-{c:?}"))
38    };
39    let idle = !matches!(phase, Phase::Running | Phase::Pausing | Phase::Done);
40    let controls = row()
41        .h(44.0)
42        .px(12.0)
43        .gap(8.0)
44        .center_y()
45        .fill(p.surface)
46        .child(ctl("▶ Run all", Kind::Primary, Control::All, idle))
47        .child(ctl("› Next task", Kind::Normal, Control::NextTask, idle))
48        .child(ctl("» Next step", Kind::Normal, Control::NextStep, idle))
49        .child(ctl(
50            "‖ Pause",
51            Kind::Normal,
52            Control::Pause,
53            phase == Phase::Running,
54        ))
55        .child(ctl(
56            "■ Cancel",
57            Kind::Danger,
58            Control::Cancel,
59            matches!(phase, Phase::Running | Phase::Pausing),
60        ))
61        .child(row().grow(1.0))
62        .child(mono("Pause stops new tasks from starting", p.faint));
63    let (label, color) = phase_label(phase);
64    let steps = r.wf.steps().len();
65    let step = r.machine.current_step().unwrap_or(steps as u32);
66    let pct = r.progress();
67    let stats = row()
68        .h(64.0)
69        .fill(p.surface)
70        .child(stat(&format!("{:.0}%", pct * 100.0), "progress", p.accent))
71        .child(stat(
72            &format!("{}/{}", r.machine.count(Status::Done), r.wf.tasks.len()),
73            "tasks done",
74            p.strong,
75        ))
76        .child(stat(&format!("{step}/{steps}"), "step", p.strong))
77        .child(stat(label, "state", color));
78    let st = stem.to_string();
79    let clock = s.now();
80    let elapsed = |i: usize| {
81        (r.machine.status[i] != Status::Queued)
82            .then(|| r.elapsed(i, clock))
83            .flatten()
84    };
85    let graph = col()
86        .key("run-dag")
87        .padding(12.0)
88        .scroll_x()
89        .child(dag::view(
90            &r.wf,
91            Look {
92                status: Some(&r.machine.status),
93                selected: Some(r.view),
94                elapsed: &elapsed,
95            },
96            move |s: &mut App, i| {
97                if let Some(r) = s.runs.iter_mut().find(|r| r.wf.prompt == st) {
98                    r.view = i;
99                }
100            },
101        ));
102    col()
103        .w_full()
104        .h_full()
105        .child(controls)
106        .child(stats)
107        .child(bar(pct, p.accent))
108        .child(graph)
109        .child(
110            col()
111                .key("run-task")
112                .max_h(320.0)
113                .padding(16.0)
114                .border_sides([1.0, 0.0, 0.0, 0.0], p.line)
115                .scroll(rune::ScrollSpec {
116                    stick_bottom: false,
117                })
118                .child(details::view(&r.wf, r.view, rune::el())),
119        )
120        .child(terminal(s, r, stem))
121}
122
123fn terminal(s: &App, r: &Run, stem: &str) -> El {
124    let p = pal();
125    let tabs = row().h(32.0).fill(p.surface).children(
126        r.wf.tasks
127            .iter()
128            .enumerate()
129            .filter(|(i, _)| r.machine.status[*i] != Status::Queued || *i == r.view)
130            .map(|(i, t)| {
131                let st = stem.to_string();
132                let on = i == r.view;
133                crate::ui::widgets::tab(&format!("{} output", t.id), on)
134                    .key(format!("log-tab-{}", t.id))
135                    .focusable(true)
136                    .cursor(rune::Cursor::Pointer)
137                    .on_press(move |s: &mut App| {
138                        if let Some(r) = s.runs.iter_mut().find(|r| r.wf.prompt == st) {
139                            r.view = i;
140                        }
141                    })
142            }),
143    );
144    let lines = r.logs[r.view].iter().map(|l| {
145        let color = if l.starts_with('✓') {
146            p.ok
147        } else if l.starts_with('✕') {
148            p.danger
149        } else if l.starts_with('$') {
150            p.fg
151        } else {
152            p.muted
153        };
154        text(l.clone())
155            .font(theme::MONO)
156            .color(color)
157            .wrap()
158            .selectable(true)
159            .w_full()
160    });
161    let mut log = col().padding(8.0).selection_group(true).children(lines);
162    if r.machine.status[r.view] == Status::Running {
163        log = log.child(mono("▍", p.accent));
164    }
165    let dir = r.evidence_dir(r.view);
166    let files = r.evidence[r.view].iter().map(|f| {
167        let path = format!("{dir}/{f}");
168        btn(f, Kind::Ghost, Some(move |s: &mut App| s.open_link(&path)))
169            .min_w(0.0)
170            .max_w(244.0)
171    });
172    let side = col()
173        .w(280.0)
174        .padding(8.0)
175        .gap(8.0)
176        .fill(p.surface)
177        .child(lbl(&format!(
178            "Attached evidence · {}",
179            r.evidence[r.view].len()
180        )))
181        .child(row().gap(4.0).flex_wrap().children(files))
182        .child(
183            mono(
184                s.ws.as_ref()
185                    .map_or(dir.clone(), |w| paths::tilde(&w.resolve(&dir))),
186                p.faint,
187            )
188            .truncate_middle()
189            .min_w(0.0),
190        );
191    col()
192        .grow(1.0)
193        .min_h(180.0)
194        .fill(p.bg)
195        .border_sides([1.0, 0.0, 0.0, 0.0], p.line)
196        .child(tabs)
197        .child(
198            row()
199                .grow(1.0)
200                .min_h(0.0)
201                .child(
202                    col()
203                        .key("log-scroll")
204                        .grow(1.0)
205                        .min_w(0.0)
206                        .h_full()
207                        .scroll(rune::ScrollSpec { stick_bottom: true })
208                        .child(log),
209                )
210                .child(side),
211        )
212}