paperboy 0.5.5

A Rust TUI API tester
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
//! "Save to Git" wizard — the terminal UI's presentation of
//! [`crate::save_flow`].
//!
//! The steps, the background work and the push rules all live in the shared
//! flow, which the GUI drives too. What remains here is the terminal UI's own
//! half: the [`Editor`]s behind each field, which field has focus, the branch
//! dropdown selection, and the drawing.
//!
//! Only ever offered for an item that already remembers where it came from
//! (`Collection::git_origin`, `workspace_git_origin`, or `Report::git_origin`).

use ratatui::Frame;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Clear, List, ListItem, Paragraph, Wrap};

use crate::collection::Collection;
use crate::environment::Environment;
use crate::i18n::Strings;
use crate::save_flow::{SaveFlow, SaveSource, SaveTargetKind, Step};

use super::app::{MouseHitTarget, MouseLayer, TuiApp};
use super::draw::*;
use super::editor::*;
use super::theme::*;
use crate::remote_flow::WorkspaceGitOrigin;

/// Which step of the wizard is on screen.
///
/// Carries no data: it is derived from the shared flow by
/// [`GitSaveWizard::stage`], so the drawing can never show one step while the
/// flow believes it is on another.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum GitSaveStage {
    Connect,
    ChoosePaths,
    ChooseTarget,
    CommitMessage,
    Pushing,
    Done,
    Error,
}

/// The terminal UI's "save to git" overlay.
pub(crate) struct GitSaveWizard {
    /// The shared state machine. The GUI drives the same one, so the two
    /// front-ends cannot disagree about how a save behaves.
    pub(crate) flow: SaveFlow,
    /// A snapshot of the collection's effective environment (linked, or
    /// active-global) taken when the wizard opened — used to build the pushed
    /// `.vars` and to know whether to offer the step at all.
    pub(crate) env: Option<Environment>,
    pub(crate) url: Editor,
    pub(crate) token: Editor,
    pub(crate) collection_path: Editor,
    pub(crate) env_path: Editor,
    pub(crate) target_name: Editor,
    pub(crate) commit_msg: Editor,
    /// Which field has focus on the current step.
    pub(crate) field: u8,
    /// The open branch dropdown's selection, or `None` when it is closed.
    pub(crate) sel: Option<usize>,
}

impl GitSaveWizard {
    /// Build a wizard for collection `ci`. `env` is the collection's effective
    /// environment, if it has one. Panics if `col` has no `git_origin` —
    /// callers gate "Save to Git" on that first.
    pub(crate) fn new(ci: usize, col: &Collection, env: Option<Environment>) -> Self {
        let flow = SaveFlow::for_collection(ci, col, env.as_ref());
        Self::from_flow(flow, env)
    }

    /// Build a wizard for pushing a whole git-loaded workspace tab back.
    pub(crate) fn new_workspace(ci: usize, col: &Collection, origin: &WorkspaceGitOrigin) -> Self {
        Self::from_flow(SaveFlow::for_workspace(ci, col, origin), None)
    }

    /// Build a wizard for pushing a PaperTrail report back.
    pub(crate) fn new_report(report_idx: usize, report: &crate::report::Report) -> Self {
        Self::from_flow(SaveFlow::for_report(report_idx, report), None)
    }

    /// Wrap a seeded flow in the editors that present it.
    fn from_flow(flow: SaveFlow, env: Option<Environment>) -> Self {
        Self {
            url: Editor::new(&flow.url, false),
            token: Editor::blank(),
            collection_path: Editor::new(&flow.path, false),
            env_path: Editor::new(&flow.env_path, false),
            target_name: Editor::new(&flow.target_name, false),
            commit_msg: Editor::new(&flow.message, false),
            field: 0,
            sel: None,
            env,
            flow,
        }
    }

    /// Copy what the user has typed into the flow, so every decision the flow
    /// makes is based on the text actually on screen. Call before handing it
    /// anything to act on.
    pub(crate) fn sync(&mut self) {
        self.flow.url = self.url.text();
        self.flow.token = self.token.text();
        self.flow.path = self.collection_path.text();
        self.flow.env_path = self.env_path.text();
        self.flow.target_name = self.target_name.text();
        self.flow.message = self.commit_msg.text();
    }

    /// The step to draw, derived from the shared flow.
    pub(crate) fn stage(&self) -> GitSaveStage {
        match self.flow.step() {
            Step::Failed(_) => GitSaveStage::Error,
            Step::Done => GitSaveStage::Done,
            _ if self.flow.is_busy() && !self.on_ref_step() => GitSaveStage::Pushing,
            Step::Connect => GitSaveStage::Connect,
            Step::ChoosePaths => GitSaveStage::ChoosePaths,
            Step::ChooseTarget => GitSaveStage::ChooseTarget,
            Step::CommitMessage => GitSaveStage::CommitMessage,
        }
    }

    /// The ref listing is fetched *while* the user is already choosing a
    /// target, so that wait mustn't be shown as a blocking "pushing" screen.
    fn on_ref_step(&self) -> bool {
        matches!(self.flow.step(), Step::ChooseTarget)
    }

    /// Whether the "also save the environment" step has anything to offer.
    pub(crate) fn has_env(&self) -> bool {
        self.env.is_some() && self.flow.source.can_include_env()
    }

    /// The error text to show on [`GitSaveStage::Error`].
    pub(crate) fn error_text(&self) -> &str {
        self.flow.error().unwrap_or_default()
    }
}

pub(crate) fn draw_git_save_wizard_with_hits(
    f: &mut Frame,
    w: &GitSaveWizard,
    s: &Strings,
    th: &Theme,
    app: Option<&TuiApp>,
) {
    if let Some(app) = app {
        app.set_mouse_layer(MouseLayer::Overlay);
    }
    let title = s.git_save_title;
    match w.stage() {
        GitSaveStage::Connect => {
            let field = w.field;
            // Same shape as the load wizard and the request editor: a label
            // column, the value beside it, the keys on the border.
            let fields = [
                (s.git_url_label, s.git_url_hint, &w.url, false),
                (s.git_token_label, s.git_token_hint, &w.token, true),
            ];
            let label_w = label_column(fields.iter().map(|(l, _, _, _)| *l));
            let area = centered_rect(74, 4, f.area());
            f.render_widget(Clear, area);
            let block = panel_hinted(title.to_string(), s.git_connect_hint, th);
            let inner = block.inner(area);
            f.render_widget(block, area);
            let rows =
                Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).split(inner);
            for (i, (label, placeholder, ed, mask)) in fields.iter().enumerate() {
                let cols = Layout::horizontal([Constraint::Length(label_w), Constraint::Min(1)])
                    .split(rows[i]);
                f.render_widget(
                    Paragraph::new(Span::styled(*label, Style::default().fg(th.accent))),
                    cols[0],
                );
                render_line_field_hinted(f, cols[1], ed, field == i as u8, *mask, placeholder, th);
                if let Some(app) = app {
                    app.push_mouse_hit(
                        MouseLayer::Overlay,
                        rows[i],
                        MouseHitTarget::GitSaveWizardRow(i),
                    );
                }
            }
        }
        GitSaveStage::ChoosePaths => {
            let field = w.field;
            let rows_n: u16 = if w.has_env() { 8 } else { 4 };
            let area = centered_rect(76, rows_n + 3, f.area());
            f.render_widget(Clear, area);
            let block = panel_hinted(title.to_string(), s.git_save_step_hint, th);
            let inner = block.inner(area);
            f.render_widget(block, area);
            let mut constraints = vec![Constraint::Length(1), Constraint::Length(1)]; // collection path label+field
            if w.has_env() {
                constraints.push(Constraint::Length(1)); // spacer
                constraints.push(Constraint::Length(1)); // checkbox
                if w.flow.include_env {
                    constraints.push(Constraint::Length(1)); // env path label
                    constraints.push(Constraint::Length(1)); // env path field
                }
            }
            let rows = Layout::vertical(constraints).split(inner);
            f.render_widget(
                Paragraph::new(Span::styled(
                    if matches!(w.flow.source, SaveSource::Report { .. }) {
                        s.git_save_report_path_label
                    } else {
                        s.git_save_collection_path_label
                    },
                    Style::default().fg(th.accent),
                )),
                rows[0],
            );
            render_line_field(f, rows[1], &w.collection_path, field == 0, false, th);
            if let Some(app) = app {
                app.push_mouse_hit(
                    MouseLayer::Overlay,
                    rows[1],
                    MouseHitTarget::GitSaveWizardRow(0),
                );
            }
            if w.has_env() {
                let mark = if w.flow.include_env { "[x]" } else { "[ ]" };
                let cb_style = if field == 1 {
                    Style::default()
                        .bg(th.accent)
                        .fg(th.bg)
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::default().fg(th.text)
                };
                f.render_widget(
                    Paragraph::new(Line::styled(
                        format!("{mark} {}", s.git_save_include_env_label),
                        cb_style,
                    )),
                    rows[3],
                );
                if let Some(app) = app {
                    app.push_mouse_hit(
                        MouseLayer::Overlay,
                        rows[3],
                        MouseHitTarget::GitSaveWizardRow(1),
                    );
                }
                if w.flow.include_env {
                    f.render_widget(
                        Paragraph::new(Span::styled(
                            s.git_save_env_path_label,
                            Style::default().fg(th.accent),
                        )),
                        rows[4],
                    );
                    render_line_field(f, rows[5], &w.env_path, field == 2, false, th);
                    if let Some(app) = app {
                        app.push_mouse_hit(
                            MouseLayer::Overlay,
                            rows[5],
                            MouseHitTarget::GitSaveWizardRow(2),
                        );
                    }
                }
            }
        }
        GitSaveStage::ChooseTarget => {
            let sel = w.sel;
            let refs = w.flow.refs();
            let branches = &refs.branches;
            let dropdown_rows = if sel.is_some() {
                branches.len().min(5) as u16
            } else {
                0
            };
            let area = centered_rect(74, 5 + dropdown_rows, f.area());
            f.render_widget(Clear, area);
            let block = panel_hinted(title.to_string(), s.git_save_target_hint, th);
            let inner = block.inner(area);
            f.render_widget(block, area);
            let rows = Layout::vertical([
                Constraint::Length(1),             // kind toggle
                Constraint::Length(1),             // name label
                Constraint::Length(1),             // name field
                Constraint::Length(dropdown_rows), // existing-branches dropdown
            ])
            .split(inner);
            let (branch_style, tag_style) = if w.flow.target_kind == SaveTargetKind::Branch {
                (
                    Style::default()
                        .bg(th.accent)
                        .fg(th.bg)
                        .add_modifier(Modifier::BOLD),
                    Style::default().fg(th.dim),
                )
            } else {
                (
                    Style::default().fg(th.dim),
                    Style::default()
                        .bg(th.accent)
                        .fg(th.bg)
                        .add_modifier(Modifier::BOLD),
                )
            };
            f.render_widget(
                Paragraph::new(Line::from(vec![
                    Span::styled(format!(" {} ", s.git_save_branch_label), branch_style),
                    Span::raw(" "),
                    Span::styled(format!(" {} ", s.git_save_tag_label), tag_style),
                ])),
                rows[0],
            );
            if let Some(app) = app {
                app.push_mouse_hit(
                    MouseLayer::Overlay,
                    rows[0],
                    MouseHitTarget::GitSaveWizardRow(0),
                );
            }
            f.render_widget(Paragraph::new(Span::raw("")), rows[1]);
            render_line_field(f, rows[2], &w.target_name, sel.is_none(), false, th);
            if let Some(app) = app {
                app.push_mouse_hit(
                    MouseLayer::Overlay,
                    rows[2],
                    MouseHitTarget::GitSaveWizardRow(1),
                );
            }
            if dropdown_rows > 0 {
                let items: Vec<ListItem> = branches
                    .iter()
                    .take(5)
                    .enumerate()
                    .map(|(i, b)| {
                        let style = if sel == Some(i) {
                            Style::default()
                                .bg(th.accent)
                                .fg(th.bg)
                                .add_modifier(Modifier::BOLD)
                        } else {
                            Style::default().fg(th.dim)
                        };
                        ListItem::new(Line::styled(b.clone(), style))
                    })
                    .collect();
                f.render_widget(List::new(items), rows[3]);
                if let Some(app) = app {
                    for i in 0..branches.len().min(5) {
                        app.push_mouse_hit(
                            MouseLayer::Overlay,
                            Rect::new(rows[3].x, rows[3].y + i as u16, rows[3].width, 1),
                            MouseHitTarget::GitSaveWizardRow(10 + i),
                        );
                    }
                }
            }
        }
        GitSaveStage::CommitMessage => {
            let area = centered_rect(74, 3, f.area());
            f.render_widget(Clear, area);
            let block = panel_hinted(title.to_string(), s.git_save_commit_msg_hint, th);
            let inner = block.inner(area);
            f.render_widget(block, area);
            let label_w = label_column([s.git_save_commit_msg_label]);
            let cols =
                Layout::horizontal([Constraint::Length(label_w), Constraint::Min(1)]).split(inner);
            f.render_widget(
                Paragraph::new(Span::styled(
                    s.git_save_commit_msg_label,
                    Style::default().fg(th.accent),
                )),
                cols[0],
            );
            render_line_field(f, cols[1], &w.commit_msg, true, false, th);
            if let Some(app) = app {
                app.push_mouse_hit(
                    MouseLayer::Overlay,
                    inner,
                    MouseHitTarget::GitSaveWizardRow(0),
                );
            }
        }
        GitSaveStage::Pushing => {
            let width = (s.git_save_pushing.len() as u16 + 4).min(f.area().width);
            let area = centered_rect(width, 3, f.area());
            f.render_widget(Clear, area);
            let block = panel(title.to_string(), true, th);
            let inner = block.inner(area);
            f.render_widget(block, area);
            f.render_widget(
                Paragraph::new(Span::styled(
                    s.git_save_pushing,
                    Style::default().fg(th.text).add_modifier(Modifier::BOLD),
                )),
                inner,
            );
        }
        GitSaveStage::Done => {
            let width = (s.git_save_success.len() as u16 + 4).min(f.area().width);
            let area = centered_rect(width, 3, f.area());
            f.render_widget(Clear, area);
            let block = panel(title.to_string(), true, th);
            let inner = block.inner(area);
            f.render_widget(block, area);
            f.render_widget(
                Paragraph::new(Span::styled(
                    s.git_save_success,
                    Style::default().fg(th.text).add_modifier(Modifier::BOLD),
                )),
                inner,
            );
        }
        GitSaveStage::Error => {
            let e = w.error_text();
            let width = (f.area().width * 6 / 10).max(40);
            let area = centered_rect(width, 7, f.area());
            f.render_widget(Clear, area);
            let block = panel_hinted(title.to_string(), s.git_error_hint, th);
            let inner = block.inner(area);
            f.render_widget(block, area);
            f.render_widget(
                Paragraph::new(e.to_string())
                    .style(Style::default().fg(th.err))
                    .wrap(Wrap { trim: true }),
                inner,
            );
        }
    }
}