pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
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
use crossterm::event::KeyCode;
use ratatui::style::Color;

use super::types::KeyChord;
use crossterm::event::KeyModifiers;

/// What: Parse a single key identifier (e.g., "F5", "Esc", "?", "r") into a [`KeyCode`].
///
/// Inputs:
/// - `s`: Raw key token from a configuration string.
///
/// Output:
/// - `Some(KeyCode)` on success; `None` when the input token is unsupported.
///
/// Details:
/// - Supports function keys, navigation keys, and single printable characters.
/// - Normalizes character keys to lowercase for consistent matching.
pub(super) fn parse_key_identifier(s: &str) -> Option<KeyCode> {
    let t = s.trim();
    // Function keys
    if let Some(num) = t.strip_prefix('F').and_then(|x| x.parse::<u8>().ok()) {
        return Some(KeyCode::F(num));
    }
    match t.to_ascii_uppercase().as_str() {
        "ESC" => Some(KeyCode::Esc),
        "ENTER" | "RETURN" => Some(KeyCode::Enter),
        "TAB" => Some(KeyCode::Tab),
        "BACKTAB" | "SHIFT+TAB" => Some(KeyCode::BackTab),
        "BACKSPACE" => Some(KeyCode::Backspace),
        "DELETE" | "DEL" => Some(KeyCode::Delete),
        "INSERT" | "INS" => Some(KeyCode::Insert),
        "HOME" => Some(KeyCode::Home),
        "END" => Some(KeyCode::End),
        "PAGEUP" | "PGUP" => Some(KeyCode::PageUp),
        "PAGEDOWN" | "PGDN" => Some(KeyCode::PageDown),
        "UP" | "ARROWUP" => Some(KeyCode::Up),
        "DOWN" | "ARROWDOWN" => Some(KeyCode::Down),
        "LEFT" | "ARROWLEFT" => Some(KeyCode::Left),
        "RIGHT" | "ARROWRIGHT" => Some(KeyCode::Right),
        "SPACE" => Some(KeyCode::Char(' ')),
        _ => {
            // Single visible character, e.g. "?" or "r"; normalize to lowercase
            let mut chars = t.chars();
            if let (Some(ch), None) = (chars.next(), chars.next()) {
                Some(KeyCode::Char(ch.to_ascii_lowercase()))
            } else {
                None
            }
        }
    }
}

/// What: Parse a full key chord such as "Ctrl+R" or "Shift+Tab" into a [`KeyChord`].
///
/// Inputs:
/// - `spec`: String specification combining optional modifiers with a key token.
///
/// Output:
/// - `Some(KeyChord)` when parsing succeeds; `None` on invalid modifier/key combinations.
///
/// Details:
/// - Recognizes Ctrl/Alt/Shift/Super modifiers in any case.
/// - Normalizes `Shift+Tab` to the dedicated `BackTab` key code and clears modifiers.
pub(super) fn parse_key_chord(spec: &str) -> Option<KeyChord> {
    // Accept formats like: CTRL+R, Alt+?, Shift+Del, F1, Tab, BackTab, Super+F2
    let mut mods = KeyModifiers::empty();
    let mut key_part: Option<String> = None;
    for part in spec.split('+') {
        let p = part.trim();
        if p.is_empty() {
            continue;
        }
        match p.to_ascii_uppercase().as_str() {
            "CTRL" | "CONTROL" => mods |= KeyModifiers::CONTROL,
            "ALT" => mods |= KeyModifiers::ALT,
            "SHIFT" => mods |= KeyModifiers::SHIFT,
            "SUPER" | "META" | "WIN" => mods |= KeyModifiers::SUPER,
            other => {
                key_part = Some(other.to_string());
            }
        }
    }
    // Special-case Shift+Tab -> BackTab (mods cleared)
    if key_part.as_deref() == Some("TAB") && mods.contains(KeyModifiers::SHIFT) {
        return Some(KeyChord {
            code: KeyCode::BackTab,
            mods: KeyModifiers::empty(),
        });
    }
    let code = parse_key_identifier(key_part.as_deref().unwrap_or(""))?;
    Some(KeyChord { code, mods })
}

/// What: Parse a color literal from configuration text into a [`Color`].
///
/// Inputs:
/// - `s`: Color specification string potentially containing inline comments.
///
/// Output:
/// - `Some(Color)` for recognized hex or decimal triplet formats; `None` otherwise.
///
/// Details:
/// - Strips trailing comments beginning with `//` or secondary `#` markers.
/// - Accepts `#RRGGBB` hex and `R,G,B` decimal triplets (0-255 per channel).
#[allow(clippy::many_single_char_names)]
pub(super) fn parse_color_value(s: &str) -> Option<Color> {
    // Trim and strip inline comments (support trailing "// ..." and "# ...").
    // Preserve a leading '#' for hex values by searching for '#' only after position 1.
    let mut t = s.trim();
    // Strip // comments first
    if let Some(i) = t.find("//") {
        t = &t[..i];
    }
    // Strip # comments, but preserve leading # for hex colors
    // Look for # after position 1 (to preserve #RRGGBB format)
    if let Some(i) = t.char_indices().skip(1).find(|(_, ch)| *ch == '#') {
        t = &t[..i.0];
    }
    t = t.trim();
    if t.is_empty() {
        return None;
    }
    // Hex formats: #RRGGBB or RRGGBB
    let h = t.strip_prefix('#').unwrap_or(t);
    if h.len() == 6 && h.chars().all(|c| c.is_ascii_hexdigit()) {
        let r = u8::from_str_radix(&h[0..2], 16).ok()?;
        let g = u8::from_str_radix(&h[2..4], 16).ok()?;
        let b = u8::from_str_radix(&h[4..6], 16).ok()?;
        return Some(Color::Rgb(r, g, b));
    }
    // Decimal triplet: R,G,B
    if let Some((r, g, b)) = t.split(',').collect::<Vec<_>>().get(0..3).and_then(|v| {
        let r = v[0].trim().parse::<u16>().ok()?;
        let g = v[1].trim().parse::<u16>().ok()?;
        let b = v[2].trim().parse::<u16>().ok()?;
        Some((r, g, b))
    }) && r <= 255
        && g <= 255
        && b <= 255
    {
        return Some(Color::Rgb(
            u8::try_from(r).unwrap_or(255),
            u8::try_from(g).unwrap_or(255),
            u8::try_from(b).unwrap_or(255),
        ));
    }
    None
}

/// What: Map a normalized theme key (lowercase, underscores) to Pacsea's canonical key.
///
/// Inputs:
/// - `norm`: Normalized key string pulled from user configuration.
///
/// Output:
/// - `Some(&'static str)` containing the canonical key when recognized; `None` otherwise.
///
/// Details:
/// - Handles legacy and alternative naming schemes to preserve backwards compatibility.
pub(super) fn canonical_for_key(norm: &str) -> Option<&'static str> {
    match norm {
        // Legacy and comprehensive keys mapped to canonical names
        "base" | "background" | "background_base" => Some("base"),
        "mantle" | "background_mantle" => Some("mantle"),
        "crust" | "background_crust" => Some("crust"),
        "surface1" | "surface_1" | "surface_level1" => Some("surface1"),
        "surface2" | "surface_2" | "surface_level2" => Some("surface2"),
        "overlay1" | "overlay_primary" | "border_primary" => Some("overlay1"),
        "overlay2" | "overlay_secondary" | "border_secondary" => Some("overlay2"),
        "text" | "text_primary" => Some("text"),
        "subtext0" | "text_secondary" => Some("subtext0"),
        "subtext1" | "text_tertiary" => Some("subtext1"),
        "sapphire" | "accent_interactive" | "accent_info" => Some("sapphire"),
        "mauve" | "accent_heading" | "accent_primary" => Some("mauve"),
        "green" | "semantic_success" => Some("green"),
        "yellow" | "semantic_warning" => Some("yellow"),
        "red" | "semantic_error" => Some("red"),
        "lavender" | "accent_emphasis" | "accent_border" => Some("lavender"),
        _ => None,
    }
}

/// What: Convert a canonical theme key into the preferred, user-facing identifier.
///
/// Inputs:
/// - `canon`: Canonical key such as `"overlay1"`.
///
/// Output:
/// - `String` containing the display-friendly key for messaging.
///
/// Details:
/// - Favors descriptive names (e.g., `overlay_primary`) when available.
pub(super) fn canonical_to_preferred(canon: &str) -> String {
    match canon {
        "base" => "background_base",
        "mantle" => "background_mantle",
        "crust" => "background_crust",
        "surface1" => "surface_level1",
        "surface2" => "surface_level2",
        "overlay1" => "overlay_primary",
        "overlay2" => "overlay_secondary",
        "text" => "text_primary",
        "subtext0" => "text_secondary",
        "subtext1" => "text_tertiary",
        "sapphire" => "accent_interactive",
        "mauve" => "accent_heading",
        "green" => "semantic_success",
        "yellow" => "semantic_warning",
        "red" => "semantic_error",
        "lavender" => "accent_emphasis",
        _ => canon,
    }
    .to_string()
}

/// What: Apply a single `key=value` override to the theme color map with validation.
///
/// Inputs:
/// - `map`: Accumulated theme colors being constructed.
/// - `key`: Raw key string from the configuration file.
/// - `value`: Raw value string associated with the key.
/// - `errors`: Mutable buffer that collects diagnostic messages.
/// - `line_no`: 1-based line number used for contextual messages.
///
/// Output:
/// - None (mutates `map` and `errors` in place).
///
/// Details:
/// - Normalizes keys, suggests close matches, and validates color formats before inserting.
pub(super) fn apply_override_to_map(
    map: &mut std::collections::HashMap<String, Color>,
    key: &str,
    value: &str,
    errors: &mut Vec<String>,
    line_no: usize,
) {
    let norm = key.trim().to_lowercase().replace(['.', '-', ' '], "_");
    let Some(canon) = canonical_for_key(&norm) else {
        let suggestion = nearest_key(&norm);
        if let Some(s) = suggestion {
            errors.push(format!(
                "- Unknown key '{}' on line {} (did you mean '{}'?)",
                key,
                line_no,
                canonical_to_preferred(s)
            ));
        } else {
            errors.push(format!("- Unknown key '{key}' on line {line_no}"));
        }
        return;
    };
    if value.is_empty() {
        errors.push(format!("- Missing value for '{key}' on line {line_no}"));
        return;
    }
    parse_color_value(value).map_or_else(
        || {
            errors.push(format!(
                "- Invalid color for '{key}' on line {line_no} (use #RRGGBB or R,G,B)"
            ));
        },
        |c| {
            map.insert(canon.to_string(), c);
        },
    );
}

/// What: Suggest the canonical key closest to a potentially misspelled input.
///
/// Inputs:
/// - `input`: User-provided key string.
///
/// Output:
/// - `Some(&'static str)` when the best match is within edit distance 3; `None` otherwise.
///
/// Details:
/// - Computes Levenshtein distance across the small known key set for quick suggestion hints.
pub(super) fn nearest_key(input: &str) -> Option<&'static str> {
    // Very small domain; simple Levenshtein distance is fine
    const CANON: [&str; 16] = [
        "base", "mantle", "crust", "surface1", "surface2", "overlay1", "overlay2", "text",
        "subtext0", "subtext1", "sapphire", "mauve", "green", "yellow", "red", "lavender",
    ];
    let mut best: Option<(&'static str, usize)> = None;
    for &k in &CANON {
        let d = levenshtein(input, k);
        if best.is_none_or(|(_, bd)| d < bd) {
            best = Some((k, d));
        }
    }
    best.and_then(|(k, d)| if d <= 3 { Some(k) } else { None })
}

/// What: Compute the Levenshtein edit distance between two strings.
///
/// Inputs:
/// - `a`: First string.
/// - `b`: Second string.
///
/// Output:
/// - `usize` representing the minimum number of single-character edits required to transform `a` into `b`.
///
/// Details:
/// - Uses a rolling dynamic programming table to reduce allocations while iterating.
pub(super) fn levenshtein(a: &str, b: &str) -> usize {
    let m = b.len();
    let mut dp: Vec<usize> = (0..=m).collect();
    for (i, ca) in a.chars().enumerate() {
        let mut prev = dp[0];
        dp[0] = i + 1;
        for (j, cb) in b.chars().enumerate() {
            let tmp = dp[j + 1];
            let cost = usize::from(ca != cb);
            dp[j + 1] = std::cmp::min(std::cmp::min(dp[j + 1] + 1, dp[j] + 1), prev + cost);
            prev = tmp;
        }
    }
    dp[m]
}

/// What: Remove inline comments from a configuration line while preserving leading hex markers.
///
/// Inputs:
/// - `s`: Raw configuration line that may include inline comments.
///
/// Output:
/// - Comment-free & trimmed substring of the input.
///
/// Details:
/// - Strips trailing `//` sections and secondary `#` characters without harming leading `#RRGGBB` values.
pub(super) fn strip_inline_comment(mut s: &str) -> &str {
    // Strip // comments first, but preserve // in URLs (http://, https://)
    // Find all occurrences of // and check each one
    let mut comment_start = None;
    for (i, _) in s.match_indices("//") {
        // Check if this // is part of a URL scheme (preceded by :)
        if i > 0 && s.as_bytes().get(i - 1) == Some(&b':') {
            // This is part of a URL scheme (http:// or https://), skip it
            continue;
        }
        // This // is likely a comment delimiter
        comment_start = Some(i);
        break;
    }
    if let Some(i) = comment_start {
        s = &s[..i];
    }
    // Strip # comments, but preserve leading # for hex colors
    // Look for # after position 1 (to preserve #RRGGBB format)
    if let Some(i) = s.char_indices().skip(1).find(|(_, ch)| *ch == '#') {
        s = &s[..i.0];
    }
    s.trim()
}

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

    #[test]
    /// What: Ensure key identifier and chord parsing maps strings onto `KeyCode`/modifier combinations.
    ///
    /// Inputs:
    /// - Identifiers such as `F5`, `?`, and `Backspace`, plus chord strings `Ctrl+R` and `Shift+Tab`.
    ///
    /// Output:
    /// - Returns matching `KeyCode` values with expected modifier flags.
    ///
    /// Details:
    /// - Guards against regressions when adding new key parsing rules.
    fn parsing_key_identifier_and_chord() {
        assert_eq!(parse_key_identifier("F5"), Some(KeyCode::F(5)));
        assert_eq!(parse_key_identifier("?"), Some(KeyCode::Char('?')));
        assert_eq!(parse_key_identifier("Backspace"), Some(KeyCode::Backspace));
        let kc = parse_key_chord("Ctrl+R").expect("Ctrl+R should be a valid key chord");
        assert_eq!(kc.code, KeyCode::Char('r'));
        assert!(kc.mods.contains(KeyModifiers::CONTROL));
        let bt = parse_key_chord("Shift+Tab").expect("Shift+Tab should be a valid key chord");
        assert_eq!(bt.code, KeyCode::BackTab);
        assert!(bt.mods.is_empty());
        let sr = parse_key_chord("Shift+R").expect("Shift+R should be a valid key chord");
        assert_eq!(sr.code, KeyCode::Char('r'));
        assert!(sr.mods.contains(KeyModifiers::SHIFT));
        assert!(!sr.mods.contains(KeyModifiers::CONTROL));
    }

    #[test]
    /// What: Validate colour parsing and mapping helpers used by theme configuration.
    ///
    /// Inputs:
    /// - Hex and RGB strings plus canonical key identifiers.
    ///
    /// Output:
    /// - Produces `Color::Rgb` values when parsable and resolves preferred canonical keys.
    ///
    /// Details:
    /// - Exercises both parsing paths and fuzzy key lookup to preserve user overrides.
    fn parsing_color_and_canon() {
        assert_eq!(parse_color_value("#ff0000"), Some(Color::Rgb(255, 0, 0)));
        assert_eq!(parse_color_value("255,0,10"), Some(Color::Rgb(255, 0, 10)));
        assert!(parse_color_value("").is_none());
        assert_eq!(canonical_for_key("background_base"), Some("base"));
        assert_eq!(canonical_to_preferred("overlay1"), "overlay_primary");
        assert!(nearest_key("bas").is_some());
    }

    #[test]
    /// What: Check inline comment stripping keeps colour literals while removing trailing annotations.
    ///
    /// Inputs:
    /// - Strings containing hex colours, `//` comments, and secondary `#` markers.
    ///
    /// Output:
    /// - Returns trimmed strings with comments removed but leading hex markers preserved.
    ///
    /// Details:
    /// - Confirms heuristics avoid truncating six-digit colour codes while cleaning inline comments.
    fn parsing_strip_inline_comment_variants() {
        // Leading '#' preserved for hex; we only strip after first character to allow '#RRGGBB'
        assert_eq!(strip_inline_comment("#foo"), "#foo");
        assert_eq!(strip_inline_comment("abc // hi"), "abc");
        assert_eq!(strip_inline_comment("#ff00ff # tail"), "#ff00ff");
        // URLs with // should be preserved
        assert_eq!(
            strip_inline_comment("https://example.com/path"),
            "https://example.com/path"
        );
        assert_eq!(
            strip_inline_comment("https://gist.github.com/user/id/raw/file.json"),
            "https://gist.github.com/user/id/raw/file.json"
        );
        // URL followed by comment should preserve URL
        assert_eq!(
            strip_inline_comment("https://example.com // comment"),
            "https://example.com"
        );
    }
}