lazy-locker 0.0.5

A secure local secrets manager with TUI interface and SDK support
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
use crate::app::{App, Field, Modal, Mode};
use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Modifier, Style, Stylize},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap},
};

// ============================================================================
// Tokyo Night Color Theme
// ============================================================================
mod theme {
    use ratatui::style::Color;

    // Tokyo Night Storm palette
    #[allow(dead_code)]
    pub const BG: Color = Color::Rgb(36, 40, 59); // #24283b
    pub const BG_DARK: Color = Color::Rgb(26, 27, 38); // #1a1b26
    pub const BG_HIGHLIGHT: Color = Color::Rgb(41, 46, 66); // #292e42
    pub const FG: Color = Color::Rgb(169, 177, 214); // #a9b1d6
    pub const FG_DARK: Color = Color::Rgb(86, 95, 137); // #565f89
    pub const COMMENT: Color = Color::Rgb(86, 95, 137); // #565f89

    // Accent colors
    pub const BLUE: Color = Color::Rgb(122, 162, 247); // #7aa2f7
    pub const CYAN: Color = Color::Rgb(125, 207, 255); // #7dcfff
    pub const PURPLE: Color = Color::Rgb(187, 154, 247); // #bb9af7
    pub const GREEN: Color = Color::Rgb(158, 206, 106); // #9ece6a
    pub const YELLOW: Color = Color::Rgb(224, 175, 104); // #e0af68
    #[allow(dead_code)]
    pub const ORANGE: Color = Color::Rgb(255, 158, 100); // #ff9e64
    pub const RED: Color = Color::Rgb(247, 118, 142); // #f7768e
    #[allow(dead_code)]
    pub const MAGENTA: Color = Color::Rgb(255, 117, 127); // #ff757f
    pub const TEAL: Color = Color::Rgb(115, 218, 202); // #73daca
}

pub fn render(app: &App, frame: &mut Frame) {
    // Split the frame into main area and persistent footer
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Min(1),    // Main content area
            Constraint::Length(3), // Persistent footer
        ])
        .split(frame.area());

    // Render main content based on mode
    match app.mode {
        Mode::InitPassphrase => render_passphrase_input(app, chunks[0], frame),
        Mode::Normal => render_main(app, chunks[0], frame),
    }

    // Render modal if open (overlaid)
    match app.modal {
        Modal::AddSecret => render_add_secret_modal(app, frame),
        Modal::DeleteConfirm => render_delete_confirm_modal(app, frame),
        Modal::Help => render_help_modal(frame),
        Modal::Command => render_command_modal(app, frame),
        Modal::None => {}
    }

    // Render persistent footer
    render_footer(app, chunks[1], frame);
}

fn render_passphrase_input(app: &App, area: Rect, frame: &mut Frame) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Min(1),    // Input and error
        ])
        .split(area);

    let title = Paragraph::new("🔒 LAZY LOCKER - Initialisation 🔒")
        .style(Style::default().fg(theme::CYAN).bold())
        .alignment(Alignment::Center)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme::BLUE))
                .style(Style::default().bg(theme::BG_DARK))
                .title(" Info "),
        );

    let passphrase_str = String::from_utf8_lossy(&app.passphrase);
    let masked_passphrase = "*".repeat(passphrase_str.len());
    let mut input_text = format!("Passphrase: {}", masked_passphrase);
    if let Some(ref error) = app.error_message {
        input_text.push_str(&format!("\n\n❌ Error: {}", error));
    }
    let input = Paragraph::new(input_text)
        .style(Style::default().fg(if app.error_message.is_some() {
            theme::RED
        } else {
            theme::FG
        }))
        .alignment(Alignment::Left)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme::PURPLE))
                .style(Style::default().bg(theme::BG_DARK))
                .title(" Enter your passphrase (Enter to confirm) "),
        );

    frame.render_widget(title, chunks[0]);
    frame.render_widget(input, chunks[1]);
}

fn render_main(app: &App, area: Rect, frame: &mut Frame) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(3), Constraint::Min(1)])
        .split(area);

    // Header with agent status indicator
    let agent_indicator = if app.agent_mode { " 🟢 Agent" } else { "" };
    let title = Paragraph::new(format!("🔒 LAZY LOCKER 🔒{}", agent_indicator))
        .style(Style::default().fg(theme::CYAN).bold())
        .alignment(Alignment::Center)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme::BLUE))
                .style(Style::default().bg(theme::BG_DARK))
                .title(" Secrets Manager "),
        );

    frame.render_widget(title, chunks[0]);

    // Horizontal layout: secrets list (left) + usages (right)
    let main_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(60), Constraint::Percentage(40)])
        .split(chunks[1]);

    // Left panel: secrets list
    render_secrets_list(app, main_chunks[0], frame);

    // Right panel: files using the selected token
    render_token_usages(app, main_chunks[1], frame);
}

fn render_secrets_list(app: &App, area: Rect, frame: &mut Frame) {
    let count = app.secrets_count();

    if count == 0 {
        let empty_msg = Paragraph::new("No secrets. Press 'a' to add one.")
            .style(Style::default().fg(theme::COMMENT))
            .alignment(Alignment::Center)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(Style::default().fg(theme::BLUE))
                    .style(Style::default().bg(theme::BG_DARK))
                    .title(" Secrets "),
            );
        frame.render_widget(empty_msg, area);
        return;
    }

    // Build items from agent_secrets or store
    let items: Vec<ListItem> = if let Some(ref secrets) = app.agent_secrets {
        // Agent mode: display from agent_secrets
        let mut names: Vec<_> = secrets.keys().collect();
        names.sort();

        names
            .iter()
            .enumerate()
            .map(|(i, name)| {
                let is_selected = i == app.selected_index;
                let prefix = if is_selected { "" } else { "  " };

                let value_display = if is_selected {
                    if let Some(ref revealed) = app.revealed_secret {
                        revealed.clone()
                    } else {
                        "********".to_string()
                    }
                } else {
                    "********".to_string()
                };

                let display = format!("{}{}: {} [via agent]", prefix, name, value_display);

                let style = if is_selected {
                    Style::default()
                        .fg(theme::YELLOW)
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::default().fg(theme::FG)
                };

                ListItem::new(display).style(style)
            })
            .collect()
    } else if let Some(ref store) = app.secrets_store {
        // Normal mode: display from store
        store
            .list_secrets()
            .iter()
            .enumerate()
            .map(|(i, s)| {
                let is_selected = i == app.selected_index;
                let prefix = if is_selected { "" } else { "  " };

                let value_display = if is_selected {
                    if let Some(ref revealed) = app.revealed_secret {
                        revealed.clone()
                    } else {
                        "********".to_string()
                    }
                } else {
                    "********".to_string()
                };

                let expiration = s.expiration_display();
                let display = format!("{}{}: {} [{}]", prefix, s.name, value_display, expiration);

                let style = if is_selected {
                    Style::default()
                        .fg(theme::YELLOW)
                        .add_modifier(Modifier::BOLD)
                } else if s.is_expired() {
                    Style::default().fg(theme::RED)
                } else {
                    Style::default().fg(theme::FG)
                };

                ListItem::new(display).style(style)
            })
            .collect()
    } else {
        Vec::new()
    };

    let list = List::new(items).block(
        Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme::PURPLE))
            .style(Style::default().bg(theme::BG_DARK))
            .title(" Secrets (↑↓ navigate) "),
    );
    frame.render_widget(list, area);
}

fn render_token_usages(app: &App, area: Rect, frame: &mut Frame) {
    let title = if let Some(name) = app.get_selected_secret_name() {
        format!(" Usage of '{}' ", name)
    } else {
        " Usage ".to_string()
    };

    if app.token_usages.is_empty() {
        let msg = if app.get_selected_secret_name().is_some() {
            "No usage found\nin the current directory."
        } else {
            "Select a secret\nto see its usages."
        };
        let paragraph = Paragraph::new(msg)
            .style(Style::default().fg(theme::COMMENT))
            .alignment(Alignment::Center)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(Style::default().fg(theme::TEAL))
                    .style(Style::default().bg(theme::BG_DARK))
                    .title(title),
            );
        frame.render_widget(paragraph, area);
    } else {
        let items: Vec<ListItem> = app
            .token_usages
            .iter()
            .take(20) // Limit to 20 results
            .map(|usage| {
                let display = format!(
                    "{}:{}\n  {}",
                    usage
                        .file_path
                        .rsplit('/')
                        .next()
                        .unwrap_or(&usage.file_path),
                    usage.line_number,
                    if usage.line_content.len() > 40 {
                        format!("{}...", &usage.line_content[..40])
                    } else {
                        usage.line_content.clone()
                    }
                );
                ListItem::new(display).style(Style::default().fg(theme::FG))
            })
            .collect();

        let count = app.token_usages.len();
        let title_with_count = format!("{} ({} files)", title, count);

        let list = List::new(items).block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme::TEAL))
                .style(Style::default().bg(theme::BG_DARK))
                .title(title_with_count),
        );
        frame.render_widget(list, area);
    }
}

/// Calcule un rectangle centré pour les modals
fn centered_rect(percent_x: u16, percent_y: u16, area: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(area);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(popup_layout[1])[1]
}

fn render_add_secret_modal(app: &App, frame: &mut Frame) {
    let area = centered_rect(60, 50, frame.area());

    // Clear the area behind the modal
    frame.render_widget(Clear, area);

    let block = Block::default()
        .title(" Add a Secret ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::GREEN))
        .style(Style::default().bg(theme::BG_HIGHLIGHT));

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

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(1)
        .constraints([
            Constraint::Length(3), // Nom
            Constraint::Length(3), // Valeur
            Constraint::Length(3), // Expiration
            Constraint::Min(1),    // Instructions
        ])
        .split(inner);

    let name_style = if app.current_field == Field::Name {
        Style::default()
            .fg(theme::YELLOW)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(theme::FG)
    };

    let value_style = if app.current_field == Field::Value {
        Style::default()
            .fg(theme::YELLOW)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(theme::FG)
    };

    let expiration_style = if app.current_field == Field::Expiration {
        Style::default()
            .fg(theme::YELLOW)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(theme::FG)
    };

    let name_input = Paragraph::new(app.new_secret_name.as_str())
        .style(name_style)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(if app.current_field == Field::Name {
                    theme::CYAN
                } else {
                    theme::FG_DARK
                }))
                .title(" Name (Enter: next) "),
        );

    // Display token in plain text (not masked)
    let value_input = Paragraph::new(app.new_secret_value.as_str())
        .style(value_style)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(if app.current_field == Field::Value {
                    theme::CYAN
                } else {
                    theme::FG_DARK
                }))
                .title(" Plain text token (Enter: next) "),
        );

    let expiration_display = if app.new_secret_expiration.is_empty() {
        "Permanent (empty = no expiration)".to_string()
    } else {
        format!("{} days", app.new_secret_expiration)
    };
    let expiration_input = Paragraph::new(expiration_display)
        .style(expiration_style)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(
                    Style::default().fg(if app.current_field == Field::Expiration {
                        theme::CYAN
                    } else {
                        theme::FG_DARK
                    }),
                )
                .title(" Expiration in days (Enter: confirm) "),
        );

    let instructions = Paragraph::new("Tab: switch field | Enter: next/confirm | Esc: cancel")
        .style(Style::default().fg(theme::COMMENT))
        .alignment(Alignment::Center);

    frame.render_widget(name_input, chunks[0]);
    frame.render_widget(value_input, chunks[1]);
    frame.render_widget(expiration_input, chunks[2]);
    frame.render_widget(instructions, chunks[3]);
}

fn render_delete_confirm_modal(app: &App, frame: &mut Frame) {
    let area = centered_rect(50, 30, frame.area());

    frame.render_widget(Clear, area);

    let block = Block::default()
        .title(" ⚠️ Confirm deletion ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::RED))
        .style(Style::default().bg(theme::BG_HIGHLIGHT));

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

    let secret_name = app
        .get_selected_secret_name()
        .unwrap_or_else(|| "?".to_string());
    let text = format!(
        "Do you really want to delete secret '{}' ?\n\n[Y] Yes  |  [N] No / Esc",
        secret_name
    );

    let paragraph = Paragraph::new(text)
        .style(Style::default().fg(theme::FG))
        .alignment(Alignment::Center)
        .wrap(Wrap { trim: true });

    frame.render_widget(paragraph, inner);
}

fn render_help_modal(frame: &mut Frame) {
    let area = centered_rect(60, 70, frame.area());

    frame.render_widget(Clear, area);

    let block = Block::default()
        .title(" 📖 Help - Keyboard shortcuts ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::PURPLE))
        .style(Style::default().bg(theme::BG_HIGHLIGHT));

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

    let help_text = vec![
        "Navigation:",
        "  ↑/↓     Navigate between secrets",
        "",
        "Actions on secrets:",
        "  a       Add a new secret",
        "  e       Reveal/hide the selected token",
        "  y       Copy decrypted token to clipboard",
        "  d       Delete the selected secret",
        "",
        "Commands (press : to open):",
        "  :env    Generate .env file (plain text)",
        "  :bash   Export to ~/.bashrc",
        "  :zsh    Export to ~/.zshrc",
        "  :fish   Export to fish config",
        "  :json   Export as JSON file",
        "  :clear  Remove exports from shell profiles",
        "",
        "General:",
        "  h       Show this help",
        "  q       Quit application",
        "  Esc     Close modal / Cancel",
        "",
        "In the add form:",
        "  Tab     Switch field",
        "  Enter   Go to next field / Confirm",
        "",
        "Press Esc or h to close",
    ];

    let paragraph = Paragraph::new(help_text.join("\n"))
        .style(Style::default().fg(theme::FG))
        .alignment(Alignment::Left)
        .wrap(Wrap { trim: false });

    frame.render_widget(paragraph, inner);
}

fn render_command_modal(app: &App, frame: &mut Frame) {
    let area = centered_rect(50, 40, frame.area());

    frame.render_widget(Clear, area);

    let block = Block::default()
        .title(" ⌨ Command ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::CYAN))
        .style(Style::default().bg(theme::BG_HIGHLIGHT));

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

    // Split inner area
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Input field
            Constraint::Min(1),    // Suggestions
        ])
        .split(inner);

    // Input field with colon prefix
    let input_block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::PURPLE));
    let input_text = format!(":{}", app.command_input);
    let input = Paragraph::new(input_text)
        .style(Style::default().fg(theme::FG).add_modifier(Modifier::BOLD))
        .block(input_block);
    frame.render_widget(input, chunks[0]);

    // Suggestions list
    let suggestions = app.get_command_suggestions();
    let items: Vec<Line> = suggestions
        .iter()
        .enumerate()
        .map(|(i, (cmd, desc))| {
            let style = if i == app.command_suggestion_index {
                Style::default()
                    .fg(theme::GREEN)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(theme::FG)
            };
            let prefix = if i == app.command_suggestion_index {
                ""
            } else {
                "  "
            };
            Line::from(vec![
                Span::styled(format!("{}{}", prefix, cmd), style),
                Span::styled(format!("  - {}", desc), Style::default().fg(theme::COMMENT)),
            ])
        })
        .collect();

    let suggestions_block = Block::default()
        .title(" Suggestions (↑/↓ to select, Enter to execute) ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::FG_DARK));

    let suggestions_widget = Paragraph::new(items)
        .block(suggestions_block)
        .wrap(Wrap { trim: false });

    frame.render_widget(suggestions_widget, chunks[1]);
}

fn render_footer(app: &App, area: Rect, frame: &mut Frame) {
    // Display status message if it exists
    let helper_text = if let Some(ref status) = app.status_message {
        status.as_str()
    } else {
        match (&app.mode, &app.modal) {
            (Mode::InitPassphrase, _) => "Type passphrase and Enter. Esc to quit.",
            (_, Modal::AddSecret) => "Tab: field | Enter: next/confirm | Esc: cancel",
            (_, Modal::DeleteConfirm) => "Y: confirm | N/Esc: cancel",
            (_, Modal::Help) => "Esc/h: close help",
            (_, Modal::Command) => "↑/↓: select | Enter: execute | Esc: cancel",
            (Mode::Normal, Modal::None) => {
                "a: add | e: reveal | y: copy | d: delete | :: cmd | h: help | q: quit"
            }
        }
    };

    let style = if app.status_message.is_some() {
        Style::default().fg(theme::GREEN)
    } else {
        Style::default().fg(theme::COMMENT)
    };

    let helper = Paragraph::new(helper_text)
        .style(style)
        .alignment(Alignment::Center)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme::FG_DARK))
                .style(Style::default().bg(theme::BG_DARK))
                .title(" Shortcuts "),
        );

    frame.render_widget(helper, area);
}