rusticity-term 0.1.8

Terminal UI components for Rusticity
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
use crate::app::{App, Service};
use crate::common::{CyclicEnum, InputFocus};
use crate::preferences::bindings;
use crate::ui::dynamodb::{
    current_table, filtered_dynamodb_tables, filtered_items, DetailTab, ItemOperation,
    ItemProjection, FILTER_CONTROLS, ITEMS_FILTER_CONTROLS, ITEM_INDEX, ITEM_OPERATION,
    ITEM_PROJECTION,
};

fn current_index_count(app: &App) -> usize {
    current_table(app).map(|t| t.index_count()).unwrap_or(0)
}

fn current_replica_count(app: &App) -> usize {
    current_table(app).map(|t| t.replicas.len()).unwrap_or(0)
}

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

pub fn next_item(app: &mut App) {
    if app.dynamodb_state.current_table.is_some() {
        match app.dynamodb_state.detail_tab {
            DetailTab::Items => {
                let n = filtered_items(app).len();
                if n > 0 {
                    app.dynamodb_state.items.next_item(n);
                }
            }
            DetailTab::Indexes => {
                let n = current_index_count(app);
                if n > 0 {
                    app.dynamodb_state.indexes_table.next_item(n);
                }
            }
            DetailTab::GlobalTables => {
                let n = current_replica_count(app);
                if n > 0 {
                    app.dynamodb_state.replicas_table.next_item(n);
                }
            }
            DetailTab::Backups => {
                let n = app.dynamodb_state.backups.items.len();
                if n > 0 {
                    app.dynamodb_state.backups.next_item(n);
                }
            }
            DetailTab::Tags => {
                let n = app.dynamodb_state.tags_table.items.len();
                if n > 0 {
                    app.dynamodb_state.tags_table.next_item(n);
                }
            }
            _ => {}
        }
        return;
    }
    let filtered = filtered_dynamodb_tables(app);
    if !filtered.is_empty() {
        app.dynamodb_state.tables.next_item(filtered.len());
    }
}

pub fn prev_item(app: &mut App) {
    if app.dynamodb_state.current_table.is_some() {
        match app.dynamodb_state.detail_tab {
            DetailTab::Items => app.dynamodb_state.items.prev_item(),
            DetailTab::Indexes => app.dynamodb_state.indexes_table.prev_item(),
            DetailTab::GlobalTables => app.dynamodb_state.replicas_table.prev_item(),
            DetailTab::Backups => app.dynamodb_state.backups.prev_item(),
            DetailTab::Tags => app.dynamodb_state.tags_table.prev_item(),
            _ => {}
        }
        return;
    }
    app.dynamodb_state.tables.prev_item();
}

pub fn page_down_filter_input(app: &mut App) {
    if is_items_tab(app) {
        if app.dynamodb_state.input_focus == InputFocus::Filter {
            page_down_normal(app);
        } else {
            let count = filtered_items(app).len();
            let state = &mut app.dynamodb_state;
            state.input_focus.handle_page_down(
                &mut state.items.selected,
                &mut state.items.scroll_offset,
                state.items.page_size.value(),
                count,
            );
        }
        return;
    }
    if app.dynamodb_state.input_focus == InputFocus::Filter {
        let filtered = filtered_dynamodb_tables(app);
        app.dynamodb_state.tables.page_down(filtered.len());
    } else {
        let page_size = app.dynamodb_state.tables.page_size.value();
        let filtered_count = filtered_dynamodb_tables(app).len();
        app.dynamodb_state.input_focus.handle_page_down(
            &mut app.dynamodb_state.tables.selected,
            &mut app.dynamodb_state.tables.scroll_offset,
            page_size,
            filtered_count,
        );
    }
}

pub fn page_down_normal(app: &mut App) {
    if is_items_tab(app) {
        let n = filtered_items(app).len();
        app.dynamodb_state.items.page_down(n);
        return;
    }
    let filtered = filtered_dynamodb_tables(app);
    app.dynamodb_state.tables.page_down(filtered.len());
}

pub fn page_up_filter_input(app: &mut App) {
    if is_items_tab(app) {
        if app.dynamodb_state.input_focus == InputFocus::Filter {
            page_up_normal(app);
        } else {
            let state = &mut app.dynamodb_state;
            state.input_focus.handle_page_up(
                &mut state.items.selected,
                &mut state.items.scroll_offset,
                state.items.page_size.value(),
            );
        }
        return;
    }
    if app.dynamodb_state.input_focus == InputFocus::Filter {
        app.dynamodb_state.tables.page_up();
    } else {
        let page_size = app.dynamodb_state.tables.page_size.value();
        app.dynamodb_state.input_focus.handle_page_up(
            &mut app.dynamodb_state.tables.selected,
            &mut app.dynamodb_state.tables.scroll_offset,
            page_size,
        );
    }
}

pub fn page_up_normal(app: &mut App) {
    if is_items_tab(app) {
        app.dynamodb_state.items.page_up();
        return;
    }
    app.dynamodb_state.tables.page_up();
}

/// True when the Items explorer is the active view.
pub fn is_items_tab(app: &App) -> bool {
    app.dynamodb_state.current_table.is_some() && app.dynamodb_state.detail_tab == DetailTab::Items
}

pub fn expand_row(app: &mut App) {
    if app.dynamodb_state.current_table.is_some() {
        match app.dynamodb_state.detail_tab {
            DetailTab::Items => app.dynamodb_state.items.toggle_expand(),
            DetailTab::Indexes => app.dynamodb_state.indexes_table.toggle_expand(),
            DetailTab::GlobalTables => app.dynamodb_state.replicas_table.toggle_expand(),
            DetailTab::Backups => app.dynamodb_state.backups.toggle_expand(),
            DetailTab::Tags => app.dynamodb_state.tags_table.toggle_expand(),
            _ => {}
        }
    } else {
        app.dynamodb_state.tables.toggle_expand();
    }
}

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

pub fn collapse_row(app: &mut App) {
    if app.dynamodb_state.current_table.is_some() {
        match app.dynamodb_state.detail_tab {
            DetailTab::Items => app.dynamodb_state.items.collapse(),
            DetailTab::Indexes => app.dynamodb_state.indexes_table.collapse(),
            DetailTab::GlobalTables => app.dynamodb_state.replicas_table.collapse(),
            DetailTab::Backups => app.dynamodb_state.backups.collapse(),
            DetailTab::Tags => app.dynamodb_state.tags_table.collapse(),
            _ => {}
        }
    } else {
        app.dynamodb_state.tables.collapse();
    }
}

pub fn go_to_page(app: &mut App, page: usize) {
    if is_items_tab(app) {
        let count = filtered_items(app).len();
        app.dynamodb_state.items.goto_page(page.max(1), count);
        return;
    }
    let filtered_count = filtered_dynamodb_tables(app).len();
    app.dynamodb_state.tables.goto_page(page, filtered_count);
}

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

pub fn get_active_filter_mut(app: &mut App) -> Option<&mut String> {
    if is_items_tab(app) {
        return Some(&mut app.dynamodb_state.item_filter);
    }
    Some(&mut app.dynamodb_state.tables.filter)
}

pub fn apply_filter_reset(app: &mut App) {
    if is_items_tab(app) {
        app.dynamodb_state.item_filter.clear();
        reload_items(app);
        return;
    }
    app.dynamodb_state.tables.reset();
}

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

pub fn next_filter_focus(app: &mut App) {
    let controls = filter_controls(app);
    app.dynamodb_state.input_focus = app.dynamodb_state.input_focus.next(controls);
}

pub fn prev_filter_focus(app: &mut App) {
    let controls = filter_controls(app);
    app.dynamodb_state.input_focus = app.dynamodb_state.input_focus.prev(controls);
}

/// The Items tab adds the operation, index and projection dropdowns.
fn filter_controls(app: &App) -> &'static [InputFocus] {
    if is_items_tab(app) {
        &ITEMS_FILTER_CONTROLS
    } else {
        &FILTER_CONTROLS
    }
}

// ── Items tab dropdowns ───────────────────────────────────────────────────────

/// Cycle the focused Items dropdown forward (j / Down in filter input).
pub fn next_dropdown_value(app: &mut App) {
    cycle_dropdown(app, true);
}

/// Cycle the focused Items dropdown backward (k / Up in filter input).
pub fn prev_dropdown_value(app: &mut App) {
    cycle_dropdown(app, false);
}

fn cycle_dropdown(app: &mut App, forward: bool) {
    if !is_items_tab(app) {
        return;
    }
    let index_count = current_table(app).map(|t| t.index_count() + 1).unwrap_or(1);
    let focus = app.dynamodb_state.input_focus;
    if focus == ITEM_OPERATION {
        app.dynamodb_state.item_operation = if forward {
            app.dynamodb_state.item_operation.next()
        } else {
            app.dynamodb_state.item_operation.prev()
        };
    } else if focus == ITEM_INDEX {
        let current = app.dynamodb_state.item_index;
        app.dynamodb_state.item_index = if forward {
            (current + 1) % index_count
        } else {
            (current + index_count - 1) % index_count
        };
    } else if focus == ITEM_PROJECTION {
        app.dynamodb_state.item_projection = if forward {
            app.dynamodb_state.item_projection.next()
        } else {
            app.dynamodb_state.item_projection.prev()
        };
    } else {
        return;
    }
    reload_items(app);
}

/// Drop the current result set and schedule a fresh Scan or Query.
pub fn reload_items(app: &mut App) {
    app.dynamodb_state.clear_items_result();
    // A Query with no partition-key value yet has nothing to run.
    app.dynamodb_state.items_loading = app.dynamodb_state.item_operation == ItemOperation::Scan
        || !app.dynamodb_state.item_filter.is_empty();
}

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

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

pub fn yank(app: &App) {
    use crate::app::copy_to_clipboard;
    if app.dynamodb_state.current_table.is_some() {
        // Detail view — copy the ARN (fall back to the table name).
        if let Some(t) = current_table(app) {
            if t.table_arn.is_empty() {
                copy_to_clipboard(&t.name);
            } else {
                copy_to_clipboard(&t.table_arn);
            }
        }
    } else {
        let filtered = filtered_dynamodb_tables(app);
        if let Some(t) = app.dynamodb_state.tables.get_selected(&filtered) {
            copy_to_clipboard(&t.name);
        }
    }
}

pub fn breadcrumb(app: &App) -> Vec<String> {
    let mut parts = vec!["DynamoDB".to_string()];
    if let Some(name) = &app.dynamodb_state.current_table {
        parts.push(name.clone());
    } else {
        parts.push("Tables".to_string());
    }
    parts
}

pub fn console_url(app: &App) -> String {
    use crate::dynamodb;
    if let Some(name) = &app.dynamodb_state.current_table {
        return dynamodb::console_url_table(
            &app.config.region,
            name,
            app.dynamodb_state.detail_tab.console_tab(),
        );
    }
    let filtered = filtered_dynamodb_tables(app);
    if let Some(t) = app.dynamodb_state.tables.get_selected(&filtered) {
        dynamodb::console_url_table(
            &app.config.region,
            &t.name,
            DetailTab::Settings.console_tab(),
        )
    } else {
        dynamodb::console_url_tables(&app.config.region)
    }
}

// ── Select / go back / detail tabs ───────────────────────────────────────────

pub fn select_item(app: &mut App) {
    if app.dynamodb_state.current_table.is_some() {
        return;
    }
    let filtered = filtered_dynamodb_tables(app);
    let Some(name) = app
        .dynamodb_state
        .tables
        .get_selected(&filtered)
        .map(|t| t.name.clone())
    else {
        return;
    };
    app.dynamodb_state.current_table = Some(name);
    app.dynamodb_state.detail_tab = DetailTab::Items;
    reset_detail_tables(app);
    app.dynamodb_state.item_operation = ItemOperation::default();
    app.dynamodb_state.item_index = 0;
    app.dynamodb_state.item_projection = ItemProjection::default();
    app.dynamodb_state.item_filter.clear();
    // Attribute columns are table specific, so start with all of them visible.
    app.dynamodb_state.item_hidden_columns.clear();
    // Items is the landing tab, so schedule its scan straight away.
    reload_items(app);
}

pub fn go_back(app: &mut App) {
    app.dynamodb_state.current_table = None;
    app.dynamodb_state.detail_tab = DetailTab::Items;
}

pub fn next_detail_tab(app: &mut App) {
    app.dynamodb_state.detail_tab = app.dynamodb_state.detail_tab.next();
    reset_detail_tables(app);
    maybe_load_tab_data(app);
}

pub fn prev_detail_tab(app: &mut App) {
    app.dynamodb_state.detail_tab = app.dynamodb_state.detail_tab.prev();
    reset_detail_tables(app);
    maybe_load_tab_data(app);
}

fn reset_detail_tables(app: &mut App) {
    app.dynamodb_state.items.reset();
    app.dynamodb_state.indexes_table.reset();
    app.dynamodb_state.replicas_table.reset();
    app.dynamodb_state.backups.reset();
    app.dynamodb_state.tags_table.reset();
}

/// Mark on-demand data as needing a load when the owning tab becomes active.
fn maybe_load_tab_data(app: &mut App) {
    match app.dynamodb_state.detail_tab {
        // TTL shows on Settings and PITR on Backups — one call feeds both.
        DetailTab::Settings => {
            app.dynamodb_state.settings_loading = true;
        }
        DetailTab::Backups => {
            app.dynamodb_state.backups.items.clear();
            app.dynamodb_state.backups_loading = true;
            app.dynamodb_state.settings_loading = true;
        }
        DetailTab::AdditionalSettings => {
            app.dynamodb_state.table_policy = None;
            app.dynamodb_state.stream_policy = None;
            app.dynamodb_state.permissions_loading = true;
        }
        DetailTab::Items => {
            reload_items(app);
        }
        DetailTab::Tags => {
            app.dynamodb_state.tags_table.items.clear();
            app.dynamodb_state.tags_loading = true;
        }
        DetailTab::Monitor => {
            use crate::ui::monitoring::MonitoringState;
            app.dynamodb_state.clear_metrics();
            app.dynamodb_state.monitoring_scroll = 0;
            app.dynamodb_state.metrics_loading = true;
        }
        _ => {}
    }
}

// ── Monitor scrolling ─────────────────────────────────────────────────────────

/// Scroll the Monitor charts down (Ctrl+D / ScrollDown).
pub fn scroll_down_detail(app: &mut App) {
    use crate::ui::monitoring::MonitoringState;
    if app.dynamodb_state.detail_tab == DetailTab::Monitor {
        let max = crate::ui::dynamodb::MONITORING_CHARTS.saturating_sub(1);
        app.dynamodb_state
            .set_monitoring_scroll((app.dynamodb_state.monitoring_scroll() + 1).min(max));
    }
}

/// Scroll the Monitor charts up (Ctrl+U / ScrollUp).
pub fn scroll_up_detail(app: &mut App) {
    use crate::ui::monitoring::MonitoringState;
    if app.dynamodb_state.detail_tab == DetailTab::Monitor {
        app.dynamodb_state
            .set_monitoring_scroll(app.dynamodb_state.monitoring_scroll().saturating_sub(1));
    }
}

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

pub fn toggle_column(app: &mut App) {
    bindings::activate_for(app, Service::DynamoDbTables);
}

pub fn next_preferences(app: &mut App) {
    bindings::move_section_for(app, Service::DynamoDbTables, true);
}

pub fn prev_preferences(app: &mut App) {
    bindings::move_section_for(app, Service::DynamoDbTables, false);
}

pub fn column_selector_max(app: &App) -> usize {
    bindings::model_for(app, Service::DynamoDbTables).max_index()
}

pub fn column_count(app: &App) -> usize {
    bindings::model_for(app, Service::DynamoDbTables).column_count()
}