rusticity-term 0.1.7

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
use crate::app::App;
use crate::common::{ColumnId, InputFocus, PageSize};
use crate::ui::efs::{
    filtered_access_points, filtered_efs_file_systems, DetailTab, FILTER_CONTROLS,
};

/// Number of access-point columns visible in the preferences selector.
fn ap_col_count(app: &App) -> usize {
    app.efs_state.ap_column_ids.len()
}

/// Number of mount-target columns visible in the preferences selector.
fn mt_col_count(app: &App) -> usize {
    app.efs_state.mt_column_ids.len()
}

/// Toggle a column's visibility within `visible`, keeping at least one visible.
fn toggle_visible(visible: &mut Vec<ColumnId>, all: &[ColumnId], idx: usize) {
    if idx > 0 && idx <= all.len() {
        let col = all[idx - 1];
        if let Some(pos) = visible.iter().position(|c| *c == col) {
            if visible.len() > 1 {
                visible.remove(pos);
            }
        } else {
            visible.push(col);
        }
    }
}

/// Apply a page-size selection when `idx` lands in the page-size section that
/// follows `n` columns (indices `n+3 ..= n+6`). Returns to the first page.
fn set_page_size_by_idx<T>(table: &mut crate::table::TableState<T>, idx: usize, n: usize) {
    let ps = if idx == n + 3 {
        PageSize::Ten
    } else if idx == n + 4 {
        PageSize::TwentyFive
    } else if idx == n + 5 {
        PageSize::Fifty
    } else if idx == n + 6 {
        PageSize::OneHundred
    } else {
        return;
    };
    table.set_page_size(ps);
}

// ── Navigation ────────────────────────────────────────────────────────────────

pub fn next_item(app: &mut App) {
    if app.efs_state.current_file_system.is_some() {
        match app.efs_state.detail_tab {
            DetailTab::Tags => {
                let tag_count = current_fs_tag_count(app);
                if tag_count > 0 {
                    app.efs_state.tags_table.next_item(tag_count);
                }
            }
            DetailTab::AccessPoints => {
                let n = filtered_access_points(app).len();
                if n > 0 {
                    app.efs_state.access_points.next_item(n);
                }
            }
            DetailTab::Network => {
                let n = app.efs_state.mount_targets.items.len();
                if n > 0 {
                    app.efs_state.mount_targets.next_item(n);
                }
            }
            _ => {}
        }
    } else {
        let filtered = filtered_efs_file_systems(app);
        if !filtered.is_empty() {
            app.efs_state.file_systems.next_item(filtered.len());
        }
    }
}

fn current_fs_tag_count(app: &App) -> usize {
    app.efs_state
        .current_file_system
        .as_deref()
        .and_then(|id| {
            app.efs_state
                .file_systems
                .items
                .iter()
                .find(|f| f.file_system_id == id)
        })
        .map(|fs| fs.tags.len())
        .unwrap_or(0)
}

pub fn prev_item(app: &mut App) {
    if app.efs_state.current_file_system.is_some() {
        match app.efs_state.detail_tab {
            DetailTab::Tags => app.efs_state.tags_table.prev_item(),
            DetailTab::AccessPoints => app.efs_state.access_points.prev_item(),
            DetailTab::Network => app.efs_state.mount_targets.prev_item(),
            _ => {}
        }
    } else {
        app.efs_state.file_systems.prev_item();
    }
}

pub fn page_down_filter_input(app: &mut App) {
    if app.efs_state.current_file_system.is_some()
        && app.efs_state.detail_tab == DetailTab::AccessPoints
    {
        let n = filtered_access_points(app).len();
        app.efs_state.access_points.page_down(n);
        return;
    }
    if app.efs_state.input_focus == InputFocus::Filter {
        let filtered = filtered_efs_file_systems(app);
        app.efs_state.file_systems.page_down(filtered.len());
    } else {
        let page_size = app.efs_state.file_systems.page_size.value();
        let filtered_count = filtered_efs_file_systems(app).len();
        app.efs_state.input_focus.handle_page_down(
            &mut app.efs_state.file_systems.selected,
            &mut app.efs_state.file_systems.scroll_offset,
            page_size,
            filtered_count,
        );
    }
}

pub fn page_down_normal(app: &mut App) {
    if app.efs_state.current_file_system.is_some() {
        match app.efs_state.detail_tab {
            DetailTab::AccessPoints => {
                let n = filtered_access_points(app).len();
                app.efs_state.access_points.page_down(n);
            }
            DetailTab::Network => {
                let n = app.efs_state.mount_targets.items.len();
                app.efs_state.mount_targets.page_down(n);
            }
            _ => {}
        }
        return;
    }
    let filtered = filtered_efs_file_systems(app);
    app.efs_state.file_systems.page_down(filtered.len());
}

pub fn page_up_filter_input(app: &mut App) {
    if app.efs_state.current_file_system.is_some()
        && app.efs_state.detail_tab == DetailTab::AccessPoints
    {
        app.efs_state.access_points.page_up();
        return;
    }
    if app.efs_state.input_focus == InputFocus::Filter {
        app.efs_state.file_systems.page_up();
    } else {
        let page_size = app.efs_state.file_systems.page_size.value();
        app.efs_state.input_focus.handle_page_up(
            &mut app.efs_state.file_systems.selected,
            &mut app.efs_state.file_systems.scroll_offset,
            page_size,
        );
    }
}

pub fn page_up_normal(app: &mut App) {
    if app.efs_state.current_file_system.is_some() {
        match app.efs_state.detail_tab {
            DetailTab::AccessPoints => app.efs_state.access_points.page_up(),
            DetailTab::Network => app.efs_state.mount_targets.page_up(),
            _ => {}
        }
        return;
    }
    app.efs_state.file_systems.page_up();
}

pub fn expand_row(app: &mut App) {
    if app.efs_state.current_file_system.is_some() {
        match app.efs_state.detail_tab {
            DetailTab::Tags => app.efs_state.tags_table.toggle_expand(),
            DetailTab::AccessPoints => app.efs_state.access_points.toggle_expand(),
            DetailTab::Network => app.efs_state.mount_targets.toggle_expand(),
            _ => {}
        }
    } else {
        app.efs_state.file_systems.toggle_expand();
    }
}

pub fn prev_pane(app: &mut App) {
    collapse_row(app);
}

pub fn collapse_row(app: &mut App) {
    if app.efs_state.current_file_system.is_some() {
        match app.efs_state.detail_tab {
            DetailTab::Tags => app.efs_state.tags_table.collapse(),
            DetailTab::AccessPoints => app.efs_state.access_points.collapse(),
            DetailTab::Network => app.efs_state.mount_targets.collapse(),
            _ => {}
        }
    } else {
        app.efs_state.file_systems.collapse();
    }
}

pub fn go_to_page(app: &mut App, page: usize) {
    if app.efs_state.current_file_system.is_some()
        && app.efs_state.detail_tab == DetailTab::AccessPoints
    {
        let n = filtered_access_points(app).len();
        app.efs_state.access_points.goto_page(page, n);
        return;
    }
    let filtered_count = filtered_efs_file_systems(app).len();
    app.efs_state.file_systems.goto_page(page, filtered_count);
}

// ── Filter ────────────────────────────────────────────────────────────────────

pub fn get_active_filter_mut(app: &mut App) -> Option<&mut String> {
    if app.efs_state.current_file_system.is_some()
        && app.efs_state.detail_tab == DetailTab::AccessPoints
    {
        return Some(&mut app.efs_state.access_points.filter);
    }
    Some(&mut app.efs_state.file_systems.filter)
}

pub fn apply_filter_reset(app: &mut App) {
    if app.efs_state.current_file_system.is_some()
        && app.efs_state.detail_tab == DetailTab::AccessPoints
    {
        app.efs_state.access_points.reset();
        return;
    }
    app.efs_state.file_systems.reset();
}

pub fn start_filter(app: &mut App) {
    app.efs_state.input_focus = InputFocus::Filter;
}

pub fn next_filter_focus(app: &mut App) {
    app.efs_state.input_focus = app.efs_state.input_focus.next(&FILTER_CONTROLS);
}

pub fn prev_filter_focus(app: &mut App) {
    app.efs_state.input_focus = app.efs_state.input_focus.prev(&FILTER_CONTROLS);
}

pub fn is_pagination_focused(app: &App) -> bool {
    app.efs_state.input_focus == InputFocus::Pagination
}

// ── Yank / console URL / breadcrumb ──────────────────────────────────────────

pub fn yank(app: &App) {
    use crate::app::copy_to_clipboard;
    if let Some(fs_id) = &app.efs_state.current_file_system {
        // On the File system policy tab, copy the policy JSON.
        if app.efs_state.detail_tab == crate::ui::efs::DetailTab::FileSystemPolicy
            && !app.efs_state.policy_document.is_empty()
        {
            copy_to_clipboard(&app.efs_state.policy_document);
            return;
        }
        // Otherwise in detail view — copy the ARN
        if let Some(fs) = app
            .efs_state
            .file_systems
            .items
            .iter()
            .find(|f| &f.file_system_id == fs_id)
        {
            copy_to_clipboard(&fs.file_system_arn);
        }
    } else {
        // In list view — copy the file system ID
        let filtered = filtered_efs_file_systems(app);
        if let Some(fs) = app.efs_state.file_systems.get_selected(&filtered) {
            copy_to_clipboard(&fs.file_system_id);
        }
    }
}

// ── Select / go back ──────────────────────────────────────────────────────────

pub fn select_item(app: &mut App) {
    if app.efs_state.current_file_system.is_none() {
        let filtered = filtered_efs_file_systems(app);
        if let Some(fs) = app.efs_state.file_systems.get_selected(&filtered) {
            app.efs_state.current_file_system = Some(fs.file_system_id.clone());
            app.efs_state.detail_tab = crate::ui::efs::DetailTab::MeteredSize;
        }
    }
}

pub fn go_back(app: &mut App) {
    app.efs_state.current_file_system = None;
    app.efs_state.detail_tab = crate::ui::efs::DetailTab::MeteredSize;
}

pub fn next_detail_tab(app: &mut App) {
    use crate::common::CyclicEnum;
    app.efs_state.detail_tab = app.efs_state.detail_tab.next();
    trigger_tab_load(app);
}

pub fn prev_detail_tab(app: &mut App) {
    use crate::common::CyclicEnum;
    app.efs_state.detail_tab = app.efs_state.detail_tab.prev();
    trigger_tab_load(app);
}

/// When switching to the Monitoring tab, mark metrics as loading so the event
/// loop fetches fresh CloudWatch data and reset the chart scroll. When switching
/// to the File system policy tab, mark the policy as loading.
fn trigger_tab_load(app: &mut App) {
    use crate::ui::monitoring::MonitoringState;
    match app.efs_state.detail_tab {
        crate::ui::efs::DetailTab::Monitoring => {
            app.efs_state.set_metrics_loading(true);
            app.efs_state.set_monitoring_scroll(0);
            app.efs_state.clear_metrics();
        }
        crate::ui::efs::DetailTab::FileSystemPolicy => {
            app.efs_state.policy_loading = true;
            app.efs_state.policy_scroll = 0;
        }
        crate::ui::efs::DetailTab::AccessPoints => {
            app.efs_state.ap_loading = true;
            app.efs_state.access_points.reset();
        }
        crate::ui::efs::DetailTab::Network => {
            app.efs_state.mt_loading = true;
            app.efs_state.mount_targets.reset();
        }
        crate::ui::efs::DetailTab::Replication => {
            app.efs_state.replication_loading = true;
        }
        _ => {}
    }
}

/// Number of charts in the EFS Monitoring tab; the last scrollable index is
/// `EFS_MONITORING_CHARTS - 1`.
const EFS_MONITORING_CHARTS: usize = 6;

/// Scroll the active detail tab down (Ctrl+D / ScrollDown).
pub fn scroll_down_detail(app: &mut App) {
    use crate::ui::monitoring::MonitoringState;
    match app.efs_state.detail_tab {
        crate::ui::efs::DetailTab::Monitoring => {
            let max = EFS_MONITORING_CHARTS.saturating_sub(1);
            app.efs_state
                .set_monitoring_scroll((app.efs_state.monitoring_scroll() + 1).min(max));
        }
        crate::ui::efs::DetailTab::FileSystemPolicy => {
            let max = app
                .efs_state
                .policy_document
                .lines()
                .count()
                .saturating_sub(1);
            app.efs_state.policy_scroll = (app.efs_state.policy_scroll + 1).min(max);
        }
        _ => {}
    }
}

/// Scroll the active detail tab up (Ctrl+U / ScrollUp).
pub fn scroll_up_detail(app: &mut App) {
    use crate::ui::monitoring::MonitoringState;
    match app.efs_state.detail_tab {
        crate::ui::efs::DetailTab::Monitoring => {
            app.efs_state
                .set_monitoring_scroll(app.efs_state.monitoring_scroll().saturating_sub(1));
        }
        crate::ui::efs::DetailTab::FileSystemPolicy => {
            app.efs_state.policy_scroll = app.efs_state.policy_scroll.saturating_sub(1);
        }
        _ => {}
    }
}

pub fn breadcrumb(app: &App) -> Vec<String> {
    let mut parts = vec!["EFS".to_string()];
    if let Some(fs_id) = &app.efs_state.current_file_system {
        parts.push(fs_id.clone());
    } else {
        parts.push("File Systems".to_string());
    }
    parts
}

pub fn console_url(app: &App) -> String {
    use crate::efs;
    // In detail view, link to the current file system's active tab.
    if let Some(fs_id) = &app.efs_state.current_file_system {
        return efs::console_url_file_system_with_tab(
            &app.config.region,
            fs_id,
            app.efs_state.detail_tab,
        );
    }
    let filtered = filtered_efs_file_systems(app);
    if let Some(fs) = app.efs_state.file_systems.get_selected(&filtered) {
        efs::console_url_file_system(&app.config.region, &fs.file_system_id)
    } else {
        efs::console_url_file_systems(&app.config.region)
    }
}

// ── Column preferences ────────────────────────────────────────────────────────

pub fn toggle_column(app: &mut App) {
    let idx = app.column_selector_index;
    if app.efs_state.current_file_system.is_some() {
        match app.efs_state.detail_tab {
            DetailTab::Tags => {
                // Key/Value columns are always visible — only page size configurable.
                set_page_size_by_idx(&mut app.efs_state.tags_table, idx, 2);
                return;
            }
            DetailTab::AccessPoints => {
                let all = app.efs_state.ap_column_ids.clone();
                toggle_visible(&mut app.efs_state.ap_visible_column_ids, &all, idx);
                set_page_size_by_idx(&mut app.efs_state.access_points, idx, all.len());
                return;
            }
            DetailTab::Network => {
                let all = app.efs_state.mt_column_ids.clone();
                toggle_visible(&mut app.efs_state.mt_visible_column_ids, &all, idx);
                set_page_size_by_idx(&mut app.efs_state.mount_targets, idx, all.len());
                return;
            }
            _ => return,
        }
    }
    // File systems list.
    if idx > 0 && idx <= app.efs_column_ids.len() {
        if let Some(col) = app.efs_column_ids.get(idx - 1) {
            if let Some(pos) = app.efs_visible_column_ids.iter().position(|c| c == col) {
                if app.efs_visible_column_ids.len() > 1 {
                    app.efs_visible_column_ids.remove(pos);
                }
            } else {
                app.efs_visible_column_ids.push(*col);
            }
        }
    } else {
        set_page_size_by_idx(
            &mut app.efs_state.file_systems,
            idx,
            app.efs_column_ids.len(),
        );
    }
}

/// Number of toggleable columns for the active view (used by the prefs cursor).
fn active_prefs_col_count(app: &App) -> usize {
    if app.efs_state.current_file_system.is_some() {
        match app.efs_state.detail_tab {
            DetailTab::Tags => 2,
            DetailTab::AccessPoints => ap_col_count(app),
            DetailTab::Network => mt_col_count(app),
            _ => app.efs_column_ids.len(),
        }
    } else {
        app.efs_column_ids.len()
    }
}

pub fn next_preferences(app: &mut App) {
    let page_size_idx = active_prefs_col_count(app) + 2;
    if app.column_selector_index < page_size_idx {
        app.column_selector_index = page_size_idx;
    } else {
        app.column_selector_index = 0;
    }
}

pub fn prev_preferences(app: &mut App) {
    let page_size_idx = active_prefs_col_count(app) + 2;
    if app.column_selector_index >= page_size_idx {
        app.column_selector_index = 0;
    } else {
        app.column_selector_index = page_size_idx;
    }
}

pub fn column_selector_max(app: &App) -> usize {
    active_prefs_col_count(app) + 6
}

pub fn column_count(app: &App) -> usize {
    active_prefs_col_count(app)
}