envx-tui 0.6.2

Terminal user interface for envx environment variable manager
Documentation
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
use crate::app::{App, ConfirmAction, EditField, Mode};
use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span, Text},
    widgets::{
        Block, Borders, Clear, List, ListItem, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, Wrap,
    },
};

// Version constant - you can also get this from Cargo.toml
const VERSION: &str = env!("CARGO_PKG_VERSION");

pub fn draw(f: &mut Frame, app: &mut App) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(1)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Min(0),    // Main content
            Constraint::Length(3), // Status bar
        ])
        .split(f.area());

    draw_header(f, chunks[0], app);
    draw_main_content(f, chunks[1], app);
    draw_status_bar(f, chunks[2], app);

    // Draw overlays based on mode
    match &app.mode {
        Mode::Edit | Mode::Add => draw_edit_dialog(f, app),
        Mode::Confirm(action) => draw_confirm_dialog(f, action),
        Mode::View(var_name) => draw_view_dialog(f, app, var_name),
        _ => {}
    }
}

fn draw_header(f: &mut Frame, area: Rect, app: &App) {
    let header_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Min(0),     // Title
            Constraint::Length(15), // Version
        ])
        .split(area);

    // Main title
    let title_text = match &app.mode {
        Mode::Search => vec![
            Span::styled("envx ", Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)),
            Span::styled("", Style::default().fg(Color::DarkGray)),
            Span::styled("Search: ", Style::default().fg(Color::Yellow)),
            Span::styled(
                app.search_input.value(),
                Style::default().fg(Color::White).add_modifier(Modifier::ITALIC),
            ),
        ],
        _ => vec![
            Span::styled("envx ", Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)),
            Span::styled("", Style::default().fg(Color::DarkGray)),
            Span::styled("Environment Variable Manager", Style::default().fg(Color::White)),
        ],
    };

    let title = Paragraph::new(Line::from(title_text)).block(
        Block::default()
            .borders(Borders::LEFT | Borders::TOP | Borders::BOTTOM)
            .border_style(Style::default().fg(Color::Cyan)),
    );

    f.render_widget(title, header_chunks[0]);

    // Version info
    let version_text = vec![
        Span::styled("v", Style::default().fg(Color::DarkGray)),
        Span::styled(VERSION, Style::default().fg(Color::Green).add_modifier(Modifier::BOLD)),
    ];

    let version = Paragraph::new(Line::from(version_text))
        .alignment(Alignment::Right)
        .block(
            Block::default()
                .borders(Borders::RIGHT | Borders::TOP | Borders::BOTTOM)
                .border_style(Style::default().fg(Color::Cyan)),
        );

    f.render_widget(version, header_chunks[1]);
}

#[allow(clippy::too_many_lines)]
fn draw_status_bar(f: &mut Frame, area: Rect, app: &App) {
    let status_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Min(0),     // Keybindings
            Constraint::Length(20), // Info section
        ])
        .split(area);

    // Keybindings with color coding
    let keybindings = match &app.mode {
        Mode::Normal => vec![
            Span::styled("↑↓", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)),
            Span::raw("/"),
            Span::styled("jk", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)),
            Span::styled(" Navigate ", Style::default().fg(Color::DarkGray)),
            Span::styled("Enter", Style::default().fg(Color::Green).add_modifier(Modifier::BOLD)),
            Span::raw("/"),
            Span::styled("v", Style::default().fg(Color::Green).add_modifier(Modifier::BOLD)),
            Span::styled(" View ", Style::default().fg(Color::DarkGray)),
            Span::styled("/", Style::default().fg(Color::Magenta).add_modifier(Modifier::BOLD)),
            Span::styled(" Search ", Style::default().fg(Color::DarkGray)),
            Span::styled("a", Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD)),
            Span::styled(" Add ", Style::default().fg(Color::DarkGray)),
            Span::styled("e", Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD)),
            Span::styled(" Edit ", Style::default().fg(Color::DarkGray)),
            Span::styled("d", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
            Span::styled(" Delete ", Style::default().fg(Color::DarkGray)),
            Span::styled("r", Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)),
            Span::styled(" Refresh ", Style::default().fg(Color::DarkGray)),
            Span::styled("q", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
            Span::styled(" Quit", Style::default().fg(Color::DarkGray)),
        ],
        Mode::Search => vec![
            Span::styled("Esc", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
            Span::styled(" Cancel ", Style::default().fg(Color::DarkGray)),
            Span::styled("Enter", Style::default().fg(Color::Green).add_modifier(Modifier::BOLD)),
            Span::styled(" Apply", Style::default().fg(Color::DarkGray)),
        ],
        Mode::Edit | Mode::Add => vec![
            Span::styled("Tab", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)),
            Span::styled(" Switch Field ", Style::default().fg(Color::DarkGray)),
            Span::styled(
                "Ctrl+Enter",
                Style::default().fg(Color::Green).add_modifier(Modifier::BOLD),
            ),
            Span::styled(" Save ", Style::default().fg(Color::DarkGray)),
            Span::styled("Esc", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
            Span::styled(" Cancel", Style::default().fg(Color::DarkGray)),
        ],
        Mode::Confirm(_) => vec![
            Span::styled("y", Style::default().fg(Color::Green).add_modifier(Modifier::BOLD)),
            Span::styled(" Yes ", Style::default().fg(Color::DarkGray)),
            Span::styled("n", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
            Span::styled(" No ", Style::default().fg(Color::DarkGray)),
            Span::styled("Esc", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)),
            Span::styled(" Cancel", Style::default().fg(Color::DarkGray)),
        ],
        Mode::View(_) => vec![
            Span::styled("Esc", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
            Span::raw("/"),
            Span::styled("q", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
            Span::styled(" Back to list", Style::default().fg(Color::DarkGray)),
        ],
    };

    // Add status message if present
    let mut left_content = vec![Line::from(keybindings)];
    if let Some((message, _)) = &app.status_message {
        left_content.push(Line::from(vec![
            Span::styled("", Style::default().fg(Color::DarkGray)),
            Span::styled(
                message,
                Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC),
            ),
        ]));
    }

    let keybindings_widget = Paragraph::new(left_content).block(
        Block::default()
            .borders(Borders::LEFT | Borders::TOP | Borders::BOTTOM)
            .border_style(Style::default().fg(Color::DarkGray)),
    );

    f.render_widget(keybindings_widget, status_chunks[0]);

    // Right info section
    let info_content = if app.filtered_vars.is_empty() {
        vec![Span::styled(
            "No items",
            Style::default().fg(Color::DarkGray).add_modifier(Modifier::ITALIC),
        )]
    } else {
        vec![
            Span::styled("Item ", Style::default().fg(Color::DarkGray)),
            Span::styled(
                format!("{}", app.selected_index + 1),
                Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
            ),
            Span::styled(" of ", Style::default().fg(Color::DarkGray)),
            Span::styled(
                format!("{}", app.filtered_vars.len()),
                Style::default().fg(Color::Green).add_modifier(Modifier::BOLD),
            ),
        ]
    };

    let info_widget = Paragraph::new(Line::from(info_content))
        .alignment(Alignment::Center)
        .block(
            Block::default()
                .borders(Borders::RIGHT | Borders::TOP | Borders::BOTTOM)
                .border_style(Style::default().fg(Color::DarkGray)),
        );

    f.render_widget(info_widget, status_chunks[1]);
}

// ... rest of the functions remain the same ...

fn draw_main_content(f: &mut Frame, area: Rect, app: &mut App) {
    let block = Block::default().borders(Borders::ALL).title(format!(
        "Environment Variables ({}/{})",
        if app.filtered_vars.is_empty() {
            0
        } else {
            app.selected_index + 1
        },
        app.filtered_vars.len()
    ));

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

    // Calculate visible height
    let visible_height = inner_area.height as usize;

    // Update scroll offset based on selection
    app.calculate_scroll(visible_height);

    // Calculate the range of items to display
    let end_index = std::cmp::min(app.scroll_offset + visible_height, app.filtered_vars.len());

    // Create list items for only the visible range
    let items: Vec<ListItem> = app
        .filtered_vars
        .iter()
        .enumerate()
        .skip(app.scroll_offset)
        .take(end_index - app.scroll_offset)
        .map(|(i, var)| {
            let style = if i == app.selected_index {
                Style::default().bg(Color::DarkGray).add_modifier(Modifier::BOLD)
            } else {
                Style::default()
            };

            let source_color = match &var.source {
                envx_core::EnvVarSource::System => Color::Yellow,
                envx_core::EnvVarSource::User => Color::Green,
                envx_core::EnvVarSource::Process => Color::Blue,
                envx_core::EnvVarSource::Shell => Color::Magenta,
                envx_core::EnvVarSource::Application(_) => Color::Cyan,
            };

            let line = Line::from(vec![
                Span::styled(
                    format!("{:<30}", truncate_string(&var.name, 30)),
                    style.fg(Color::White),
                ),
                Span::raw(""),
                Span::styled(
                    format!("{:<50}", truncate_string(&var.value, 50)),
                    style.fg(Color::Gray),
                ),
                Span::raw(""),
                Span::styled(format!("{:?}", var.source), style.fg(source_color)),
            ]);

            ListItem::new(line)
        })
        .collect();

    let list = List::new(items);
    f.render_widget(list, inner_area);

    // Draw scrollbar if needed
    if app.filtered_vars.len() > visible_height {
        let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
            .begin_symbol(Some(""))
            .end_symbol(Some(""));

        let mut scrollbar_state = ScrollbarState::new(app.filtered_vars.len()).position(app.scroll_offset);

        f.render_stateful_widget(
            scrollbar,
            inner_area.inner(ratatui::layout::Margin {
                vertical: 1,
                horizontal: 0,
            }),
            &mut scrollbar_state,
        );
    }

    // Draw selection indicator in the margin
    #[allow(clippy::cast_possible_truncation)]
    if !app.filtered_vars.is_empty() {
        let relative_selected = app.selected_index.saturating_sub(app.scroll_offset);
        if relative_selected < visible_height {
            let y = inner_area.y + relative_selected as u16;
            if y < inner_area.bottom() {
                let selection_indicator =
                    Span::styled("", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD));
                f.render_widget(
                    Paragraph::new(selection_indicator),
                    Rect::new(inner_area.x.saturating_sub(2), y, 2, 1),
                );
            }
        }
    }
}

fn draw_edit_dialog(f: &mut Frame, app: &App) {
    let area = centered_rect(80, 80, f.area());

    let title = if matches!(app.mode, Mode::Add) {
        "Add Variable"
    } else {
        "Edit Variable"
    };

    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .style(Style::default().bg(Color::Black));

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

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(1)
        .constraints([
            Constraint::Length(3), // Name input
            Constraint::Length(1), // Separator
            Constraint::Min(5),    // Value textarea
            Constraint::Length(2), // Help text
        ])
        .split(inner_area);

    // Name input
    let name_style = if app.active_edit_field == EditField::Name {
        Style::default().fg(Color::Yellow)
    } else {
        Style::default()
    };

    let name_input = Paragraph::new(app.edit_name_input.value()).block(
        Block::default()
            .borders(Borders::ALL)
            .title("Name")
            .border_style(name_style),
    );
    f.render_widget(name_input, chunks[0]);

    // Value textarea
    let value_style = if app.active_edit_field == EditField::Value {
        Style::default().fg(Color::Yellow)
    } else {
        Style::default()
    };

    let value_block = Block::default()
        .borders(Borders::ALL)
        .title("Value (supports multiple lines)")
        .border_style(value_style);

    // Render the block first, then the textarea widget inside it
    let inner_value_area = value_block.inner(chunks[2]);
    f.render_widget(value_block, chunks[2]);
    f.render_widget(&app.edit_value_textarea, inner_value_area);

    // Help text
    let help = Paragraph::new("Press Tab to switch fields, Ctrl+Enter to save, Esc to cancel")
        .style(Style::default().fg(Color::DarkGray))
        .alignment(Alignment::Center);
    f.render_widget(help, chunks[3]);
}

fn draw_view_dialog(f: &mut Frame, app: &App, var_name: &str) {
    let area = centered_rect(90, 90, f.area());

    // Find the variable
    let var = app.filtered_vars.iter().find(|v| v.name == var_name);

    if let Some(var) = var {
        let block = Block::default()
            .title(format!("View Variable: {var_name}"))
            .borders(Borders::ALL)
            .style(Style::default().bg(Color::Black));

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

        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .margin(1)
            .constraints([
                Constraint::Length(3), // Variable info
                Constraint::Length(1), // Separator
                Constraint::Min(5),    // Value display
                Constraint::Length(2), // Help text
            ])
            .split(inner_area);

        // Variable info
        let info = vec![
            Line::from(vec![
                Span::styled("Source: ", Style::default().add_modifier(Modifier::BOLD)),
                Span::styled(
                    format!("{:?}", var.source),
                    Style::default().fg(match &var.source {
                        envx_core::EnvVarSource::System => Color::Yellow,
                        envx_core::EnvVarSource::User => Color::Green,
                        envx_core::EnvVarSource::Process => Color::Blue,
                        envx_core::EnvVarSource::Shell => Color::Magenta,
                        envx_core::EnvVarSource::Application(_) => Color::Cyan,
                    }),
                ),
            ]),
            Line::from(vec![
                Span::styled("Modified: ", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(var.modified.format("%Y-%m-%d %H:%M:%S").to_string()),
            ]),
        ];

        let info_widget = Paragraph::new(info).block(Block::default().borders(Borders::NONE));
        f.render_widget(info_widget, chunks[0]);

        // Value display with line numbers
        let value_lines: Vec<Line> = if var.value.lines().count() > 1 {
            var.value
                .lines()
                .enumerate()
                .map(|(i, line)| {
                    Line::from(vec![
                        Span::styled(format!("{:4}", i + 1), Style::default().fg(Color::DarkGray)),
                        Span::raw(line),
                    ])
                })
                .collect()
        } else {
            vec![Line::from(var.value.clone())]
        };

        let value_widget = Paragraph::new(value_lines)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title("Value")
                    .border_style(Style::default().fg(Color::Green)),
            )
            .wrap(Wrap { trim: false })
            .scroll((0, 0)); // Can be made scrollable in the future

        f.render_widget(value_widget, chunks[2]);

        // Help text
        let help = Paragraph::new("Press Esc or q to return to the list")
            .style(Style::default().fg(Color::DarkGray))
            .alignment(Alignment::Center);
        f.render_widget(help, chunks[3]);
    }
}

fn draw_confirm_dialog(f: &mut Frame, action: &ConfirmAction) {
    let area = centered_rect(50, 20, f.area());

    let message = match action {
        ConfirmAction::Delete(name) => format!("Delete variable '{name}'?"),
        ConfirmAction::Save(name, _) => format!("Save variable '{name}'?"),
    };

    let block = Block::default()
        .title("Confirm")
        .borders(Borders::ALL)
        .style(Style::default().bg(Color::Black));

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

    let text = Text::from(vec![
        Line::from(""),
        Line::from(message),
        Line::from(""),
        Line::from("Press [y] to confirm, [n] to cancel"),
    ]);

    let paragraph = Paragraph::new(text)
        .alignment(Alignment::Center)
        .wrap(Wrap { trim: true });

    f.render_widget(paragraph, inner_area);
}

fn centered_rect(percent_x: u16, percent_y: u16, r: 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(r);

    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 truncate_string(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len - 3])
    }
}