feedr 0.7.0

Feedr is a feature-rich terminal-based RSS/Atom feed reader written in Rust.
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::collections::HashMap;
use std::str::FromStr;

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub enum KeyAction {
    // Global
    Quit,
    ForceQuit,
    Back,
    Home,
    ToggleTheme,
    Refresh,
    Help,
    OpenSearch,
    // Navigation
    MoveUp,
    MoveDown,
    PageUp,
    PageDown,
    JumpTop,
    JumpBottom,
    Select,
    // Item actions
    AddFeed,
    DeleteFeed,
    ToggleRead,
    ToggleStar,
    MarkAllRead,
    OpenInBrowser,
    TogglePreview,
    // Filter/Category
    OpenFilter,
    CycleCategory,
    OpenCategoryManagement,
    AssignCategory,
    // Detail
    ExtractLinks,
    ScrollPreviewUp,
    ScrollPreviewDown,
    // Tree
    ToggleExpand,
    // Tab
    NextTab,
    PrevTab,
}

impl FromStr for KeyAction {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "quit" => Ok(Self::Quit),
            "force_quit" => Ok(Self::ForceQuit),
            "back" => Ok(Self::Back),
            "home" => Ok(Self::Home),
            "toggle_theme" => Ok(Self::ToggleTheme),
            "refresh" => Ok(Self::Refresh),
            "help" => Ok(Self::Help),
            "open_search" => Ok(Self::OpenSearch),
            "move_up" => Ok(Self::MoveUp),
            "move_down" => Ok(Self::MoveDown),
            "page_up" => Ok(Self::PageUp),
            "page_down" => Ok(Self::PageDown),
            "jump_top" => Ok(Self::JumpTop),
            "jump_bottom" => Ok(Self::JumpBottom),
            "select" => Ok(Self::Select),
            "add_feed" => Ok(Self::AddFeed),
            "delete_feed" => Ok(Self::DeleteFeed),
            "toggle_read" => Ok(Self::ToggleRead),
            "toggle_star" => Ok(Self::ToggleStar),
            "mark_all_read" => Ok(Self::MarkAllRead),
            "open_in_browser" => Ok(Self::OpenInBrowser),
            "toggle_preview" => Ok(Self::TogglePreview),
            "open_filter" => Ok(Self::OpenFilter),
            "cycle_category" => Ok(Self::CycleCategory),
            "open_category_management" => Ok(Self::OpenCategoryManagement),
            "assign_category" => Ok(Self::AssignCategory),
            "extract_links" => Ok(Self::ExtractLinks),
            "scroll_preview_up" => Ok(Self::ScrollPreviewUp),
            "scroll_preview_down" => Ok(Self::ScrollPreviewDown),
            "toggle_expand" => Ok(Self::ToggleExpand),
            "next_tab" => Ok(Self::NextTab),
            "prev_tab" => Ok(Self::PrevTab),
            _ => Err(()),
        }
    }
}

#[derive(Clone, Debug)]
pub struct KeyBinding {
    pub code: KeyCode,
    pub modifiers: KeyModifiers,
}

impl KeyBinding {
    pub fn new(code: KeyCode) -> Self {
        Self {
            code,
            modifiers: KeyModifiers::NONE,
        }
    }

    pub fn with_ctrl(code: KeyCode) -> Self {
        Self {
            code,
            modifiers: KeyModifiers::CONTROL,
        }
    }

    pub fn with_shift(code: KeyCode) -> Self {
        Self {
            code,
            modifiers: KeyModifiers::SHIFT,
        }
    }

    pub fn matches(&self, key: &KeyEvent) -> bool {
        self.code == key.code && key.modifiers.contains(self.modifiers)
    }
}

pub type KeyBindingMap = HashMap<KeyAction, Vec<KeyBinding>>;

pub fn default_keybindings() -> KeyBindingMap {
    let mut map = KeyBindingMap::new();

    // Global
    map.insert(KeyAction::Quit, vec![KeyBinding::new(KeyCode::Char('q'))]);
    map.insert(
        KeyAction::ForceQuit,
        vec![KeyBinding::with_ctrl(KeyCode::Char('q'))],
    );
    map.insert(
        KeyAction::Back,
        vec![
            KeyBinding::new(KeyCode::Char('h')),
            KeyBinding::new(KeyCode::Esc),
            KeyBinding::new(KeyCode::Backspace),
        ],
    );
    map.insert(KeyAction::Home, vec![KeyBinding::new(KeyCode::Home)]);
    map.insert(
        KeyAction::ToggleTheme,
        vec![KeyBinding::new(KeyCode::Char('t'))],
    );
    map.insert(
        KeyAction::Refresh,
        vec![KeyBinding::new(KeyCode::Char('r'))],
    );
    map.insert(KeyAction::Help, vec![KeyBinding::new(KeyCode::Char('?'))]);
    map.insert(
        KeyAction::OpenSearch,
        vec![KeyBinding::new(KeyCode::Char('/'))],
    );

    // Navigation
    map.insert(
        KeyAction::MoveUp,
        vec![
            KeyBinding::new(KeyCode::Up),
            KeyBinding::new(KeyCode::Char('k')),
        ],
    );
    map.insert(
        KeyAction::MoveDown,
        vec![
            KeyBinding::new(KeyCode::Down),
            KeyBinding::new(KeyCode::Char('j')),
        ],
    );
    map.insert(
        KeyAction::PageUp,
        vec![
            KeyBinding::new(KeyCode::PageUp),
            KeyBinding::with_ctrl(KeyCode::Char('u')),
        ],
    );
    map.insert(
        KeyAction::PageDown,
        vec![
            KeyBinding::new(KeyCode::PageDown),
            KeyBinding::with_ctrl(KeyCode::Char('d')),
        ],
    );
    map.insert(
        KeyAction::JumpTop,
        vec![KeyBinding::new(KeyCode::Char('g'))],
    );
    map.insert(
        KeyAction::JumpBottom,
        vec![
            KeyBinding::new(KeyCode::Char('G')),
            KeyBinding::new(KeyCode::End),
        ],
    );
    map.insert(KeyAction::Select, vec![KeyBinding::new(KeyCode::Enter)]);

    // Item actions
    map.insert(
        KeyAction::AddFeed,
        vec![KeyBinding::new(KeyCode::Char('a'))],
    );
    map.insert(
        KeyAction::DeleteFeed,
        vec![KeyBinding::new(KeyCode::Char('d'))],
    );
    map.insert(
        KeyAction::ToggleRead,
        vec![KeyBinding::new(KeyCode::Char(' '))],
    );
    map.insert(
        KeyAction::ToggleStar,
        vec![KeyBinding::new(KeyCode::Char('s'))],
    );
    map.insert(
        KeyAction::MarkAllRead,
        vec![KeyBinding::new(KeyCode::Char('m'))],
    );
    map.insert(
        KeyAction::OpenInBrowser,
        vec![KeyBinding::new(KeyCode::Char('o'))],
    );
    map.insert(
        KeyAction::TogglePreview,
        vec![KeyBinding::new(KeyCode::Char('p'))],
    );

    // Filter/Category
    map.insert(
        KeyAction::OpenFilter,
        vec![KeyBinding::new(KeyCode::Char('f'))],
    );
    map.insert(
        KeyAction::CycleCategory,
        vec![KeyBinding::new(KeyCode::Char('c'))],
    );
    map.insert(
        KeyAction::OpenCategoryManagement,
        vec![KeyBinding::with_ctrl(KeyCode::Char('c'))],
    );
    map.insert(
        KeyAction::AssignCategory,
        vec![KeyBinding::new(KeyCode::Char('c'))],
    );

    // Detail
    map.insert(
        KeyAction::ExtractLinks,
        vec![KeyBinding::new(KeyCode::Char('l'))],
    );
    map.insert(
        KeyAction::ScrollPreviewUp,
        vec![
            KeyBinding::with_shift(KeyCode::Char('K')),
            KeyBinding::with_shift(KeyCode::Up),
        ],
    );
    map.insert(
        KeyAction::ScrollPreviewDown,
        vec![
            KeyBinding::with_shift(KeyCode::Char('J')),
            KeyBinding::with_shift(KeyCode::Down),
        ],
    );

    // Tree
    map.insert(
        KeyAction::ToggleExpand,
        vec![KeyBinding::new(KeyCode::Char(' '))],
    );

    // Tab
    map.insert(KeyAction::NextTab, vec![KeyBinding::new(KeyCode::Tab)]);
    map.insert(
        KeyAction::PrevTab,
        vec![KeyBinding::with_shift(KeyCode::Tab)],
    );

    map
}

/// Parse a key string like "q", "Ctrl+q", "Enter", "Space", "?", "F5", "Shift+Tab"
pub fn parse_key_string(s: &str) -> Option<KeyBinding> {
    let parts: Vec<&str> = s.split('+').collect();
    let mut modifiers = KeyModifiers::NONE;
    let key_part;

    if parts.len() == 1 {
        key_part = parts[0].trim();
    } else if parts.len() == 2 {
        let modifier = parts[0].trim().to_lowercase();
        key_part = parts[1].trim();
        match modifier.as_str() {
            "ctrl" | "control" => modifiers |= KeyModifiers::CONTROL,
            "shift" => modifiers |= KeyModifiers::SHIFT,
            "alt" => modifiers |= KeyModifiers::ALT,
            _ => return None,
        }
    } else {
        return None;
    }

    let code = match key_part.to_lowercase().as_str() {
        "enter" | "return" => KeyCode::Enter,
        "esc" | "escape" => KeyCode::Esc,
        "space" => KeyCode::Char(' '),
        "tab" => KeyCode::Tab,
        "backspace" | "bs" => KeyCode::Backspace,
        "up" => KeyCode::Up,
        "down" => KeyCode::Down,
        "left" => KeyCode::Left,
        "right" => KeyCode::Right,
        "home" => KeyCode::Home,
        "end" => KeyCode::End,
        "pageup" | "pgup" => KeyCode::PageUp,
        "pagedown" | "pgdn" => KeyCode::PageDown,
        "delete" | "del" => KeyCode::Delete,
        "f1" => KeyCode::F(1),
        "f2" => KeyCode::F(2),
        "f3" => KeyCode::F(3),
        "f4" => KeyCode::F(4),
        "f5" => KeyCode::F(5),
        s if s.len() == 1 => {
            let c = s.chars().next().unwrap();
            if modifiers.contains(KeyModifiers::SHIFT) && c.is_ascii_alphabetic() {
                KeyCode::Char(c.to_ascii_uppercase())
            } else {
                KeyCode::Char(c)
            }
        }
        _ => return None,
    };

    Some(KeyBinding { code, modifiers })
}

/// Build keybinding map by merging defaults with config overrides.
/// Config format in TOML: [keybindings] section with action_name = "key" or action_name = ["key1", "key2"]
/// Returns the map and a list of warnings for invalid config entries.
pub fn build_keybindings(
    config_keybindings: &HashMap<String, toml::Value>,
) -> (KeyBindingMap, Vec<String>) {
    let mut map = default_keybindings();
    let mut warnings = Vec::new();

    for (action_str, value) in config_keybindings {
        let action: KeyAction = match action_str.parse() {
            Ok(a) => a,
            Err(_) => {
                warnings.push(format!("unknown action '{}'", action_str));
                continue;
            }
        };

        // Parse key bindings
        let keys: Vec<String> = match value {
            toml::Value::String(s) => vec![s.clone()],
            toml::Value::Array(arr) => arr
                .iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect(),
            _ => {
                warnings.push(format!(
                    "'{}' has invalid value (expected string or array)",
                    action_str
                ));
                continue;
            }
        };

        let mut bindings = Vec::new();
        for key_str in &keys {
            match parse_key_string(key_str) {
                Some(b) => bindings.push(b),
                None => warnings.push(format!(
                    "could not parse key '{}' for action '{}'",
                    key_str, action_str
                )),
            }
        }
        if !bindings.is_empty() {
            map.insert(action, bindings);
        }
    }

    (map, warnings)
}

/// Get display string for the first binding of an action
pub fn key_display(action: &KeyAction, map: &KeyBindingMap) -> String {
    if let Some(bindings) = map.get(action) {
        if let Some(binding) = bindings.first() {
            let mut parts = Vec::new();
            if binding.modifiers.contains(KeyModifiers::CONTROL) {
                parts.push("Ctrl".to_string());
            }
            if binding.modifiers.contains(KeyModifiers::SHIFT) {
                parts.push("Shift".to_string());
            }
            if binding.modifiers.contains(KeyModifiers::ALT) {
                parts.push("Alt".to_string());
            }
            let key_name = match binding.code {
                KeyCode::Char(' ') => "Space".to_string(),
                KeyCode::Char(c) => c.to_string(),
                KeyCode::Enter => "Enter".to_string(),
                KeyCode::Esc => "Esc".to_string(),
                KeyCode::Tab => "Tab".to_string(),
                KeyCode::Backspace => "Backspace".to_string(),
                KeyCode::Up => "\u{2191}".to_string(),
                KeyCode::Down => "\u{2193}".to_string(),
                KeyCode::Left => "\u{2190}".to_string(),
                KeyCode::Right => "\u{2192}".to_string(),
                KeyCode::Home => "Home".to_string(),
                KeyCode::End => "End".to_string(),
                KeyCode::PageUp => "PgUp".to_string(),
                KeyCode::PageDown => "PgDn".to_string(),
                KeyCode::F(n) => format!("F{}", n),
                _ => "?".to_string(),
            };
            parts.push(key_name);
            return parts.join("+");
        }
    }
    "?".to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};

    fn make_key(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
        KeyEvent {
            code,
            modifiers,
            kind: KeyEventKind::Press,
            state: KeyEventState::NONE,
        }
    }

    #[test]
    fn test_default_keybindings_contains_basics() {
        let map = default_keybindings();
        assert!(map.contains_key(&KeyAction::Quit));
        assert!(map.contains_key(&KeyAction::ForceQuit));
        assert!(map.contains_key(&KeyAction::MoveUp));
        assert!(map.contains_key(&KeyAction::MoveDown));
        assert!(map.contains_key(&KeyAction::Select));
    }

    #[test]
    fn test_key_binding_matches() {
        let binding = KeyBinding::new(KeyCode::Char('q'));
        let key = make_key(KeyCode::Char('q'), KeyModifiers::NONE);
        assert!(binding.matches(&key));

        let wrong_key = make_key(KeyCode::Char('w'), KeyModifiers::NONE);
        assert!(!binding.matches(&wrong_key));
    }

    #[test]
    fn test_key_binding_ctrl_matches() {
        let binding = KeyBinding::with_ctrl(KeyCode::Char('q'));
        let key = make_key(KeyCode::Char('q'), KeyModifiers::CONTROL);
        assert!(binding.matches(&key));

        let plain_key = make_key(KeyCode::Char('q'), KeyModifiers::NONE);
        assert!(!binding.matches(&plain_key));
    }

    #[test]
    fn test_parse_key_string_simple() {
        let b = parse_key_string("q").unwrap();
        assert_eq!(b.code, KeyCode::Char('q'));
        assert_eq!(b.modifiers, KeyModifiers::NONE);
    }

    #[test]
    fn test_parse_key_string_ctrl() {
        let b = parse_key_string("Ctrl+q").unwrap();
        assert_eq!(b.code, KeyCode::Char('q'));
        assert_eq!(b.modifiers, KeyModifiers::CONTROL);
    }

    #[test]
    fn test_parse_key_string_special() {
        let b = parse_key_string("Enter").unwrap();
        assert_eq!(b.code, KeyCode::Enter);

        let b = parse_key_string("Space").unwrap();
        assert_eq!(b.code, KeyCode::Char(' '));

        let b = parse_key_string("Tab").unwrap();
        assert_eq!(b.code, KeyCode::Tab);
    }

    #[test]
    fn test_parse_key_string_shift() {
        let b = parse_key_string("Shift+Tab").unwrap();
        assert_eq!(b.code, KeyCode::Tab);
        assert_eq!(b.modifiers, KeyModifiers::SHIFT);
    }

    #[test]
    fn test_build_keybindings_override() {
        let mut overrides = HashMap::new();
        overrides.insert("quit".to_string(), toml::Value::String("x".to_string()));
        let (map, warnings) = build_keybindings(&overrides);

        // Quit should now be 'x'
        let bindings = map.get(&KeyAction::Quit).unwrap();
        assert_eq!(bindings.len(), 1);
        assert_eq!(bindings[0].code, KeyCode::Char('x'));
        assert!(warnings.is_empty());
    }

    #[test]
    fn test_build_keybindings_array_override() {
        let mut overrides = HashMap::new();
        overrides.insert(
            "quit".to_string(),
            toml::Value::Array(vec![
                toml::Value::String("x".to_string()),
                toml::Value::String("Ctrl+w".to_string()),
            ]),
        );
        let (map, warnings) = build_keybindings(&overrides);

        let bindings = map.get(&KeyAction::Quit).unwrap();
        assert_eq!(bindings.len(), 2);
        assert_eq!(bindings[0].code, KeyCode::Char('x'));
        assert_eq!(bindings[1].code, KeyCode::Char('w'));
        assert_eq!(bindings[1].modifiers, KeyModifiers::CONTROL);
        assert!(warnings.is_empty());
    }

    #[test]
    fn test_key_display() {
        let map = default_keybindings();
        let display = key_display(&KeyAction::Quit, &map);
        assert_eq!(display, "q");

        let display = key_display(&KeyAction::ForceQuit, &map);
        assert_eq!(display, "Ctrl+q");

        let display = key_display(&KeyAction::Select, &map);
        assert_eq!(display, "Enter");
    }

    #[test]
    fn test_unknown_action_warns() {
        let mut overrides = HashMap::new();
        overrides.insert(
            "nonexistent_action".to_string(),
            toml::Value::String("x".to_string()),
        );
        let (map, warnings) = build_keybindings(&overrides);
        assert!(map.contains_key(&KeyAction::Quit)); // defaults still present
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].contains("unknown action"));
        assert!(warnings[0].contains("nonexistent_action"));
    }

    #[test]
    fn test_unparseable_key_warns() {
        let mut overrides = HashMap::new();
        overrides.insert(
            "quit".to_string(),
            toml::Value::String("Crtl+q".to_string()), // typo
        );
        let (map, warnings) = build_keybindings(&overrides);
        // Default binding should remain since the override failed to parse
        let bindings = map.get(&KeyAction::Quit).unwrap();
        assert_eq!(bindings[0].code, KeyCode::Char('q'));
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].contains("could not parse"));
        assert!(warnings[0].contains("Crtl+q"));
    }

    #[test]
    fn test_invalid_value_type_warns() {
        let mut overrides = HashMap::new();
        overrides.insert("quit".to_string(), toml::Value::Integer(42));
        let (map, warnings) = build_keybindings(&overrides);
        // Default binding should remain
        let bindings = map.get(&KeyAction::Quit).unwrap();
        assert_eq!(bindings[0].code, KeyCode::Char('q'));
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].contains("invalid value"));
    }

    #[test]
    fn test_parse_key_string_invalid_inputs() {
        // Empty string
        assert!(parse_key_string("").is_none());
        // Too many parts
        assert!(parse_key_string("Ctrl+Shift+X").is_none());
        // Unknown modifier
        assert!(parse_key_string("Meta+q").is_none());
        // Trailing +
        assert!(parse_key_string("Ctrl+").is_none());
        // Multi-char key name that isn't a special key
        assert!(parse_key_string("abc").is_none());
    }
}