op-loader 0.5.0

TUI for configuring 1password secrets for injection into your shell environment
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
use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::Line,
    widgets::{Block, BorderType, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap},
};

use crate::app::{Account, App, FocusedPanel, ItemField, Vault};
use crate::command_log::CommandLogEntry;

pub fn render(frame: &mut Frame, app: &mut App) {
    let outer_layout = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
        .split(frame.area());

    let left_pane_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(5),
            Constraint::Min(8),
            Constraint::Length(8),
            Constraint::Length(8),
        ])
        .split(outer_layout[0]);

    let right_pane_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage(66),
            Constraint::Percentage(34),
            Constraint::Length(1),
        ])
        .split(outer_layout[1]);

    render_list_panel(&AccountListPanel, frame, app, left_pane_layout[0]);
    render_list_panel(&VaultListPanel, frame, app, left_pane_layout[1]);
    render_list_panel(&VarsListPanel, frame, app, left_pane_layout[2]);
    render_command_log(frame, app, left_pane_layout[3]);
    render_vault_item_panel(frame, app, right_pane_layout[0]);
    render_item_details_panel(frame, app, right_pane_layout[1]);
    render_right_column_footer(frame, right_pane_layout[2]);

    if app.modal.is_some() {
        render_modal(frame, app);
    }
}

trait ListPanel {
    type Item;

    fn title(&self) -> &str;
    fn title_bottom(&self) -> Option<&str> {
        None
    }
    fn focus_variant(&self) -> FocusedPanel;
    fn selected_color(&self) -> Color;

    fn items<'a>(&self, app: &'a App) -> &'a [Self::Item];

    fn display_item(&self, item: &Self::Item) -> String;

    fn is_favorite(&self, _app: &App, _item: &Self::Item) -> bool {
        false
    }

    fn selected_idx(&self, app: &App) -> Option<usize>;
    fn list_state<'a>(&self, app: &'a mut App) -> &'a mut ListState;

    fn selection_prefix(&self, _app: &App, _item: &Self::Item, is_selected: bool) -> String {
        if is_selected {
            "".to_string()
        } else {
            "  ".to_string()
        }
    }
}

fn render_list_panel<P: ListPanel>(panel: &P, frame: &mut Frame, app: &mut App, area: Rect) {
    let is_focused = app.focused_panel == panel.focus_variant();

    let mut block = Block::default()
        .title(panel.title())
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(if is_focused {
            Style::default().fg(Color::Cyan)
        } else {
            Style::default()
        });

    if let Some(title_bottom) = panel.title_bottom() {
        block = block.title_bottom(Line::from(title_bottom).right_aligned());
    }

    let inner_area = block.inner(area);
    frame.render_widget(block, area);

    render_list_inner(panel, frame, app, inner_area);
}

fn render_list_inner<P: ListPanel>(panel: &P, frame: &mut Frame, app: &mut App, area: Rect) {
    let selected_idx = panel.selected_idx(app);
    let selected_color = panel.selected_color();

    let items: Vec<ListItem> = panel
        .items(app)
        .iter()
        .enumerate()
        .map(|(idx, item)| {
            let is_selected = selected_idx == Some(idx);
            let is_favorite = panel.is_favorite(app, item);
            let prefix = panel.selection_prefix(app, item, is_selected);
            let suffix = if is_favorite { "" } else { "" };
            let content = format!("{}{}{}", prefix, panel.display_item(item), suffix);

            ListItem::new(content).style(if is_selected {
                Style::default().fg(selected_color)
            } else {
                Style::default()
            })
        })
        .collect();

    let list = List::new(items)
        .highlight_style(
            Style::default()
                .bg(Color::DarkGray)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("> ");

    frame.render_stateful_widget(list, area, panel.list_state(app));
}

fn render_vault_item_panel(frame: &mut Frame, app: &mut App, area: Rect) {
    let is_focused = app.focused_panel == FocusedPanel::VaultItemList && !app.search_active;

    let block = Block::default()
        .title(" [2] Items ")
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(if is_focused {
            Style::default().fg(Color::Cyan)
        } else {
            Style::default()
        });

    let inner = block.inner(area);
    frame.render_widget(block, area);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Fill(1), Constraint::Length(3)])
        .split(inner);

    render_filtered_vault_items(frame, app, chunks[0]);
    render_search_box(frame, app, chunks[1]);
}

fn render_item_details_panel(frame: &mut Frame, app: &mut App, area: Rect) {
    let is_focused = app.focused_panel == FocusedPanel::VaultItemDetail;

    let block = Block::default()
        .title(" [3] Details ")
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(if is_focused {
            Style::default().fg(Color::Cyan)
        } else {
            Style::default()
        });

    let inner = block.inner(area);
    frame.render_widget(block, area);

    render_item_details(frame, app, inner);
}

fn render_filtered_vault_items(frame: &mut Frame, app: &mut App, area: Rect) {
    let selected_idx = app.selected_vault_item_idx;

    let items: Vec<ListItem> = app
        .filtered_item_indices
        .iter()
        .enumerate()
        .map(|(display_idx, &real_idx)| {
            let item = &app.vault_items[real_idx];
            let is_selected = selected_idx == Some(display_idx);
            let prefix = if is_selected { "" } else { "  " };
            let content = format!("{}{}", prefix, item.title);

            ListItem::new(content).style(if is_selected {
                Style::default().fg(Color::Cyan)
            } else {
                Style::default()
            })
        })
        .collect();

    let list = List::new(items)
        .highlight_style(
            Style::default()
                .bg(Color::DarkGray)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("> ");

    frame.render_stateful_widget(list, area, &mut app.vault_item_list_state);
}

fn render_search_box(frame: &mut Frame, app: &App, area: Rect) {
    let is_active = app.search_active;

    let block = Block::default()
        .title(" [/] Search ")
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(if is_active {
            Style::default().fg(Color::Yellow)
        } else {
            Style::default()
        });

    let inner = block.inner(area);
    frame.render_widget(block, area);

    let text = if app.search_query.is_empty() {
        if is_active {
            String::new()
        } else {
            "Press / to search".to_string()
        }
    } else if is_active {
        format!("{}", app.search_query)
    } else {
        app.search_query.clone()
    };

    let style = if app.search_query.is_empty() && !is_active {
        Style::default().fg(Color::DarkGray)
    } else {
        Style::default()
    };

    let paragraph = Paragraph::new(text).style(style);
    frame.render_widget(paragraph, inner);
}

fn render_item_details(frame: &mut Frame, app: &mut App, area: Rect) {
    let Some(details) = &app.selected_item_details else {
        let empty = Paragraph::new("Select an item and press Enter");
        frame.render_widget(empty, area);
        return;
    };

    let fields: Vec<&ItemField> = details
        .fields
        .iter()
        .filter(|f| f.label != "notesPlain")
        .collect();

    let items: Vec<ListItem> = fields
        .iter()
        .enumerate()
        .map(|(idx, f)| {
            let is_selected = app.selected_field_idx == Some(idx);
            let value = if f.field_type == "CONCEALED" {
                "********".to_string()
            } else {
                f.value.clone().unwrap_or_default()
            };
            let prefix = if is_selected { "" } else { "  " };
            let content = format!("{}{}: {}\n    {}", prefix, f.label, value, f.reference);

            ListItem::new(content).style(if is_selected {
                Style::default().fg(Color::Cyan)
            } else {
                Style::default()
            })
        })
        .collect();

    let list = List::new(items)
        .highlight_style(
            Style::default()
                .bg(Color::DarkGray)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("> ");

    frame.render_stateful_widget(list, area, &mut app.item_detail_list_state);
}

fn render_command_log(frame: &mut Frame, app: &App, area: Rect) {
    let block = Block::default()
        .title(" Command Log ")
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded);

    let visible_lines = area.height.saturating_sub(2) as usize;

    let text: String = app
        .command_log
        .recent(visible_lines)
        .iter()
        .map(CommandLogEntry::display)
        .collect::<Vec<_>>()
        .join("\n");

    let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: false });

    frame.render_widget(paragraph, area);
}

fn render_right_column_footer(frame: &mut Frame, area: Rect) {
    let text = "[Enter] Select  [k/Up] Up  [j/Down] Down  [q] Quit ";
    let paragraph = Paragraph::new(text)
        .style(Style::default().fg(Color::DarkGray))
        .alignment(Alignment::Right);
    frame.render_widget(paragraph, area);
}

#[allow(clippy::too_many_lines)]
fn render_modal(frame: &mut Frame, app: &App) {
    let area = frame.area();
    let Some(modal) = app.modal.as_ref() else {
        return;
    };

    match modal {
        crate::app::Modal::EnvVar { .. } => {
            // Content: field info (5) + spacer (1) + input (3) + error (1) + help (1) = 11, plus border (2) = 13
            let modal_width = area.width * 60 / 100;
            let modal_height = 13_u16.min(area.height - 4);
            let modal_x = (area.width - modal_width) / 2;
            let modal_y = (area.height - modal_height) / 2;

            let modal_area = Rect::new(modal_x, modal_y, modal_width, modal_height);

            frame.render_widget(Clear, modal_area);

            let block = Block::default()
                .title(" Save to Configuration ")
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(Color::Yellow));

            let inner = block.inner(modal_area);
            frame.render_widget(block, modal_area);

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(5), // field info
                    Constraint::Length(1), // spacer
                    Constraint::Length(3), // env var input
                    Constraint::Length(1), // error message
                    Constraint::Length(1), // help text
                ])
                .split(inner);

            if let Some(field) = app.modal_selected_field() {
                let value_display = if field.field_type == "CONCEALED" {
                    "********".to_string()
                } else {
                    field.value.clone().unwrap_or_default()
                };

                let info_text = format!(
                    "Field: {}\nValue: {}\n\nReference:\n{}",
                    field.label, value_display, field.reference
                );

                let info = Paragraph::new(info_text).wrap(Wrap { trim: false });
                frame.render_widget(info, chunks[0]);
            }

            let input_block = Block::default()
                .title(" Environment Variable Name ")
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(Color::Cyan));

            let input_inner = input_block.inner(chunks[2]);
            frame.render_widget(input_block, chunks[2]);

            let input_text = format!("{}", app.modal_env_var_name().unwrap_or(""));
            let input = Paragraph::new(input_text);
            frame.render_widget(input, input_inner);

            if let Some(ref error) = app.error_message {
                let error_text = Paragraph::new(error.as_str())
                    .style(Style::default().fg(Color::Red))
                    .alignment(Alignment::Center);
                frame.render_widget(error_text, chunks[3]);
            }

            let help = Paragraph::new("Enter: Save  |  Esc: Cancel")
                .style(Style::default().fg(Color::DarkGray))
                .alignment(Alignment::Center);
            frame.render_widget(help, chunks[4]);
        }
        crate::app::Modal::VarDeleteConfirm { vars } => {
            let modal_width = area.width * 60 / 100;
            let modal_height = 7_u16.min(area.height - 4);
            let modal_x = (area.width - modal_width) / 2;
            let modal_y = (area.height - modal_height) / 2;

            let modal_area = Rect::new(modal_x, modal_y, modal_width, modal_height);

            frame.render_widget(Clear, modal_area);

            let block = Block::default()
                .title(" Delete Managed Vars ")
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(Color::Yellow));

            let inner = block.inner(modal_area);
            frame.render_widget(block, modal_area);

            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(1),
                    Constraint::Min(1),
                    Constraint::Length(1),
                ])
                .split(inner);

            let header = Paragraph::new("Delete these vars?")
                .style(Style::default().fg(Color::Yellow))
                .alignment(Alignment::Center);
            frame.render_widget(header, chunks[0]);

            let vars_text = if vars.is_empty() {
                "(no vars selected)".to_string()
            } else {
                vars.join("\n")
            };
            let vars_paragraph = Paragraph::new(vars_text).wrap(Wrap { trim: false });
            frame.render_widget(vars_paragraph, chunks[1]);

            let help = Paragraph::new("Y: Confirm  |  N/Esc: Cancel")
                .style(Style::default().fg(Color::DarkGray))
                .alignment(Alignment::Center);
            frame.render_widget(help, chunks[2]);
        }
    }
}

struct AccountListPanel;

impl ListPanel for AccountListPanel {
    type Item = Account;

    fn title(&self) -> &'static str {
        " [0] Accounts "
    }
    fn title_bottom(&self) -> Option<&str> {
        Some(" [f] Favorite ")
    }
    fn focus_variant(&self) -> FocusedPanel {
        FocusedPanel::AccountList
    }
    fn items<'a>(&self, app: &'a App) -> &'a [Account] {
        &app.accounts
    }
    fn display_item(&self, item: &Self::Item) -> String {
        item.email.clone()
    }
    fn is_favorite(&self, app: &App, item: &Self::Item) -> bool {
        app.config
            .as_ref()
            .and_then(|c| c.default_account_id.as_ref())
            .is_some_and(|id| id == &item.account_uuid)
    }
    fn list_state<'a>(&self, app: &'a mut App) -> &'a mut ListState {
        &mut app.account_list_state
    }
    fn selected_color(&self) -> Color {
        Color::Cyan
    }
    fn selected_idx(&self, app: &App) -> Option<usize> {
        app.selected_account_idx
    }
}

struct VaultListPanel;

impl ListPanel for VaultListPanel {
    type Item = Vault;

    fn title(&self) -> &'static str {
        " [1] Vaults "
    }
    fn title_bottom(&self) -> Option<&str> {
        Some(" [f] Favorite ")
    }
    fn focus_variant(&self) -> FocusedPanel {
        FocusedPanel::VaultList
    }
    fn items<'a>(&self, app: &'a App) -> &'a [Vault] {
        &app.vaults
    }
    fn display_item(&self, item: &Self::Item) -> String {
        item.name.clone()
    }
    fn is_favorite(&self, app: &App, item: &Self::Item) -> bool {
        app.selected_account()
            .map(|a| a.account_uuid.clone())
            .and_then(|account_id| {
                app.config
                    .as_ref()
                    .and_then(|c| c.default_vault_per_account.get(&account_id))
            })
            .is_some_and(|vault_id| vault_id == &item.id)
    }
    fn list_state<'a>(&self, app: &'a mut App) -> &'a mut ListState {
        &mut app.vault_list_state
    }
    fn selected_color(&self) -> Color {
        Color::Cyan
    }
    fn selected_idx(&self, app: &App) -> Option<usize> {
        app.selected_vault_idx
    }
}

struct VarsListPanel;

impl ListPanel for VarsListPanel {
    type Item = String;

    fn title(&self) -> &'static str {
        " [v] Managed Vars "
    }

    fn title_bottom(&self) -> Option<&str> {
        Some(" [Space] Select  [c] Copy Name  [d] Delete ")
    }

    fn focus_variant(&self) -> FocusedPanel {
        FocusedPanel::VarsList
    }

    fn items<'a>(&self, app: &'a App) -> &'a [String] {
        &app.managed_vars
    }

    fn display_item(&self, item: &Self::Item) -> String {
        item.clone()
    }

    fn list_state<'a>(&self, app: &'a mut App) -> &'a mut ListState {
        &mut app.managed_vars_list_state
    }

    fn selected_color(&self) -> Color {
        Color::Cyan
    }

    fn selected_idx(&self, app: &App) -> Option<usize> {
        app.managed_vars_list_state.selected()
    }

    fn selection_prefix(&self, app: &App, item: &Self::Item, _is_selected: bool) -> String {
        if app.managed_vars_selected.contains(item) {
            "".to_string()
        } else {
            "  ".to_string()
        }
    }
}