vig 0.7.0

Git TUI side-by-side diff viewer
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
//! The Actions page: GitHub Actions workflow runs, the jobs and steps of the
//! selected run, and one job's log. Read-only: only `gh run list`, `gh run
//! view` and a GET of the job-log endpoint are ever run.

use crate::actions::domain::client;
use crate::actions::domain::types::{Job, WorkflowRun};
use crate::actions::panes::jobs::{JobsAction, JobsPane};
use crate::actions::panes::log::{LogAction, LogPane};
use crate::actions::panes::runs::{RunsAction, RunsPane};
use crate::core::app::{AppContext, PageState};
use crate::core::config::{Config, LoadedPageConfig};
use crate::core::keymap::{Keymap, ViewAction};
use crate::core::layout::{split_page_frame, PageLayoutConfig};
use crate::core::page::PageAction;
use crate::core::pane::{self, Pane, PaneEvent, PaneSet, PaneShared};
use crate::core::search::SearchState;
use crate::core::ui::status_bar;
use crate::github::domain::client::check_gh_available;
use anyhow::Result;
use crossterm::event::KeyEvent;
use ratatui::{
    layout::Rect,
    style::{Color, Style},
    text::{Line, Span},
    widgets::Paragraph,
    Frame,
};
use std::sync::mpsc;
use std::time::{Duration, Instant};

/// How often the run list (and the selected run's jobs) are re-fetched
/// while something is still queued or running.
pub const ACTIVE_REFRESH_INTERVAL: Duration = Duration::from_secs(5);

/// Pane IDs resolved from the KDL config at construction time.
#[derive(Debug, Clone, Copy)]
pub struct ActionsPaneIds {
    pub runs: usize,
    pub jobs: usize,
    pub log: usize,
}

impl ActionsPaneIds {
    fn from_config(cfg: &LoadedPageConfig) -> Self {
        Self {
            runs: cfg.resolve_id_expect("runs"),
            jobs: cfg.resolve_id_expect("jobs"),
            log: cfg.resolve_id_expect("log"),
        }
    }
}

pub enum ActionsBgMessage {
    Auth(Result<(), String>),
    RunList(Result<Vec<WorkflowRun>, String>),
    Jobs {
        run_id: u64,
        result: Result<Vec<Job>, String>,
    },
    Log {
        request_id: u64,
        append: bool,
        result: Result<Vec<String>, String>,
    },
}

pub struct ActionsPanes {
    pub runs: RunsPane,
    pub jobs: JobsPane,
    pub log: LogPane,
    pub ids: ActionsPaneIds,
}

impl PaneSet for ActionsPanes {
    fn get_mut(&mut self, idx: usize) -> Option<&mut dyn Pane<PaneEvent>> {
        if idx == self.ids.runs {
            Some(&mut self.runs)
        } else if idx == self.ids.jobs {
            Some(&mut self.jobs)
        } else if idx == self.ids.log {
            Some(&mut self.log)
        } else {
            None
        }
    }
}

pub struct ActionsState {
    pub pane: PaneShared,
    pub panes: ActionsPanes,
    /// `None` until `gh auth status` has answered.
    pub gh_available: Option<bool>,
    pub gh_error: Option<String>,
    bg_rx: Option<mpsc::Receiver<ActionsBgMessage>>,
    bg_tx: Option<mpsc::Sender<ActionsBgMessage>>,
    initialized: bool,
    last_list_refresh: Option<Instant>,
    last_jobs_refresh: Option<Instant>,
    layout_config: PageLayoutConfig,
    view_keymap: Keymap<ViewAction>,
}

impl pane::PageLayout for ActionsState {
    type Panes = ActionsPanes;
    fn page_parts_mut(
        &mut self,
    ) -> (
        &mut PaneShared,
        &mut Self::Panes,
        &Keymap<ViewAction>,
        &PageLayoutConfig,
    ) {
        (
            &mut self.pane,
            &mut self.panes,
            &self.view_keymap,
            &self.layout_config,
        )
    }
}

impl ActionsState {
    pub fn new(cfg: &Config) -> Result<Self> {
        let page_cfg = cfg.actions_page()?;
        let ids = ActionsPaneIds::from_config(&page_cfg);
        // Validates the bind declaration (runs → jobs).
        let _ = page_cfg.resolve_select_bindings();

        let runs_km = page_cfg.keymap::<RunsAction>("runs")?;
        let jobs_km = page_cfg.keymap::<JobsAction>("jobs")?;
        let log_km = page_cfg.keymap::<LogAction>("log")?;
        let view_km = page_cfg.keymap::<ViewAction>("view")?;

        let mut runs = RunsPane::new(ids.runs, ids.jobs, ids.log);
        runs.set_keymap(runs_km);
        let mut jobs = JobsPane::new(ids.jobs, ids.log);
        jobs.set_keymap(jobs_km);
        let mut log = LogPane::new(ids.log);
        log.set_keymap(log_km);

        Ok(Self {
            pane: PaneShared {
                focused_pane: ids.runs,
                previous_pane: ids.runs,
                search: SearchState::new(),
            },
            panes: ActionsPanes {
                runs,
                jobs,
                log,
                ids,
            },
            gh_available: None,
            gh_error: None,
            bg_rx: None,
            bg_tx: None,
            initialized: false,
            last_list_refresh: None,
            last_jobs_refresh: None,
            layout_config: page_cfg.layout,
            view_keymap: view_km,
        })
    }

    /// First switch to the page: check `gh`, show the cached list, fetch.
    fn initialize(&mut self) {
        if self.initialized {
            return;
        }
        self.initialized = true;
        let (tx, rx) = mpsc::channel();
        self.bg_tx = Some(tx);
        self.bg_rx = Some(rx);
        self.check_auth();
        if let Some(runs) = client::load_run_list() {
            self.panes.runs.set_runs(runs);
            self.sync_jobs();
        }
        self.spawn_runs();
    }

    fn check_auth(&mut self) {
        let Some(tx) = self.bg_tx.clone() else {
            return;
        };
        std::thread::spawn(move || {
            let _ = tx.send(ActionsBgMessage::Auth(check_gh_available()));
        });
    }

    fn spawn_runs(&mut self) {
        let Some(tx) = self.bg_tx.clone() else {
            return;
        };
        self.last_list_refresh = Some(Instant::now());
        self.panes.runs.set_loading(true);
        std::thread::spawn(move || {
            let _ = tx.send(ActionsBgMessage::RunList(client::list_runs(
                client::RUN_LIST_LIMIT,
            )));
        });
    }

    /// `r`: re-check `gh`, re-fetch the runs, the jobs and the log.
    fn refresh(&mut self) {
        self.gh_error = None;
        if self.gh_available == Some(false) {
            self.gh_available = None;
        }
        self.check_auth();
        self.spawn_runs();
        if let Some(tx) = self.bg_tx.clone() {
            self.last_jobs_refresh = Some(Instant::now());
            self.panes.jobs.spawn_fetch(&tx);
            self.panes.log.refresh(&tx);
        }
    }

    /// Point the jobs pane at the selected run.
    fn sync_jobs(&mut self) {
        let Some(tx) = self.bg_tx.clone() else {
            return;
        };
        let selected = self.panes.runs.selected().cloned();
        let changed = self.panes.jobs.run_id() != selected.as_ref().map(|r| r.id);
        self.panes.jobs.load(selected.as_ref(), &tx);
        if changed {
            self.last_jobs_refresh = Some(Instant::now());
            // The log belonged to a job of the previous run.
            self.panes.log.clear();
        }
    }

    /// Load the log of the job selected in the jobs pane (a step row
    /// scrolls to that step once the log is in).
    fn open_selected_log(&mut self) {
        let Some(tx) = self.bg_tx.clone() else {
            return;
        };
        if let Some((target, step)) = self.panes.jobs.selected_target() {
            self.panes.log.load(target, step, &tx);
        }
    }

    /// After a jobs refresh, tell the log pane whether its job finished.
    fn sync_log_target(&mut self) {
        let Some(tx) = self.bg_tx.clone() else {
            return;
        };
        let job_id = self.panes.log.target().map(|t| t.job_id);
        if let Some(latest) = job_id.and_then(|id| self.panes.jobs.target_for(id)) {
            self.panes.log.update_target(latest, &tx);
        }
    }

    fn drain_bg_messages(&mut self) {
        let messages: Vec<_> = match &self.bg_rx {
            Some(rx) => rx.try_iter().collect(),
            None => return,
        };
        for msg in messages {
            match msg {
                ActionsBgMessage::Auth(result) => match result {
                    Ok(()) => {
                        self.gh_available = Some(true);
                    }
                    Err(e) => {
                        self.gh_available = Some(false);
                        self.gh_error = Some(e);
                        self.panes.runs.set_loading(false);
                    }
                },
                ActionsBgMessage::RunList(result) => {
                    self.panes.runs.set_loading(false);
                    match result {
                        Ok(runs) => {
                            client::save_run_list(&runs);
                            self.panes.runs.set_runs(runs);
                            self.sync_jobs();
                        }
                        Err(e) => self.note_error(e),
                    }
                }
                ActionsBgMessage::Jobs { run_id, result } => {
                    self.panes.jobs.apply(run_id, result);
                    self.sync_log_target();
                }
                ActionsBgMessage::Log {
                    request_id,
                    append,
                    result,
                } => {
                    self.panes.log.apply(request_id, append, result);
                }
            }
        }
    }

    fn note_error(&mut self, e: String) {
        if self.gh_error.is_none() {
            self.gh_error = Some(e);
        }
    }

    fn process_events(
        &mut self,
        ctx: &mut AppContext,
        events: Vec<PaneEvent>,
    ) -> Result<PageAction> {
        let ids = self.panes.ids;
        for event in events {
            if pane::process_common_event(&mut self.pane, ctx, &event) {
                continue;
            }
            match event {
                PaneEvent::SetFocus(id) if id == ids.jobs => {
                    self.sync_jobs();
                }
                PaneEvent::SetFocus(id) if id == ids.log => {
                    self.open_selected_log();
                }
                PaneEvent::SelectionChanged if self.pane.focused_pane == ids.runs => {
                    self.sync_jobs();
                }
                PaneEvent::JumpToMatch(forward) => {
                    if let Some(origin) =
                        self.pane
                            .jump_to_search_match(&mut self.panes, ctx, forward)
                    {
                        if origin == ids.runs {
                            self.sync_jobs();
                        }
                    }
                }
                _ => {}
            }
        }
        Ok(PageAction::None)
    }

    fn handle_view_key(&mut self, ctx: &mut AppContext, key: KeyEvent) -> Result<PageAction> {
        if let Some(action) = self.view_keymap.lookup(key) {
            if let Some(page_action) = pane::execute_common_view_action(ctx, *action) {
                return Ok(page_action);
            }
            if *action == ViewAction::Refresh {
                self.refresh();
                return Ok(PageAction::None);
            }
        }
        let events = pane::dispatch_page_key(self, key);
        self.process_events(ctx, events)
    }

    /// Summary for the status bar: `(runs, active runs)`.
    pub fn counts(&self) -> (usize, usize) {
        self.panes.runs.counts()
    }

    pub fn is_updating(&self) -> bool {
        self.panes.runs.is_loading() || self.panes.jobs.is_loading() || self.panes.log.is_loading()
    }

    fn render_unavailable(&self, f: &mut Frame, area: Rect) {
        let err = self.gh_error.as_deref().unwrap_or("gh not found");
        let lines = vec![
            Line::default(),
            Line::from(Span::styled(
                format!("  gh not available: {err}"),
                Style::default().fg(Color::DarkGray),
            )),
            Line::from(Span::styled(
                "  Install the GitHub CLI and run `gh auth login`, then press r.",
                Style::default().fg(Color::DarkGray),
            )),
        ];
        f.render_widget(Paragraph::new(lines), area);
    }
}

impl PageState for ActionsState {
    fn id(&self) -> &'static str {
        "actions"
    }

    fn label(&self) -> &'static str {
        "Actions"
    }

    fn help_bindings(&self) -> Vec<(String, String)> {
        use crate::core::keymap::help_section;
        let s = |k: &str, v: &str| (k.to_string(), v.to_string());
        let mut entries = vec![s("1 … 7", "Switch view")];
        entries.extend(self.view_keymap.help_entries());
        entries.extend(help_section("Runs"));
        entries.extend(self.panes.runs.keymap().help_entries());
        entries.extend(help_section("Jobs"));
        entries.extend(self.panes.jobs.keymap().help_entries());
        entries.extend(help_section("Log"));
        entries.extend(self.panes.log.keymap().help_entries());
        entries
    }

    fn handle_key(&mut self, ctx: &mut AppContext, key: KeyEvent) -> Result<PageAction> {
        if self.pane.handle_search_input(&mut self.panes, ctx, key) {
            // A confirmed search moves the list selection without an event.
            let origin = self.pane.search.origin;
            if !self.pane.search.active && origin == self.panes.ids.runs {
                self.sync_jobs();
            }
            return Ok(PageAction::None);
        }
        self.handle_view_key(ctx, key)
    }

    fn render(&mut self, f: &mut Frame, ctx: &AppContext, area: Rect) {
        let frame = split_page_frame(area);
        status_bar::render_actions_header(f, ctx, frame.header);
        if self.gh_available == Some(false) {
            self.render_unavailable(f, frame.content);
        } else {
            pane::render_page_content(self, f, ctx, frame.content);
        }
        status_bar::render_actions_status_bar(f, ctx, self, frame.status_bar);
    }

    fn intercepts_all_keys(&self) -> bool {
        self.pane.search.active
    }

    fn on_tick(&mut self, _ctx: &mut AppContext) {
        if !self.initialized || self.gh_available != Some(true) {
            return;
        }
        let Some(tx) = self.bg_tx.clone() else {
            return;
        };
        let due = |t: Option<Instant>| t.is_none_or(|t| t.elapsed() >= ACTIVE_REFRESH_INTERVAL);
        if self.panes.runs.has_active()
            && due(self.last_list_refresh)
            && !self.panes.runs.is_loading()
        {
            self.spawn_runs();
        }
        if self.panes.jobs.run_is_active()
            && due(self.last_jobs_refresh)
            && !self.panes.jobs.is_loading()
        {
            self.last_jobs_refresh = Some(Instant::now());
            self.panes.jobs.spawn_fetch(&tx);
        }
        self.panes.log.on_tick(&tx);
    }

    fn on_activate(&mut self, _ctx: &mut AppContext) {
        self.initialize();
    }

    fn drain_background(&mut self) {
        self.drain_bg_messages();
    }
}

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

    #[test]
    fn actions_page_builds_from_builtin_config() {
        let cfg = Config::builtin();
        let state = ActionsState::new(&cfg).expect("actions page");
        assert_eq!(state.id(), "actions");
        assert_eq!(state.label(), "Actions");
        assert_eq!(state.pane.focused_pane, state.panes.ids.runs);
        assert_eq!(
            state.layout_config.tab_panes,
            vec![
                state.panes.ids.runs,
                state.panes.ids.jobs,
                state.panes.ids.log
            ]
        );
        let help = state.help_bindings();
        assert_eq!(help[0].0, "1 … 7");
        assert!(help.iter().any(|(_, d)| d.contains("Log")));
        assert!(help.iter().any(|(k, _)| k == "]"));
    }
}