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
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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
//! The terminal UI's "import a whole Postman workspace" wizard.
//!
//! Every decision about *what happens next* lives in [`crate::postman_flow`],
//! shared with the GUI so the two front-ends cannot drift. What remains here is
//! the terminal's presentation of it: which step is on screen, the text editors
//! with their cursors, and the highlighted row.

use super::listscroll::ListScroll;
use std::path::{Path, PathBuf};

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

use crate::i18n::Strings;
use crate::postman_flow::{
    KeySource, PostmanFlow, Step, default_dest_name, human_duration, item_kind_label,
    plan_skipped_line, plan_summary,
};
use crate::postman_import::ImportFormat;

use super::draw::*;
use super::editor::*;
use super::theme::*;

/// Which step the terminal is drawing.
///
/// A *view* of [`PostmanFlow`]'s state, derived fresh each time rather than
/// stored — an in-flight operation and an error both take precedence over the
/// underlying step, because that is what the user needs to see.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PostmanStage {
    Connect,
    Loading,
    PickWorkspace,
    Options,
    Confirm,
    Downloading,
    Done,
    Error,
}

/// Which cell has focus on the Options step. Ordered top to bottom so Tab and
/// the arrow keys can simply add or subtract one.
pub(crate) const OPTION_ROWS: usize = 6;

/// The Connect step's focusable rows: the key source, the key, the workspace
/// id and the API host.
pub(crate) const POSTMAN_CONNECT_FIELDS: u8 = 4;

/// The wizard overlay's own state: the editors, the focused field and the
/// workspace list's highlighted row.
pub(crate) struct PostmanWizard {
    pub(crate) flow: PostmanFlow,
    /// What the key field is asking for — the key itself, or the address of
    /// the place it is kept. The reference syntax is assembled from this and
    /// the field's text (see [`KeySource::reference`]) so nobody has to type
    /// `{{ op://… }}` from memory.
    pub(crate) key_source: KeySource,
    pub(crate) key: Editor,
    pub(crate) workspace_ref: Editor,
    pub(crate) base_url: Editor,
    /// The import destination. Not an [`Editor`]: it is chosen in the file
    /// browser (`FileAction::PostmanDestChooseFolder`) like every other
    /// "save into a folder" in the app, never typed here.
    pub(crate) dest: PathBuf,
    /// Connect step: 0 = key source, 1 = key, 2 = workspace id, 3 = API host.
    pub(crate) field: u8,
    /// Options step: which row is focused (0 = destination, 1 = collections,
    /// 2 = environments, 3 = format, 4 = overwrite, 5 = the Import button).
    pub(crate) option_row: usize,
    /// The step a failure interrupted, so dismissing the error returns there
    /// rather than throwing the whole wizard back to the key prompt.
    pub(crate) before_error: Step,
    /// Where the workspace list is scrolled to, carried between frames (see
    /// [`ListScroll`]).
    pub(crate) list_scroll: ListScroll,
    /// How far down an open collection preview has been scrolled. Lives here
    /// rather than in the flow because it is a property of this screen, not of
    /// what was fetched — the GUI scrolls the same preview with its own.
    pub(crate) preview_scroll: usize,
    /// Key *references* used before, most recent first, straight from the
    /// session. Filtered to the chosen source before being shown, so switching
    /// to SSM doesn't offer 1Password paths.
    pub(crate) recent: Vec<String>,
    /// `Some` while the recent-keys dropdown has keyboard focus, indexing into
    /// [`PostmanWizard::recent_matches`].
    pub(crate) recent_sel: Option<usize>,
    /// Set by Esc, which dismisses the dropdown without leaving the field.
    /// Cleared by anything that changes what the list would show -- typing,
    /// switching source, or coming back to the field -- so the suggestions are
    /// never gone for good, exactly as the request wizard's dropdown behaves.
    pub(crate) recent_hidden: bool,
    /// What a finished import landed, kept so the receipt can say it rather
    /// than only naming the folder it wrote.
    pub(crate) done: Option<crate::postman_import::ImportSummary>,
}

impl PostmanWizard {
    pub(crate) fn new(recent: Vec<String>) -> Self {
        // An API key in the environment is the one credential a user is likely
        // to already have to hand, and typing a PMAK by hand is miserable.
        let flow = PostmanFlow::new().with_env_key();
        // A seeded key is a pasted one; `detect` keeps that honest if the
        // seeding ever grows to understand references.
        let (key_source, entry) = KeySource::detect(&flow.key);
        Self {
            key_source,
            key: Editor::new(&entry, false),
            workspace_ref: Editor::blank(),
            base_url: Editor::blank(),
            dest: PathBuf::new(),
            flow,
            field: 0,
            option_row: 0,
            before_error: Step::Connect,
            list_scroll: ListScroll::default(),
            preview_scroll: 0,
            recent,
            recent_sel: None,
            recent_hidden: false,
            done: None,
        }
    }

    /// The remembered entries that belong to the source now chosen, as the
    /// field's own text — the `op://…` path, not the `{{ … }}` wrapper.
    pub(crate) fn recent_entries(&self) -> Vec<String> {
        self.recent
            .iter()
            .filter_map(|raw| {
                let (src, entry) = KeySource::detect(raw);
                (src == self.key_source && !entry.is_empty()).then_some(entry)
            })
            .collect()
    }

    /// The remembered entries the dropdown is actually offering: those for
    /// this source, narrowed to what the field already holds. Typing therefore
    /// filters the list rather than leaving a dozen paths on screen that no
    /// longer match, and a field holding one of them exactly has nothing left
    /// to suggest.
    pub(crate) fn recent_matches(&self) -> Vec<String> {
        let typed = self.key.text();
        let typed = typed.trim().to_lowercase();
        self.recent_entries()
            .into_iter()
            .filter(|e| {
                let e = e.to_lowercase();
                e != typed && (typed.is_empty() || e.contains(&typed))
            })
            .collect()
    }

    /// Whether the dropdown is on screen: the key field has focus, it has not
    /// been dismissed, and there is something to offer.
    pub(crate) fn recent_open(&self) -> bool {
        self.field == 1 && !self.recent_hidden && !self.recent_matches().is_empty()
    }

    pub(crate) fn stage(&self) -> PostmanStage {
        if self.flow.error().is_some() {
            return PostmanStage::Error;
        }
        // Planning happens while the Confirm step is already on screen, so a
        // busy flow only means "Loading" when there is nothing else to show.
        match self.flow.step() {
            Step::Connect => PostmanStage::Connect,
            Step::PickWorkspace => {
                if self.flow.is_busy() {
                    PostmanStage::Loading
                } else {
                    PostmanStage::PickWorkspace
                }
            }
            Step::Options => PostmanStage::Options,
            Step::Confirm => {
                if self.flow.plan().is_none() {
                    PostmanStage::Loading
                } else {
                    PostmanStage::Confirm
                }
            }
            Step::Downloading => PostmanStage::Downloading,
            Step::Done => PostmanStage::Done,
            Step::Failed(_) => PostmanStage::Error,
        }
    }

    /// Copy the on-screen editors into the flow, so the flow never has to know
    /// what an [`Editor`] is.
    pub(crate) fn sync_fields(&mut self) {
        self.flow.key = self.key_source.reference(&self.key.text());
        self.flow.workspace_ref = self.workspace_ref.text();
        self.flow.base_url = self.base_url.text();
        self.flow.dest = self.dest.to_string_lossy().into_owned();
    }

    /// Fill the destination in from the chosen workspace's name, unless one has
    /// already been picked — a suggestion, never an override.
    /// `base` is where the last import landed, if there was one: workspaces are
    /// downloaded to be kept together, so the folder the user chose last time
    /// is a far better guess than the directory the app was started from.
    pub(crate) fn suggest_dest(&mut self, base: Option<&Path>) {
        if !self.dest.as_os_str().is_empty() {
            return;
        }
        let base = base
            .filter(|p| p.is_dir())
            .map(Path::to_owned)
            .or_else(|| std::env::current_dir().ok())
            .unwrap_or_else(|| PathBuf::from("."));
        self.set_dest(base.join(default_dest_name(self.flow.workspace_name())));
    }

    /// Record a destination chosen in the browser, keeping `flow.dest` in step.
    pub(crate) fn set_dest(&mut self, path: PathBuf) {
        self.flow.dest = path.to_string_lossy().into_owned();
        self.dest = path;
    }

    /// The leaf folder name to seed the browser's inline name editor with, so
    /// reopening the picker offers back what is already chosen.
    pub(crate) fn dest_folder_name(&self) -> String {
        self.dest
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| default_dest_name(self.flow.workspace_name()))
    }

    /// Where the picker should open: the destination's parent, if it exists.
    pub(crate) fn dest_parent(&self) -> Option<PathBuf> {
        self.dest
            .parent()
            .filter(|p| p.is_dir())
            .map(Path::to_owned)
    }

    /// Remember where a failure came from, so the error screen can go back.
    pub(crate) fn remember_step(&mut self) {
        if !matches!(self.flow.step(), Step::Failed(_)) {
            self.before_error = self.flow.step().clone();
        }
    }
}

/// The Options step's rows, in focus order. Kept next to the drawing code
/// because the row *is* the label.
pub(crate) fn option_labels(w: &PostmanWizard, s: &Strings) -> Vec<String> {
    let check = |on: bool| if on { "[x]" } else { "[ ]" };
    let format = match w.flow.format {
        ImportFormat::Raw => s.postman_format_raw,
        ImportFormat::Hurl => s.postman_format_hurl,
    };
    vec![
        format!("{}: {}", s.postman_dest_label, w.dest.display()),
        format!(
            "{} {}",
            check(w.flow.include_collections),
            s.postman_include_collections
        ),
        format!(
            "{} {}",
            check(w.flow.include_environments),
            s.postman_include_environments
        ),
        format!("{}: {format}", s.postman_format_label),
        format!("{} {}", check(w.flow.overwrite), s.postman_overwrite),
        s.postman_start.to_string(),
    ]
}

pub(crate) fn draw_postman_wizard(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme) {
    let title = s.postman_title;
    match w.stage() {
        PostmanStage::Connect => draw_connect(f, w, s, th, title),
        PostmanStage::Loading => draw_loading(f, w, s, th, title),
        PostmanStage::PickWorkspace => draw_pick_workspace(f, w, s, th),
        PostmanStage::Options => draw_options(f, w, s, th, title),
        PostmanStage::Confirm => draw_confirm(f, w, s, th, title),
        PostmanStage::Downloading => draw_downloading(f, w, s, th, title),
        PostmanStage::Done => draw_done(f, w, s, th),
        PostmanStage::Error => draw_error(f, w, s, th, title),
    }
}

/// How many lines `text` takes when wrapped to `width` columns, the way
/// [`Paragraph`]'s word wrap does it. Used to size a hint's row before it is
/// drawn, so a long sentence (or a longer translation of it) grows the dialog
/// instead of being cut off at the panel edge.
fn wrapped_height(text: &str, width: u16) -> u16 {
    if width == 0 {
        return 1;
    }
    let width = width as usize;
    let mut lines = 1u16;
    let mut col = 0usize;
    for word in text.split_whitespace() {
        let len = word.chars().count();
        if col == 0 {
            col = len;
        } else if col + 1 + len <= width {
            col += 1 + len;
        } else {
            lines += 1;
            col = len;
        }
        // A word longer than the line (a URL) spills onto further lines.
        while col > width {
            lines += 1;
            col -= width;
        }
    }
    lines
}

fn draw_connect(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme, title: &str) {
    // Laid out like the request wizard: a label column on the left, the value
    // beside it, and the keys on the border. Four short rows say as much as
    // four fields did with a paragraph of explanation under each — what a
    // field wants is shown *in* the field, as a dim example, where the answer
    // is about to be typed.
    let fields: [(&str, &str, u8); 4] = [
        (s.postman_key_source_label, "", 0),
        (w.key_source.field_label(s), w.key_source.field_hint(s), 1),
        (s.postman_workspace_label, s.postman_workspace_hint, 2),
        (s.postman_base_url_label, s.postman_base_url_hint, 3),
    ];
    // Width the column against *every* label it could ever hold, not just the
    // ones showing: the key row's label changes with the chosen source ("API
    // key" vs "1Password item"), and sizing to the current one made the whole
    // value column jump sideways as the user arrowed through the sources.
    let label_w = label_column(
        fields
            .iter()
            .map(|(l, _, _)| *l)
            .chain(KeySource::ALL.iter().map(|src| src.field_label(s))),
    );

    // The references this key has been read from before, offered *under* the
    // key field as a dropdown rather than as a permanent list: finding the
    // 1Password item path is the tedious half of setting an import up, and it
    // is the same path every time -- but it is a suggestion about one field,
    // not a fifth row of the form.
    let recent = w.recent_matches();

    // A line under the key field saying what picking this source commits the
    // user to. The picker names four sources but says nothing about what each
    // one needs of them, which is the whole question for someone who has never
    // referenced a secret by address before.
    let help = w.key_source.field_help(s);
    let width = 66.min(f.area().width);
    let help_rows = wrapped_height(help, width.saturating_sub(2));
    let area = centered_rect(width, fields.len() as u16 + help_rows + 2, f.area());
    f.render_widget(Clear, area);
    let hint = if w.recent_sel.is_some() {
        // While the list has focus the connect hint is describing keys that
        // now do something else: Enter fills the field rather than connecting.
        s.postman_recent_pick_hint.to_string()
    } else if !recent.is_empty() {
        format!(
            "{}  \u{b7}  {}",
            s.postman_connect_hint, s.postman_recent_hint
        )
    } else {
        s.postman_connect_hint.to_string()
    };
    let block = panel_hinted(title.to_string(), &hint, th);
    let inner = block.inner(area);
    f.render_widget(block, area);

    let all_rows = Layout::vertical([
        Constraint::Length(1),         // key source
        Constraint::Length(1),         // key
        Constraint::Length(help_rows), // what that source means
        Constraint::Length(1),         // workspace
        Constraint::Length(1),         // host
    ])
    .split(inner);
    let rows = [all_rows[0], all_rows[1], all_rows[3], all_rows[4]];
    for (row, (label, placeholder, idx)) in rows.iter().zip(fields.iter()) {
        let cols =
            Layout::horizontal([Constraint::Length(label_w), Constraint::Min(1)]).split(*row);
        f.render_widget(
            Paragraph::new(Span::styled(*label, Style::default().fg(th.accent))),
            cols[0],
        );
        if *idx == 0 {
            // A cycled value, written the way every other one-of-several
            // choice in the app is, and lit when it holds focus.
            let style = if w.field == 0 {
                Style::default().fg(th.accent).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(th.text)
            };
            f.render_widget(
                Paragraph::new(Span::styled(
                    format!("\u{2039} {} \u{203a}", w.key_source.source_label(s)),
                    style,
                )),
                cols[1],
            );
            continue;
        }
        // Only a pasted key is itself a credential; a reference is the
        // *address* of one, and masking that would only stop the user
        // checking they typed it correctly.
        let (ed, mask) = match idx {
            1 => (&w.key, w.key_source.is_secret()),
            2 => (&w.workspace_ref, false),
            _ => (&w.base_url, false),
        };
        render_line_field_hinted(f, cols[1], ed, w.field == *idx, mask, placeholder, th);
    }

    f.render_widget(
        Paragraph::new(Span::styled(help, Style::default().fg(th.dim))).wrap(Wrap { trim: true }),
        all_rows[2],
    );

    if w.recent_open() {
        // Anchored under the key field, like the request wizard's header-name
        // dropdown: these are suggestions about one field, and a list living
        // permanently in the form read as another row of it -- or, drawn dim,
        // as more ghost text. Last, so it sits over the rows beneath it.
        let value_col = Layout::horizontal([Constraint::Length(label_w), Constraint::Min(1)])
            .split(all_rows[1])[1];
        draw_recent_dropdown(f, w, s, th, value_col, &recent);
    }
}

/// The saved key references, as a dropdown hanging off the key field.
///
/// Deliberately the same object as [`super::new_request::wizard::draw_key_suggestions`]:
/// a bordered popup saying what Enter does, a `\u{203a}` on the highlighted row,
/// and a background of its own -- `Clear` alone would let the terminal's
/// colours through.
fn draw_recent_dropdown(
    f: &mut Frame,
    w: &PostmanWizard,
    s: &Strings,
    th: &Theme,
    anchor: Rect,
    entries: &[String],
) {
    const VISIBLE: usize = 6;
    let fr = f.area();
    let content_w = entries.iter().map(|e| e.chars().count()).max().unwrap_or(0) as u16;
    let width = (content_w + 6).max(anchor.width).max(16).min(fr.width);
    let rows = entries.len().min(VISIBLE) as u16;
    let height = (rows + 2).min(fr.height);

    let mut x = anchor.x;
    if x + width > fr.right() {
        x = fr.right().saturating_sub(width);
    }
    // Below the field where possible, flipped above when there is no room.
    let y = if anchor.y + 1 + height <= fr.bottom() {
        anchor.y + 1
    } else {
        anchor.y.saturating_sub(height)
    };
    let popup = Rect {
        x,
        y,
        width,
        height,
    };

    // Enough of a scroll to keep the highlight on screen. Stateless: there is
    // nowhere else to go in a list of at most ten.
    let offset = w
        .recent_sel
        .map_or(0, |i| i.saturating_sub(VISIBLE - 1))
        .min(entries.len().saturating_sub(VISIBLE));
    let items: Vec<ListItem> = entries
        .iter()
        .enumerate()
        .skip(offset)
        .take(VISIBLE)
        .map(|(i, entry)| {
            let selected = w.recent_sel == Some(i);
            let style = if selected {
                Style::default()
                    .bg(th.accent)
                    .fg(th.bg)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(th.text)
            };
            let marker = if selected { "\u{203a} " } else { "  " };
            ListItem::new(Line::styled(format!("{marker}{entry}"), style))
        })
        .collect();

    f.render_widget(Clear, popup);
    f.render_widget(
        List::new(items).block(
            Block::default()
                .borders(Borders::ALL)
                .style(Style::default().bg(th.panel).fg(th.text))
                .border_style(Style::default().fg(th.accent))
                .title(Span::styled(
                    format!(" {} ", s.suggest_hint),
                    Style::default().fg(th.dim),
                )),
        ),
        popup,
    );
}

fn draw_loading(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme, title: &str) {
    // Says why it is waiting and how long it has been, not just what it is
    // doing: Postman's rate limit can hold a listing for minutes, and a bare
    // phase label leaves that looking like a hang.
    let msg = w
        .flow
        .busy_line(s)
        .unwrap_or_else(|| s.postman_busy_listing.to_string());
    // The allowance is the fact behind the wait: a listing paced to one call
    // every twelve seconds is a rate limit being respected, not a stall.
    let budget = w.flow.budget_line(s);
    let widest = msg
        .chars()
        .count()
        .max(s.git_loading_hint.chars().count())
        .max(budget.as_ref().map_or(0, |b| b.chars().count()));
    let width = (widest as u16 + 4).min(f.area().width);
    let area = centered_rect(width, if budget.is_some() { 4 } else { 3 }, f.area());
    f.render_widget(Clear, area);
    let block = panel_hinted(title.to_string(), s.git_loading_hint, th);
    let inner = block.inner(area);
    f.render_widget(block, area);
    let mut lines = vec![Line::styled(
        msg,
        Style::default().fg(th.text).add_modifier(Modifier::BOLD),
    )];
    if let Some(budget) = budget {
        lines.push(Line::styled(budget, Style::default().fg(th.dim)));
    }
    f.render_widget(Paragraph::new(lines), inner);
}

fn draw_pick_workspace(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme) {
    let items: Vec<String> = w
        .flow
        .visible_workspaces()
        .iter()
        .map(|ws| ws.name.clone())
        .collect();
    let area = centered_rect(
        (f.area().width * 7 / 10).max(50),
        (f.area().height * 7 / 10).max(10),
        f.area(),
    );
    f.render_widget(Clear, area);
    // A Postman-specific hint rather than the shared filter one: importing
    // everything is the reason someone opened this wizard during a migration,
    // and a key nobody is told about may as well not exist.
    let block = panel_hinted(
        s.postman_pick_workspace.to_string(),
        s.postman_pick_workspace_hint,
        th,
    );
    let inner = block.inner(area);
    f.render_widget(block, area);
    let rows = Layout::vertical([Constraint::Length(1), Constraint::Min(1)]).split(inner);
    f.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled(s.git_filter_label.to_string(), Style::default().fg(th.dim)),
            Span::styled(w.flow.filter.clone(), Style::default().fg(th.text)),
        ])),
        rows[0],
    );
    let list_items: Vec<ListItem> = items
        .iter()
        .map(|n| ListItem::new(Line::styled(n.clone(), Style::default().fg(th.text))))
        .collect();
    let list = List::new(list_items)
        .highlight_style(
            Style::default()
                .bg(th.accent)
                .fg(th.bg)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("\u{203a} ");
    let sel = (!items.is_empty()).then(|| w.flow.selected.min(items.len() - 1));
    w.list_scroll.render(f, rows[1], list, sel, items.len());
}

fn draw_options(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme, title: &str) {
    let labels = option_labels(w, s);
    // What Enter does here depends on the row, so the border says which: a
    // single hint could only be wrong on two rows out of three.
    let hint = match w.option_row {
        0 => s.postman_options_hint_dest,
        r if r == OPTION_ROWS - 1 => s.postman_options_hint_import,
        _ => s.postman_options_hint_toggle,
    };
    // Whatever this particular set of options needs said about it. Importing
    // several workspaces changes the shape of what lands on disk, so it is
    // spelled out here rather than discovered afterwards.
    let mut notes: Vec<&str> = Vec::new();
    if w.flow.target_count() > 1 {
        notes.push(s.postman_import_all_note);
    }
    if w.flow.format == ImportFormat::Hurl {
        notes.push(s.postman_format_hurl_note);
    }
    let width = 78.min(f.area().width);
    let note_h: u16 = notes
        .iter()
        .map(|n| wrapped_height(n, width.saturating_sub(2)))
        .sum();
    let area = centered_rect(width, OPTION_ROWS as u16 + note_h + 3, f.area());
    f.render_widget(Clear, area);
    let block = panel_hinted(
        format!("{title} \u{2014} {}", w.flow.target_label(s)),
        hint,
        th,
    );
    let inner = block.inner(area);
    f.render_widget(block, area);
    let rows = Layout::vertical([
        Constraint::Length(1),                      // dest label
        Constraint::Length(1),                      // dest editor
        Constraint::Length(OPTION_ROWS as u16 - 1), // the toggles + button
        Constraint::Length(note_h),                 // format note
    ])
    .split(inner);

    f.render_widget(
        Paragraph::new(Span::styled(
            s.postman_dest_label,
            Style::default().fg(th.accent),
        )),
        rows[0],
    );
    let dest = if w.dest.as_os_str().is_empty() {
        s.postman_dest_unset.to_string()
    } else {
        w.dest.display().to_string()
    };
    let dest_style = if w.option_row == 0 {
        Style::default()
            .bg(th.accent)
            .fg(th.bg)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(th.text)
    };
    // The "press Enter" affordance is the point of the row, so it keeps its
    // width and the path gives way - elided from the LEFT, since the folder
    // name at the end is what distinguishes one destination from another.
    // It is a hint about the row rather than part of the path, so it is drawn
    // dim: at the path's own weight it reads as another path segment.
    let browse = format!("  {}", s.postman_browse);
    let room = (rows[1].width as usize).saturating_sub(browse.chars().count());
    let hint_style = if w.option_row == 0 {
        // On the selected row the highlight owns the background, so the hint
        // steps back by dropping the path's bold rather than by changing hue.
        Style::default().bg(th.accent).fg(th.bg)
    } else {
        Style::default().fg(th.dim)
    };
    f.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled(elide_left(&dest, room), dest_style),
            Span::styled(browse, hint_style),
        ])),
        rows[1],
    );

    let list_items: Vec<ListItem> = labels
        .iter()
        .enumerate()
        .skip(1)
        .map(|(i, l)| {
            let style = if w.option_row == i {
                Style::default()
                    .bg(th.accent)
                    .fg(th.bg)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(th.text)
            };
            ListItem::new(Line::styled(l.clone(), style))
        })
        .collect();
    f.render_widget(List::new(list_items), rows[2]);

    if !notes.is_empty() {
        let lines: Vec<Line> = notes
            .iter()
            .map(|n| Line::styled(*n, Style::default().fg(th.dim)))
            .collect();
        f.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), rows[3]);
    }
}

fn draw_confirm(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme, title: &str) {
    let Some(plan) = w.flow.plan() else {
        return;
    };
    // An open preview takes the screen: it is a list to read, and reading it
    // beside the summary would leave neither enough room to be useful.
    if let Some(preview) = w.flow.preview() {
        draw_preview(f, w, preview, s, th, title);
        return;
    }
    let width = 78.min(f.area().width);
    let note_h = wrapped_height(s.postman_rate_limit_note, width.saturating_sub(2));
    let warn_h = if plan.strains_monthly_budget() {
        wrapped_height(s.postman_budget_warning, width.saturating_sub(2))
    } else {
        0
    };
    // Workspaces that could not be listed are said here, not only in the final
    // summary: an import of forty that quietly became thirty-eight is exactly
    // the kind of thing a migration must not find out about afterwards.
    let skipped = plan_skipped_line(plan, s);
    let skip_h = skipped
        .as_deref()
        .map_or(0, |t| wrapped_height(t, width.saturating_sub(2)));
    // Sized to its content — no trailing empty rows, which on a four-line
    // screen read as "something failed to draw".
    // The collections, so the workspace can be read into before any of it is
    // downloaded. Bounded so a 60-collection workspace still leaves the
    // summary and the estimate on screen.
    let list_h = match w.flow.previewable().len() {
        0 => 0,
        n => (n as u16).min(8) + 2, // + the cost note and a blank line
    };
    let area = centered_rect(width, note_h + warn_h + skip_h + list_h + 5, f.area());
    f.render_widget(Clear, area);
    // The hint is on the border like everywhere else, but in the accent rather
    // than dim: nothing on this screen is waiting for anything else, and a dim
    // line here was read as a status message, leaving people parked wondering
    // why nothing was downloading.
    let hint = if w.flow.previewable().is_empty() {
        s.postman_confirm_hint
    } else {
        s.postman_preview_hint
    };
    let block = panel_hinted_styled(
        title.to_string(),
        hint,
        Style::default().fg(th.accent).add_modifier(Modifier::BOLD),
        th,
    );
    let inner = block.inner(area);
    f.render_widget(block, area);
    let rows = Layout::vertical([
        Constraint::Length(1),      // heading
        Constraint::Length(1),      // counts
        Constraint::Length(note_h), // rate limit note
        Constraint::Length(1),      // estimate
        Constraint::Length(warn_h), // budget warning
        Constraint::Length(skip_h), // workspaces that could not be listed
        Constraint::Length(list_h), // collections to preview
    ])
    .split(inner);

    f.render_widget(
        Paragraph::new(Span::styled(
            s.postman_confirm_title,
            Style::default().fg(th.accent).add_modifier(Modifier::BOLD),
        )),
        rows[0],
    );
    f.render_widget(
        Paragraph::new(Line::styled(
            plan_summary(plan, s),
            Style::default().fg(th.text),
        )),
        rows[1],
    );
    f.render_widget(
        Paragraph::new(Line::styled(
            s.postman_rate_limit_note,
            Style::default().fg(th.dim),
        ))
        .wrap(Wrap { trim: true }),
        rows[2],
    );
    f.render_widget(
        Paragraph::new(Line::styled(
            format!(
                "{} {}",
                s.postman_estimate,
                human_duration(plan.estimated_duration(), s)
            ),
            Style::default().fg(th.text).add_modifier(Modifier::BOLD),
        )),
        rows[3],
    );
    if plan.strains_monthly_budget() {
        f.render_widget(
            Paragraph::new(Line::styled(
                s.postman_budget_warning,
                Style::default().fg(th.err),
            ))
            .wrap(Wrap { trim: true }),
            rows[4],
        );
    }
    if let Some(text) = skipped {
        f.render_widget(
            Paragraph::new(Line::styled(text, Style::default().fg(th.err)))
                .wrap(Wrap { trim: true }),
            rows[5],
        );
    }
    if list_h > 0 {
        draw_preview_list(f, w, s, th, rows[6]);
    }
}

/// The plan's collections, highlighted one at a time, with a line saying what
/// opening one costs. Nothing is fetched until asked for by name.
fn draw_preview_list(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme, area: Rect) {
    let rows = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).split(area);
    let status = match (w.flow.preview_pending(), w.flow.preview_error()) {
        (Some(_), _) => Line::styled(s.postman_preview_busy, Style::default().fg(th.accent)),
        (None, Some(err)) => Line::styled(err.to_string(), Style::default().fg(th.err)),
        (None, None) => Line::styled(s.postman_preview_cost, Style::default().fg(th.dim)),
    };
    f.render_widget(Paragraph::new(status), rows[0]);

    let height = rows[1].height as usize;
    let items = w.flow.previewable();
    // Keep the highlight on screen without a scrollbar's worth of machinery:
    // the window simply follows the selection.
    let first = w.flow.preview_sel.saturating_sub(height.saturating_sub(1));
    let lines: Vec<Line> = items
        .iter()
        .enumerate()
        .skip(first)
        .take(height)
        .map(|(i, item)| {
            let selected = i == w.flow.preview_sel;
            let mark = if selected { "> " } else { "  " };
            let seen = if w.flow.preview_is_cached(item.fetch_id()) {
                " ·"
            } else {
                ""
            };
            let style = if selected {
                Style::default().fg(th.accent).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(th.text)
            };
            Line::styled(format!("{mark}{}{seen}", item.name), style)
        })
        .collect();
    f.render_widget(Paragraph::new(lines), rows[1]);
}

/// One collection, read out: its requests in their folders, then whatever the
/// conversion would not manage exactly.
fn draw_preview(
    f: &mut Frame,
    w: &PostmanWizard,
    preview: &crate::postman_flow::Preview,
    s: &Strings,
    th: &Theme,
    title: &str,
) {
    let width = 78.min(f.area().width);
    let height = (f.area().height * 4 / 5).max(6.min(f.area().height));
    let area = centered_rect(width, height, f.area());
    f.render_widget(Clear, area);
    let block = panel_hinted_styled(
        format!("{title} \u{2014} {}", preview.name),
        s.postman_preview_close_hint,
        Style::default().fg(th.accent).add_modifier(Modifier::BOLD),
        th,
    );
    let inner = block.inner(area);
    f.render_widget(block, area);
    let rows = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).split(inner);

    f.render_widget(
        Paragraph::new(Line::styled(
            format!(
                "{} \u{b7} {} {}",
                s.postman_preview_title, preview.requests, s.postman_preview_requests
            ),
            Style::default().fg(th.accent).add_modifier(Modifier::BOLD),
        )),
        rows[0],
    );

    let mut lines: Vec<Line> = Vec::new();
    if preview.rows.is_empty() {
        lines.push(Line::styled(
            s.postman_preview_empty,
            Style::default().fg(th.dim),
        ));
    }
    for row in &preview.rows {
        let indent = " ".repeat(row.depth * 2);
        let label = if row.label.trim().is_empty() {
            s.postman_preview_untitled
        } else {
            row.label.as_str()
        };
        lines.push(match &row.method {
            Some(method) => Line::from(vec![
                Span::styled(format!("{indent}{method} "), Style::default().fg(th.accent)),
                Span::styled(label.to_string(), Style::default().fg(th.text)),
            ]),
            None => Line::styled(format!("{indent}{label}/"), Style::default().fg(th.dim)),
        });
    }
    if !preview.notes.is_empty() {
        lines.push(Line::from(""));
        lines.push(Line::styled(
            s.postman_preview_notes,
            Style::default().fg(th.err),
        ));
        for note in &preview.notes {
            lines.push(Line::styled(note.clone(), Style::default().fg(th.dim)));
        }
    }
    // Clamped so scrolling past the end can't leave an empty panel.
    let max = lines.len().saturating_sub(rows[1].height as usize);
    let scroll = w.preview_scroll.min(max);
    f.render_widget(Paragraph::new(lines).scroll((scroll as u16, 0)), rows[1]);
}

fn draw_downloading(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme, title: &str) {
    let p = w.flow.progress();
    // The bar is the whole point of this screen, so it gets the room: a fixed
    // 74 columns left it stranded in the middle of a wide terminal, drawing a
    // half-width bar for a full-width wait.
    let width = (f.area().width * 9 / 10).max(40.min(f.area().width));
    let eta = p.eta();
    // Only the rows that have something in them — an empty row reserved for an
    // ETA that hasn't been worked out yet is just a hole in the dialog.
    let mut constraints = vec![
        Constraint::Length(1), // gauge
        Constraint::Length(1), // current item
    ];
    if eta.is_some() {
        constraints.push(Constraint::Length(1));
    }
    if p.waiting.is_some() {
        constraints.push(Constraint::Length(1));
    }
    let budget = w.flow.budget_line(s);
    if budget.is_some() {
        constraints.push(Constraint::Length(1));
    }
    let area = centered_rect(width, constraints.len() as u16 + 2, f.area());
    f.render_widget(Clear, area);
    let block = panel_hinted(title.to_string(), s.git_loading_hint, th);
    let inner = block.inner(area);
    f.render_widget(block, area);
    let rows = Layout::vertical(constraints).split(inner);
    let mut row = 0;
    let mut next = || {
        row += 1;
        rows[row - 1]
    };

    f.render_widget(
        Gauge::default()
            .gauge_style(Style::default().fg(th.accent).bg(th.panel))
            .ratio(p.fraction().clamp(0.0, 1.0) as f64)
            .label(format!("{}/{}", p.done, p.total)),
        next(),
    );
    let current = match p.current_kind {
        Some(kind) => format!("{}: {}", item_kind_label(kind, s), p.current),
        None => p.current.clone(),
    };
    f.render_widget(
        Paragraph::new(Line::styled(current, Style::default().fg(th.text))),
        next(),
    );
    if let Some(eta) = eta {
        f.render_widget(
            Paragraph::new(Line::styled(
                format!(
                    "{} {} {}",
                    s.postman_estimate,
                    human_duration(eta, s),
                    s.postman_remaining
                ),
                Style::default().fg(th.dim),
            )),
            next(),
        );
    }
    // A paced import spends most of its life deliberately idle; saying so is
    // the difference between "working" and "hung".
    if let Some((reason, secs)) = &p.waiting {
        let label = match reason {
            crate::postman_import::WaitReason::Pacing => s.postman_waiting_paced,
            crate::postman_import::WaitReason::RateLimited => s.postman_waiting_limited,
        };
        f.render_widget(
            Paragraph::new(Line::styled(
                format!("{label} ({secs}s)"),
                Style::default().fg(th.pending),
            )),
            next(),
        );
    }
    if let Some(budget) = budget {
        f.render_widget(
            Paragraph::new(Line::styled(budget, Style::default().fg(th.dim))),
            next(),
        );
    }
}

fn draw_done(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme) {
    let width = 74u16.min(f.area().width);
    let mut lines: Vec<Line> = Vec::new();
    if let Some(d) = &w.done {
        lines.push(Line::styled(
            crate::postman_flow::imported_counts(d.collections, d.environments, s),
            Style::default().fg(th.text),
        ));
    }
    lines.push(Line::styled(
        format!(
            "{} {}",
            s.postman_done_saved_to,
            w.flow.dest_path().to_string_lossy()
        ),
        Style::default().fg(th.dim),
    ));
    // What the receipt is really for: the folder has just become a tab, and
    // nothing else on screen says so.
    lines.push(Line::styled(
        s.postman_done_opened,
        Style::default().fg(th.text),
    ));
    let failures = w.flow.failures();
    if !failures.is_empty() {
        lines.push(Line::styled(
            format!("{} {}", failures.len(), s.postman_skipped),
            Style::default().fg(th.pending),
        ));
    }
    if w.done.as_ref().is_some_and(|d| d.converted_with_notes) {
        lines.push(Line::styled(
            s.postman_notes_written,
            Style::default().fg(th.dim),
        ));
    }
    // Sized to the wrapped text: the receipt grows a line when something was
    // skipped, and a fixed height would hide exactly that line.
    let body_h: u16 = lines
        .iter()
        .map(|l| {
            let text: String = l.spans.iter().map(|sp| sp.content.as_ref()).collect();
            wrapped_height(&text, width.saturating_sub(2))
        })
        .sum();
    let area = centered_rect(width, body_h + 2, f.area());
    f.render_widget(Clear, area);
    let block = panel_hinted(s.postman_done_title.to_string(), s.git_error_hint, th);
    let inner = block.inner(area);
    f.render_widget(block, area);
    f.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), inner);
}

fn draw_error(f: &mut Frame, w: &PostmanWizard, s: &Strings, th: &Theme, title: &str) {
    let e = w.flow.error().unwrap_or_default().to_string();
    let width = (f.area().width * 6 / 10).max(40);
    let body_h = wrapped_height(&e, width.saturating_sub(2));
    let area = centered_rect(width, body_h + 2, f.area());
    f.render_widget(Clear, area);
    // Not "press Esc to close": Esc goes back to the step that can be fixed —
    // the key prompt for a rejected key — rather than throwing the import away.
    let block = panel_hinted(title.to_string(), s.postman_error_hint, th);
    let inner = block.inner(area);
    f.render_widget(block, area);
    f.render_widget(
        Paragraph::new(e)
            .style(Style::default().fg(th.err))
            .wrap(Wrap { trim: true }),
        inner,
    );
}

/// Shorten `text` to `width` columns by dropping characters from the FRONT and
/// marking the cut with a leading ellipsis. Used for paths, where the tail (the
/// folder being written) carries far more information than the root.
fn elide_left(text: &str, width: usize) -> String {
    let len = text.chars().count();
    if len <= width {
        return text.to_string();
    }
    // Below two columns there is no room for the marker plus any content, so
    // return what fits rather than an ellipsis that says nothing.
    if width < 2 {
        return text.chars().skip(len - width).collect();
    }
    let kept: String = text.chars().skip(len - (width - 1)).collect();
    format!("\u{2026}{kept}")
}

#[cfg(test)]
mod tests {
    use super::{elide_left, wrapped_height};

    /// The API-key hint is a full sentence with a URL in it, and it is longer
    /// again in French. Sizing its row from the wrapped height is what stopped
    /// it being cut off mid-word at the panel edge.
    #[test]
    fn a_hint_is_measured_at_the_width_it_will_be_drawn_at() {
        assert_eq!(wrapped_height("short enough", 40), 1);
        assert_eq!(wrapped_height("one two three four", 9), 3);
        // A word longer than the line still fits somewhere rather than
        // reporting a height that would clip it.
        assert_eq!(wrapped_height("https://go.postman.co/settings", 10), 3);
        assert_eq!(wrapped_height("anything", 0), 1);
    }

    #[test]
    fn a_path_that_fits_is_left_alone() {
        assert_eq!(elide_left("/tmp/Alpha", 20), "/tmp/Alpha");
        assert_eq!(elide_left("/tmp/Alpha", 10), "/tmp/Alpha");
    }

    /// The folder name is the part that identifies the destination, so it is
    /// the front of the path that goes.
    #[test]
    fn a_long_path_keeps_its_tail() {
        assert_eq!(
            elide_left("/home/someone/work/Alpha", 10),
            "\u{2026}ork/Alpha"
        );
        // The result fills the space it was given, marker included.
        assert_eq!(
            elide_left("/home/someone/work/Alpha", 10).chars().count(),
            10
        );
    }

    /// A pathological width must not panic or produce a lone ellipsis.
    #[test]
    fn a_width_too_small_for_a_marker_still_returns_content() {
        assert_eq!(elide_left("/tmp/Alpha", 1), "a");
        assert_eq!(elide_left("/tmp/Alpha", 0), "");
    }
}