rusticity-term 0.1.6

Terminal UI components for Rusticity
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
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
use crate::app::{App, Service, ViewMode};
use crate::keymap::Mode;
use crate::ui::cfn::DetailTab;
use crate::ui::red_text;
use ratatui::{prelude::*, widgets::*};
use std::time::{SystemTime, UNIX_EPOCH};

pub const SPINNER_FRAMES: &[&str] = &["", "", "", "", "", "", "", ""];

pub fn first_hint(key: &'static str, action: &'static str) -> Vec<Span<'static>> {
    vec![
        Span::styled(key, red_text()),
        Span::raw(" "),
        Span::raw(action),
        Span::raw(""),
    ]
}

pub fn hint(key: &'static str, action: &'static str) -> Vec<Span<'static>> {
    vec![
        Span::raw(" "),
        Span::styled(key, red_text()),
        Span::raw(" "),
        Span::raw(action),
        Span::raw(""),
    ]
}

pub fn last_hint(key: &'static str, action: &'static str) -> Vec<Span<'static>> {
    vec![
        Span::raw(" "),
        Span::styled(key, red_text()),
        Span::raw(" "),
        Span::raw(action),
    ]
}

fn common_detail_hotkeys() -> Vec<Span<'static>> {
    let mut spans = vec![];
    spans.extend(first_hint("↑↓", "scroll"));
    spans.extend(hint("", "back"));
    spans.extend(hint("[]", "switch"));
    spans.extend(hint("⇤⇥", "switch"));
    spans.extend(hint("^u", "page up"));
    spans.extend(hint("^d", "page down"));
    spans.extend(hint("y", "yank"));
    spans.extend(hint("^o", "console"));
    spans.extend(hint("p", "preferences"));
    spans.extend(hint("^p", "print"));
    spans.extend(hint("^r", "refresh"));
    spans.extend(hint("^w", "close"));
    spans.extend(last_hint("q", "quit"));
    spans
}

fn common_list_hotkeys() -> Vec<Span<'static>> {
    let mut spans = vec![];
    spans.extend(first_hint("↑↓", "scroll"));
    spans.extend(hint("←→", "toggle"));
    spans.extend(hint("", "open"));
    spans.extend(hint("[]", "switch"));
    spans.extend(hint("^u", "page up"));
    spans.extend(hint("^d", "page down"));
    spans.extend(hint("y", "yank"));
    spans.extend(hint("^o", "console"));
    spans.extend(hint("p", "preferences"));
    spans.extend(hint("^p", "print"));
    spans.extend(hint("^r", "refresh"));
    spans.extend(hint("^w", "close"));
    spans.extend(last_hint("q", "quit"));
    spans
}

pub fn render_bottom_bar(frame: &mut Frame, app: &App, area: Rect) {
    let is_insert = match app.mode {
        Mode::FilterInput | Mode::EventFilterInput | Mode::InsightsInput => true,
        Mode::ServicePicker | Mode::SpaceMenu => app.service_picker.filter_active,
        Mode::RegionPicker => app.region_filter_active,
        Mode::SessionPicker => app.session_filter_active,
        Mode::ProfilePicker => app.profile_filter_active,
        _ => false,
    };

    let mode_indicator = if is_insert { " INSERT " } else { " NORMAL " };
    let mode_style = if is_insert {
        Style::default().bg(Color::Yellow).fg(Color::Black)
    } else {
        Style::default().bg(Color::Blue).fg(Color::White)
    };

    let help = if app.mode == Mode::ColumnSelector {
        let mut hints = vec![];
        hints.extend(hint("↑↓", "scroll"));
        hints.extend(hint("", "toggle"));

        if app.current_service == Service::CloudWatchAlarms {
            hints.extend(hint("⇤⇥", "switch"));
        }

        hints.extend(last_hint("", "close"));
        hints
    } else if app.mode == Mode::InsightsInput {
        let mut hints = vec![];
        hints.extend(hint(" tab", "switch"));
        hints.extend(hint("↑↓", "scroll"));
        hints.extend(hint("", "toggle"));
        hints.extend(hint("", "execute"));
        hints.extend(hint("", "cancel"));
        hints.extend(hint("^r", "refresh"));
        hints.extend(last_hint("^w", "close"));
        hints
    } else if app.current_service == Service::CloudWatchInsights {
        let mut hints = vec![];
        hints.extend(hint(" i", "insert"));
        hints.extend(hint("", "execute"));
        hints.extend(hint("?", "help"));
        hints.extend(hint("^r", "refresh"));
        hints.extend(hint("^o", "console"));
        hints.extend(hint("", "back"));
        hints.extend(hint("^w", "close"));
        hints.extend(last_hint("q", "quit"));
        hints
    } else if app.mode == Mode::EventFilterInput {
        let mut hints = vec![];
        hints.extend(first_hint("⇤⇥", "switch"));
        hints.extend(hint("", "change unit"));
        hints.extend(hint("", "apply"));
        hints.extend(hint("", "cancel"));
        hints.extend(last_hint("^w", "close"));
        hints
    } else if app.mode == Mode::RegionPicker {
        let mut hints = vec![];
        hints.extend(first_hint("↑↓", "scroll"));
        hints.extend(hint("i", "filter"));
        hints.extend(hint("", "select"));
        hints.extend(hint("^l", "measure latency"));
        hints.extend(hint("", "close"));
        hints.extend(last_hint("q", "quit"));
        hints
    } else if app.mode == Mode::FilterInput {
        let mut hints = vec![];
        hints.extend(first_hint("", "apply"));
        hints.extend(hint("", "cancel"));
        hints.extend(hint("", "toggle"));
        hints.extend(hint("⇤⇥", "switch"));
        hints.extend(last_hint("^w", "close"));
        hints
    } else if app.view_mode == ViewMode::Events {
        let mut hints = vec![];
        hints.extend(first_hint("↑↓", "scroll"));
        hints.extend(hint("←→", "toggle"));
        hints.extend(hint("y", "yank"));
        hints.extend(hint("^o", "console"));
        hints.extend(hint("^r", "refresh"));
        hints.extend(hint("p", "preferences"));
        hints.extend(hint("^p", "print"));
        hints.extend(hint("^w", "close"));
        hints.extend(last_hint("q", "quit"));
        hints
    } else if app.view_mode == ViewMode::Detail {
        let mut hints = vec![];
        hints.extend(first_hint("↑↓", "scroll"));
        hints.extend(hint("←→", "toggle"));
        hints.extend(hint("", "open"));
        hints.extend(hint("", "back"));
        hints.extend(hint("i", "insert"));
        hints.extend(hint("s", "sort"));
        hints.extend(hint("o", "order"));
        hints.extend(hint("<num>p", "page"));
        hints.extend(hint("^o", "console"));
        hints.extend(hint("⇤⇥", "switch"));
        hints.extend(hint("^r", "refresh"));
        hints.extend(hint("p", "preferences"));
        hints.extend(hint("^p", "print"));
        hints.extend(hint("^w", "close"));
        hints.extend(last_hint("q", "quit"));
        hints
    } else if app.current_service == Service::EcrRepositories {
        if app.ecr_state.current_repository.is_some() {
            let mut hints = vec![];
            hints.extend(first_hint("↑↓", "scroll"));
            hints.extend(hint("←→", "toggle"));
            hints.extend(hint("", "back"));
            hints.extend(hint("y", "yank"));
            hints.extend(hint("^o", "console"));
            hints.extend(hint("^r", "refresh"));
            hints.extend(hint("^w", "close"));
            hints.extend(last_hint("q", "quit"));
            hints
        } else {
            let mut hints = vec![];
            hints.extend(first_hint("↑↓", "scroll"));
            hints.extend(hint("←→", "toggle"));
            hints.extend(hint("", "open"));
            hints.extend(hint("⇤⇥", "switch"));
            hints.extend(hint("y", "yank"));
            hints.extend(hint("^o", "console"));
            hints.extend(hint("^r", "refresh"));
            hints.extend(hint("^w", "close"));
            hints.extend(last_hint("q", "quit"));
            hints
        }
    } else if app.current_service == Service::S3Buckets {
        if app.s3_state.current_bucket.is_some() {
            let mut hints = vec![];
            hints.extend(first_hint("↑↓", "scroll"));
            hints.extend(hint("←→", "toggle"));
            hints.extend(hint("", "open"));
            hints.extend(hint("", "back"));
            hints.extend(hint("⇤⇥", "switch"));
            hints.extend(hint("^o", "console"));
            hints.extend(hint("^r", "refresh"));
            hints.extend(hint("^w", "close"));
            hints.extend(last_hint("q", "quit"));
            hints
        } else {
            let mut hints = vec![];
            hints.extend(first_hint("↑↓", "scroll"));
            hints.extend(hint("←→", "toggle"));
            hints.extend(hint("", "open"));
            hints.extend(hint("⇤⇥", "switch"));
            hints.extend(hint("^o", "console"));
            hints.extend(hint("^r", "refresh"));
            hints.extend(hint("^w", "close"));
            hints.extend(last_hint("q", "quit"));
            hints
        }
    } else if app.current_service == Service::CloudFormationStacks {
        if app.cfn_state.current_stack.is_some() {
            // In stack detail view - customize hints based on tab
            if app.cfn_state.detail_tab == DetailTab::Template
                || app.cfn_state.detail_tab == DetailTab::GitSync
            {
                // Template and GitSync tabs: no preferences
                let mut hints = vec![];
                hints.extend(first_hint("↑↓", "scroll"));
                hints.extend(hint("", "back"));
                hints.extend(hint("[]", "switch"));
                hints.extend(hint("⇤⇥", "switch"));
                hints.extend(hint("^u", "page up"));
                hints.extend(hint("^d", "page down"));
                hints.extend(hint("y", "yank"));
                hints.extend(hint("^o", "console"));
                hints.extend(hint("^r", "refresh"));
                hints.extend(hint("^w", "close"));
                hints.extend(last_hint("q", "quit"));
                hints
            } else {
                common_detail_hotkeys()
            }
        } else {
            // In stack list view - build custom hints with snapshot
            let mut hints = vec![];
            hints.extend(first_hint("↑↓", "scroll"));
            hints.extend(hint("←→", "toggle"));
            hints.extend(hint("", "open"));
            hints.extend(hint("[]", "switch"));
            hints.extend(hint("^u", "page up"));
            hints.extend(hint("^d", "page down"));
            hints.extend(hint("y", "yank"));
            hints.extend(hint("^p", "snapshot"));
            hints.extend(hint("^o", "console"));
            hints.extend(hint("^r", "refresh"));
            hints.extend(hint("^w", "close"));
            hints.extend(last_hint("q", "quit"));
            hints
        }
    } else if app.current_service == Service::IamUsers {
        if app.iam_state.current_user.is_some() {
            common_detail_hotkeys()
        } else {
            common_list_hotkeys()
        }
    } else if app.current_service == Service::IamRoles {
        if app.iam_state.current_role.is_some() {
            common_detail_hotkeys()
        } else {
            common_list_hotkeys()
        }
    } else if app.current_service == Service::LambdaFunctions {
        if app.lambda_state.current_function.is_some() {
            common_detail_hotkeys()
        } else {
            common_list_hotkeys()
        }
    } else if app.view_mode == ViewMode::List {
        let mut hints = vec![];
        hints.extend(first_hint("↑↓", "scroll"));
        hints.extend(hint("←→", "toggle"));
        hints.extend(hint("", "open"));
        hints.extend(hint("^o", "console"));
        hints.extend(hint("p", "preferences"));
        hints.extend(hint("^p", "print"));
        hints.extend(hint("^r", "refresh"));
        hints.extend(hint("^w", "close"));
        hints.extend(last_hint("q", "quit"));
        hints
    } else {
        let mut hints = vec![];
        hints.extend(first_hint("↑↓", "scroll"));
        hints.extend(hint("←→", "toggle"));
        hints.extend(hint("", "open"));
        hints.extend(hint("", "back"));
        hints.extend(hint("<num>p", "page"));
        hints.extend(hint("^o", "console"));
        hints.extend(hint("^r", "refresh"));
        hints.extend(hint("p", "preferences"));
        hints.extend(hint("^p", "print"));
        hints.extend(hint("^w", "close"));
        hints.extend(last_hint("q", "quit"));
        hints
    };

    let millis = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis();

    let spinner_frame = if app.log_groups_state.loading {
        SPINNER_FRAMES[(millis / 100 % SPINNER_FRAMES.len() as u128) as usize]
    } else {
        " "
    };

    let status_line_temp = if app.log_groups_state.loading {
        let max_width = area.width.saturating_sub(10) as usize;
        let msg = if app.log_groups_state.loading_message.len() > max_width {
            format!(
                "{}...",
                &app.log_groups_state.loading_message[..max_width.saturating_sub(3)]
            )
        } else {
            app.log_groups_state.loading_message.clone()
        };
        Some(Line::from(vec![Span::raw(msg)]))
    } else if !app.page_input.is_empty() {
        Some(Line::from(vec![Span::raw(format!(
            "Go to page {} (press p to confirm)",
            app.page_input
        ))]))
    } else {
        None
    };

    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(8),
            Constraint::Length(2),
            Constraint::Min(0),
        ])
        .split(area);

    let mode_widget = Paragraph::new(mode_indicator).style(mode_style);

    let spinner_widget = Paragraph::new(spinner_frame)
        .block(Block::default())
        .style(Style::default().bg(Color::DarkGray).fg(Color::Yellow));

    if let Some(line) = status_line_temp {
        let status_widget = Paragraph::new(line)
            .alignment(Alignment::Left)
            .style(Style::default().bg(Color::DarkGray).fg(Color::White));
        frame.render_widget(mode_widget, chunks[0]);
        frame.render_widget(spinner_widget, chunks[1]);
        frame.render_widget(status_widget, chunks[2]);
    } else {
        // Build version string
        let version = env!("CARGO_PKG_VERSION");
        let commit = option_env!("GIT_HASH").unwrap_or("unknown");
        let version_text = format!("⋮ RUSTICITY v{} (#{})", version, commit);

        let colors = [
            Color::Red,
            Color::Green,
            Color::Yellow,
            Color::Blue,
            Color::Magenta,
            Color::Cyan,
        ];
        let seed = millis as usize;
        let version_spans: Vec<Span> = version_text
            .chars()
            .enumerate()
            .map(|(i, c)| {
                let color = colors[(seed + i) % colors.len()];
                Span::styled(c.to_string(), Style::default().fg(color))
            })
            .collect();
        let version_line = Line::from(version_spans);
        let version_width = version_text.len() as u16;

        // Split status area into help (left) and version (right)
        let status_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Min(0), Constraint::Length(version_width)])
            .split(chunks[2]);

        let help_widget = Paragraph::new(Line::from(help))
            .block(Block::default())
            .alignment(Alignment::Left)
            .style(Style::default().bg(Color::DarkGray).fg(Color::White));

        let version_widget = Paragraph::new(version_line)
            .alignment(Alignment::Right)
            .style(Style::default().bg(Color::DarkGray).fg(Color::White));

        frame.render_widget(mode_widget, chunks[0]);
        frame.render_widget(spinner_widget, chunks[1]);
        frame.render_widget(help_widget, status_chunks[0]);
        frame.render_widget(version_widget, status_chunks[1]);
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_version_string_format() {
        let version = env!("CARGO_PKG_VERSION");
        let commit = option_env!("GIT_HASH").unwrap_or("unknown");
        let version_text = format!("RUSTICITY v{} (#{})", version, commit);

        assert!(version_text.starts_with("RUSTICITY v"));
        assert!(version_text.contains("#"));
    }

    #[test]
    fn test_version_padding_calculation() {
        // Simulate help text width
        let help_text = " ↑↓ scroll ⋮ ←→ toggle ⋮ ⏎ open ⋮ ^o console ⋮ p preferences ⋮ ^p print ⋮ ^r refresh ⋮ ^w close ⋮ q quit ";
        let help_width: usize = help_text.len();

        let version = env!("CARGO_PKG_VERSION");
        let commit = option_env!("GIT_HASH").unwrap_or("unknown");
        let version_text = format!("RUSTICITY v{} (#{})", version, commit);
        let version_width: usize = version_text.len();

        // Simulate terminal width of 200
        let status_width: usize = 200;
        let padding: usize = status_width
            .saturating_sub(help_width)
            .saturating_sub(version_width);

        // Total should equal status_width
        assert_eq!(help_width + padding + version_width, status_width);
    }

    #[test]
    fn test_preferences_hint_uses_p_not_ctrl_p() {
        use super::common_list_hotkeys;
        let spans = common_list_hotkeys();
        let text: String = spans.iter().map(|s| s.content.as_ref()).collect();

        // Should have "p preferences" not "^p preferences"
        assert!(
            text.contains("p preferences"),
            "Should show 'p preferences'"
        );
        assert!(
            !text.contains("^p preferences"),
            "Should not show '^p preferences'"
        );
    }

    #[test]
    fn test_print_hint_uses_ctrl_p() {
        use super::common_list_hotkeys;
        let spans = common_list_hotkeys();
        let text: String = spans.iter().map(|s| s.content.as_ref()).collect();

        // Should have "^p print" for copy to clipboard
        assert!(
            text.contains("^p print"),
            "Should show '^p print' for copy to clipboard"
        );
    }

    #[test]
    fn test_yank_hint_uses_y() {
        use super::common_list_hotkeys;
        let spans = common_list_hotkeys();
        let text: String = spans.iter().map(|s| s.content.as_ref()).collect();

        // Should have "y yank" for copying selected item
        assert!(
            text.contains("y yank"),
            "Should show 'y yank' for copying selected item"
        );
    }

    #[test]
    fn test_common_detail_hotkeys_has_correct_hints() {
        use super::common_detail_hotkeys;
        let spans = common_detail_hotkeys();
        let text: String = spans.iter().map(|s| s.content.as_ref()).collect();

        // Detail views should have p for preferences and ^p for print
        assert!(
            text.contains("p preferences"),
            "Detail view should show 'p preferences'"
        );
        assert!(
            text.contains("^p print"),
            "Detail view should show '^p print'"
        );
    }

    #[test]
    fn test_no_columns_hint_anywhere() {
        let list_spans = super::common_list_hotkeys();
        let list_text: String = list_spans.iter().map(|s| s.content.as_ref()).collect();

        // Should never show "columns", always "preferences"
        assert!(
            !list_text.contains("p columns"),
            "Should not show 'p columns', use 'p preferences' instead"
        );

        let detail_spans = super::common_detail_hotkeys();
        let detail_text: String = detail_spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(
            !detail_text.contains("p columns"),
            "Should not show 'p columns', use 'p preferences' instead"
        );
    }

    #[test]
    fn test_all_views_have_print_hotkey() {
        // Test list view
        let list_spans = super::common_list_hotkeys();
        let list_text: String = list_spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(
            list_text.contains("^p print"),
            "List view must have '^p print' hotkey"
        );

        // Test detail view
        let detail_spans = super::common_detail_hotkeys();
        let detail_text: String = detail_spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(
            detail_text.contains("^p print"),
            "Detail view must have '^p print' hotkey"
        );
    }

    #[test]
    fn test_preferences_always_uses_p_not_ctrl_p() {
        let list_spans = super::common_list_hotkeys();
        let list_text: String = list_spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(
            list_text.contains("p preferences"),
            "Should use 'p preferences'"
        );
        assert!(
            !list_text.contains("^p preferences"),
            "Should not use '^p preferences'"
        );

        let detail_spans = super::common_detail_hotkeys();
        let detail_text: String = detail_spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(
            detail_text.contains("p preferences"),
            "Should use 'p preferences'"
        );
        assert!(
            !detail_text.contains("^p preferences"),
            "Should not use '^p preferences'"
        );
    }
}