diskr 0.1.68

Lightweight terminal file explorer and disk/storage manager for macOS
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
use std::collections::HashSet;

use crate::app::Focus;

#[derive(Clone, Copy)]
pub struct FooterBinding {
    pub key: &'static str,
    pub action: &'static str,
}

#[derive(Clone, Copy)]
pub struct KeyBinding {
    pub key: &'static str,
    pub action: &'static str,
    pub footer: Option<FooterBinding>,
}

pub struct KeySection {
    pub title: &'static str,
    pub bindings: &'static [KeyBinding],
}

const GLOBAL: &[KeyBinding] = &[
    KeyBinding {
        key: "?",
        action: "Show keyboard help",
        footer: Some(FooterBinding {
            key: "?",
            action: "help",
        }),
    },
    KeyBinding {
        key: "q",
        action: "Quit",
        footer: Some(FooterBinding {
            key: "q",
            action: "quit",
        }),
    },
    KeyBinding {
        key: "Esc, Ctrl+C",
        action: "Close modal or clear search/filter",
        footer: None,
    },
];

const NAVIGATION: &[KeyBinding] = &[
    KeyBinding {
        key: "Up/Down, j/k",
        action: "Move selection",
        footer: Some(FooterBinding {
            key: "↑↓/jk",
            action: "move",
        }),
    },
    KeyBinding {
        key: "PageUp/PageDown",
        action: "Move one page",
        footer: None,
    },
    KeyBinding {
        key: "Home/End",
        action: "Jump first/last",
        footer: None,
    },
    KeyBinding {
        key: "Enter",
        action: "Open selected item",
        footer: Some(FooterBinding {
            key: "",
            action: "open",
        }),
    },
    KeyBinding {
        key: "Backspace",
        action: "Go to parent directory",
        footer: Some(FooterBinding {
            key: "",
            action: "up",
        }),
    },
    KeyBinding {
        key: "Left/Right, h/l",
        action: "Switch pane or package view",
        footer: Some(FooterBinding {
            key: "←→/hl",
            action: "pane/view",
        }),
    },
    KeyBinding {
        key: "Tab, Shift+Tab",
        action: "Cycle panes",
        footer: Some(FooterBinding {
            key: "Tab",
            action: "pane",
        }),
    },
];

const FILES: &[KeyBinding] = &[
    KeyBinding {
        key: "/",
        action: "Search files or filter packages; Enter keeps, Esc clears",
        footer: Some(FooterBinding {
            key: "/",
            action: "search",
        }),
    },
    KeyBinding {
        key: "i",
        action: "Show details",
        footer: Some(FooterBinding {
            key: "i",
            action: "info",
        }),
    },
    KeyBinding {
        key: ".",
        action: "Toggle hidden files",
        footer: None,
    },
    KeyBinding {
        key: "o",
        action: "Cycle sort mode",
        footer: None,
    },
    KeyBinding {
        key: "r",
        action: "Refresh view and rescan visible dirs",
        footer: Some(FooterBinding {
            key: "r",
            action: "refresh",
        }),
    },
    KeyBinding {
        key: "S",
        action: "Scan missing or stale visible directory sizes",
        footer: Some(FooterBinding {
            key: "S",
            action: "scan all",
        }),
    },
    KeyBinding {
        key: "B",
        action: "Save history baseline",
        footer: None,
    },
    KeyBinding {
        key: "t",
        action: "Open top-files list",
        footer: None,
    },
    KeyBinding {
        key: "c",
        action: "Rename selected file",
        footer: None,
    },
    KeyBinding {
        key: "n",
        action: "Create directory",
        footer: None,
    },
    KeyBinding {
        key: "v",
        action: "Toggle mark",
        footer: None,
    },
    KeyBinding {
        key: "a",
        action: "Mark all visible files",
        footer: None,
    },
    KeyBinding {
        key: "d",
        action: "Move selected item (or marks) to Trash",
        footer: Some(FooterBinding {
            key: "d",
            action: "trash",
        }),
    },
];

const EXTERNAL_ACTIONS: &[KeyBinding] = &[
    KeyBinding {
        key: "Space",
        action: "Quick Look selected item",
        footer: Some(FooterBinding {
            key: "Space",
            action: "preview",
        }),
    },
    KeyBinding {
        key: "f",
        action: "Reveal selected item in Finder",
        footer: None,
    },
    KeyBinding {
        key: "O",
        action: "Open selected item with default app",
        footer: None,
    },
    KeyBinding {
        key: "y",
        action: "Copy selected path",
        footer: None,
    },
    KeyBinding {
        key: "s",
        action: "Open selected location in Terminal",
        footer: None,
    },
];

const PACKAGES: &[KeyBinding] = &[
    KeyBinding {
        key: "p",
        action: "Open packages pane / switch view",
        footer: Some(FooterBinding {
            key: "p",
            action: "packages",
        }),
    },
    KeyBinding {
        key: "u",
        action: "Toggle dependency-leaf filter",
        footer: Some(FooterBinding {
            key: "u",
            action: "leaves",
        }),
    },
    KeyBinding {
        key: "x",
        action: "Uninstall selected package",
        footer: Some(FooterBinding {
            key: "x",
            action: "uninstall",
        }),
    },
];

const RECLAIM_REPORTS: &[KeyBinding] = &[
    KeyBinding {
        key: "R",
        action: "Re-scan reclaim pane",
        footer: Some(FooterBinding {
            key: "R",
            action: "rescan",
        }),
    },
    KeyBinding {
        key: "E",
        action: "Empty Trash (when the report lists Trash)",
        footer: Some(FooterBinding {
            key: "E",
            action: "empty trash",
        }),
    },
    KeyBinding {
        key: "d",
        action: "Trash selected reclaim/top-files path",
        footer: Some(FooterBinding {
            key: "d",
            action: "trash",
        }),
    },
];

pub const HELP_SECTIONS: &[KeySection] = &[
    KeySection {
        title: "Global",
        bindings: GLOBAL,
    },
    KeySection {
        title: "Navigation",
        bindings: NAVIGATION,
    },
    KeySection {
        title: "Files",
        bindings: FILES,
    },
    KeySection {
        title: "Actions",
        bindings: EXTERNAL_ACTIONS,
    },
    KeySection {
        title: "Packages",
        bindings: PACKAGES,
    },
    KeySection {
        title: "Reclaim / Reports",
        bindings: RECLAIM_REPORTS,
    },
];

/// Destructive keys are highlighted differently in the help overlay so they
/// stand out from navigation. Kept here (rather than a struct field) so the
/// single source of truth stays the key table.
pub fn is_destructive_key(key: &str) -> bool {
    matches!(key, "d" | "E" | "x")
}

/// Footer hints relevant to the focused pane, most-relevant first and
/// de-duplicated. The caller renders as many as fit and always advertises `?`,
/// so the strip stays short and on-topic instead of listing every binding.
pub fn footer_bindings_for(focus: Focus) -> Vec<FooterBinding> {
    let sections: &[&[KeyBinding]] = match focus {
        Focus::Files => &[NAVIGATION, FILES, EXTERNAL_ACTIONS, PACKAGES],
        Focus::Disks => &[NAVIGATION, EXTERNAL_ACTIONS, PACKAGES],
        Focus::Packages => &[NAVIGATION, PACKAGES, EXTERNAL_ACTIONS],
        Focus::Reclaim => &[NAVIGATION, RECLAIM_REPORTS, EXTERNAL_ACTIONS],
    };
    let mut seen = HashSet::new();
    let mut out: Vec<FooterBinding> = Vec::new();
    for binding in sections.iter().copied().flatten() {
        if let Some(footer) = binding.footer {
            if seen.insert(footer.key) {
                out.push(footer);
            }
        }
    }
    // `q` quit always rides along after the pane-specific hints; `?` is added
    // by the renderer so it survives width truncation.
    for binding in GLOBAL {
        if let Some(footer) = binding.footer {
            if footer.key != "?" && seen.insert(footer.key) {
                out.push(footer);
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn keymap_includes_help_binding() {
        assert!(HELP_SECTIONS
            .iter()
            .flat_map(|section| section.bindings.iter())
            .any(|binding| binding.key == "?"));
    }

    #[test]
    fn help_binding_has_a_footer_hint() {
        // The footer renderer always appends `? help`; the `?` binding must
        // carry the footer text it uses.
        let help = GLOBAL.iter().find(|binding| binding.key == "?").unwrap();
        assert_eq!(help.footer.map(|footer| footer.action), Some("help"));
    }

    #[test]
    fn focus_footer_is_focused_and_short() {
        // Files footer carries file actions, not package/reclaim noise.
        let files: Vec<&str> = footer_bindings_for(Focus::Files)
            .iter()
            .map(|fb| fb.key)
            .collect();
        assert!(files.contains(&"/"));
        assert!(files.contains(&"S"));
        assert!(
            !files.contains(&"R"),
            "reclaim key leaked into Files footer"
        );

        // Reclaim footer carries reclaim actions.
        let reclaim: Vec<&str> = footer_bindings_for(Focus::Reclaim)
            .iter()
            .map(|fb| fb.key)
            .collect();
        assert!(reclaim.contains(&"R"));
        assert!(reclaim.contains(&"E"));

        // Packages footer carries package actions.
        let packages: Vec<&str> = footer_bindings_for(Focus::Packages)
            .iter()
            .map(|fb| fb.key)
            .collect();
        assert!(packages.contains(&"p"));
        assert!(packages.contains(&"u"));

        // No focus produces a duplicate key, and `?` is left to the renderer.
        for focus in [Focus::Files, Focus::Disks, Focus::Packages, Focus::Reclaim] {
            let keys: Vec<&str> = footer_bindings_for(focus).iter().map(|fb| fb.key).collect();
            let mut deduped = keys.clone();
            deduped.sort_unstable();
            deduped.dedup();
            assert_eq!(
                keys.len(),
                deduped.len(),
                "duplicate footer key for {focus:?}"
            );
            assert!(!keys.contains(&"?"));
        }
    }

    #[test]
    fn destructive_keys_are_flagged() {
        assert!(is_destructive_key("d"));
        assert!(is_destructive_key("E"));
        assert!(is_destructive_key("x"));
        assert!(!is_destructive_key("r"));
        assert!(!is_destructive_key("S"));
    }

    #[test]
    fn readme_documents_every_keymap_key() {
        let readme = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"));
        for binding in HELP_SECTIONS
            .iter()
            .flat_map(|section| section.bindings.iter())
        {
            assert!(
                readme.contains(binding.key),
                "README key table is missing keymap binding `{}`",
                binding.key
            );
        }
    }

    #[test]
    fn readme_runtime_claims_stay_truthful() {
        let readme = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"));
        assert!(
            !readme.contains("no serde"),
            "README still claims 'no serde'"
        );
        assert!(
            readme.contains("serde_json"),
            "README should name the serde_json dependency"
        );
    }
}