raffi 0.25.0

A simple fuzzel launcher based on YAML file
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
use super::preview::describe_file_action;
use crate::AddonsConfig;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum HelpSection {
    KeywordsAndActions,
    KeyboardShortcuts,
}

impl HelpSection {
    pub(super) fn heading(self) -> &'static str {
        match self {
            Self::KeywordsAndActions => "Keywords and actions",
            Self::KeyboardShortcuts => "Keyboard shortcuts",
        }
    }
}

/// A single row of the keyword help list shown with `Ctrl+/`.
#[derive(Debug, Clone, PartialEq)]
pub(super) struct HelpEntry {
    pub section: HelpSection,
    pub trigger: String,
    pub title: String,
    pub usage: String,
    pub icon: Option<String>,
    /// Text placed in the search input when the row is selected. `None` marks
    /// an informational row that cannot be armed, such as the calculator.
    pub completion: Option<String>,
}

/// Keys of the password manager lists, shown in their help rows.
const VAULT_KEYS: &str =
    "Enter copies the password, Ctrl+Enter types it, Ctrl+U copies the username and Ctrl+O the OTP code";

/// Build the help rows for every enabled addon.
///
/// The order mirrors [`super::app::route_query`] precedence so the help list
/// reflects which trigger actually wins when two of them overlap.
pub(super) fn help_entries(addons: &AddonsConfig) -> Vec<HelpEntry> {
    let mut entries = Vec::new();

    if addons.file_browser.enabled {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: "~/".to_string(),
            title: "File browser".to_string(),
            usage: "~/ or /path/to/dir".to_string(),
            icon: None,
            completion: Some("~/".to_string()),
        });
    }

    for config in &addons.script_filters {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: config.keyword.clone(),
            title: config.name.clone(),
            usage: format!("{} <query>", config.keyword),
            icon: config.icon.clone(),
            completion: Some(format!("{} ", config.keyword)),
        });
    }

    for config in &addons.text_snippets {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: config.keyword.clone(),
            title: config.name.clone(),
            usage: format!("{} <filter>", config.keyword),
            icon: config.icon.clone(),
            completion: Some(format!("{} ", config.keyword)),
        });
    }

    if addons.emoji.enabled {
        let trigger = addons.emoji.trigger.as_deref().unwrap_or("emoji");
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Emoji and icons".to_string(),
            usage: format!("{trigger} <name>"),
            icon: None,
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.clipboard.enabled {
        let trigger = addons.clipboard.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Clipboard history".to_string(),
            usage: format!("{trigger} <text>"),
            icon: Some(super::app::CLIPBOARD_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
        let trigger = addons.clipboard.image_trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Clipboard images".to_string(),
            usage: format!("{trigger} <text>"),
            icon: Some(super::app::CLIPBOARD_IMAGES_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    for config in &addons.web_searches {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: config.keyword.clone(),
            title: config.name.clone(),
            usage: format!("{} <query>", config.keyword),
            icon: config.icon.clone(),
            completion: Some(format!("{} ", config.keyword)),
        });
    }

    if addons.system.enabled {
        let trigger = addons.system.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "System commands".to_string(),
            usage: format!("{trigger} <lock|suspend|hibernate|log out|reboot|shut down>"),
            icon: Some(super::app::SYSTEM_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.windows.enabled {
        let trigger = addons.windows.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Window switcher".to_string(),
            usage: format!("{trigger} <title or app>, Ctrl+Enter brings it here"),
            icon: Some(super::app::WINDOWS_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.browser_tabs.enabled {
        let trigger = addons.browser_tabs.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Browser tabs".to_string(),
            usage: format!("{trigger} <title or URL>, Enter switches, Ctrl+Enter closes"),
            icon: Some(super::app::BROWSER_TABS_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.media.enabled {
        let trigger = addons.media.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Media controls".to_string(),
            usage: format!("{trigger} <action or player>, Enter plays, pauses or skips"),
            icon: Some(super::app::MEDIA_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.audio.enabled {
        let trigger = addons.audio.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Audio devices".to_string(),
            usage: format!(
                "{trigger} <device or volume>, Enter sets the default, Ctrl+Enter mutes"
            ),
            icon: Some(super::app::AUDIO_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.kill.enabled {
        let trigger = addons.kill.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Kill process".to_string(),
            usage: format!("{trigger} <name>, Enter twice sends SIGTERM, Ctrl+Enter twice SIGKILL"),
            icon: Some(super::app::KILL_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.bluetooth.enabled {
        let trigger = addons.bluetooth.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Bluetooth".to_string(),
            usage: format!("{trigger} <device>, Enter connects, or disconnects when pressed twice"),
            icon: Some(super::app::BLUETOOTH_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.pass.enabled {
        let trigger = addons.pass.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Password store".to_string(),
            usage: format!("{trigger} <entry>, {VAULT_KEYS}"),
            icon: Some(super::app::PASS_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.bitwarden.enabled {
        let trigger = addons.bitwarden.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Bitwarden".to_string(),
            usage: format!("{trigger} <item>, {VAULT_KEYS}"),
            icon: Some(super::app::BITWARDEN_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.slack.enabled {
        let trigger = addons.slack.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Slack".to_string(),
            usage: format!(
                "{trigger} <name>, {trigger} search <words>, {trigger} people <name>, {trigger} unread, {trigger} status <text>"
            ),
            icon: Some(super::slack::SLACK_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.devtools.enabled {
        let trigger = addons.devtools.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Developer tools".to_string(),
            usage: format!("{trigger} <text> to encode, decode, hash or reformat it"),
            icon: Some(super::app::DEVTOOLS_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.terminal.enabled {
        let trigger = addons.terminal.trigger();
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Run in terminal".to_string(),
            usage: format!("{trigger} <command>, Enter runs it in a terminal"),
            icon: Some(super::terminal::TERMINAL_ICON.to_string()),
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.calculator.enabled {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: "math".to_string(),
            title: "Calculator".to_string(),
            usage: "2+2*3 or (10/4)^2".to_string(),
            icon: None,
            completion: None,
        });
    }

    if addons.currency.enabled {
        let trigger = addons.currency.trigger.as_deref().unwrap_or("$");
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: trigger.to_string(),
            title: "Currency converter".to_string(),
            usage: "100 usd to eur".to_string(),
            icon: None,
            completion: Some(format!("{trigger} ")),
        });
    }

    if addons.units.enabled {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: "units".to_string(),
            title: "Unit converter".to_string(),
            usage: "10 km to mi or 70f to c".to_string(),
            icon: None,
            completion: None,
        });
    }

    if addons.color.enabled {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: "color".to_string(),
            title: "Color converter".to_string(),
            usage: "#ff8800, rgb(255 136 0) or hsl(32 100% 50%)".to_string(),
            icon: None,
            completion: None,
        });
    }

    if addons.world_clock.enabled {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: "time".to_string(),
            title: "World clock".to_string(),
            usage: "time tokyo or 15:00 paris to nyc".to_string(),
            icon: None,
            completion: None,
        });
    }

    if addons.devtools.enabled {
        entries.push(HelpEntry {
            section: HelpSection::KeywordsAndActions,
            trigger: "uuid/pw".to_string(),
            title: "UUID and password generator".to_string(),
            usage: "uuid, pw or pw 32".to_string(),
            icon: None,
            completion: None,
        });
    }

    entries.extend(keyboard_shortcut_entries(addons));

    entries
}

/// Informational rows listing the global keyboard shortcuts, shown after the
/// keyword rows. They cannot be armed, so Enter and Tab do nothing on them.
fn keyboard_shortcut_entries(addons: &AddonsConfig) -> Vec<HelpEntry> {
    let shortcut = |trigger: &str, title: &str, usage: &str| HelpEntry {
        section: HelpSection::KeyboardShortcuts,
        trigger: trigger.to_string(),
        title: title.to_string(),
        usage: usage.to_string(),
        icon: None,
        completion: None,
    };

    let mut entries = vec![
        shortcut(
            "↑/↓",
            "Navigate results",
            "Moves the selection through the results",
        ),
        shortcut(
            "Ctrl+J",
            "Scroll preview down",
            "Scrolls the right-hand preview pane",
        ),
        shortcut(
            "Ctrl+K",
            "Scroll preview up",
            "Scrolls the right-hand preview pane",
        ),
        shortcut(
            "Ctrl+P",
            "Toggle preview pane",
            "Shows or hides the right-hand preview",
        ),
    ];
    let file_browser = &addons.file_browser;
    if file_browser.enabled {
        entries.extend([
            shortcut(
                "Ctrl+H",
                "Toggle hidden files",
                "File browser: show or hide dotfiles",
            ),
            shortcut(
                "Ctrl+O",
                "Open with xdg-open",
                "File browser: opens the selected file or directory",
            ),
            shortcut(
                "Ctrl+B",
                "Open in file manager",
                "File browser: opens the current directory with the entry selected",
            ),
            shortcut(
                "Ctrl+Y",
                "Copy path",
                "File browser: copies the selected path to the clipboard",
            ),
            shortcut(
                "Ctrl+Alt+Y",
                "Copy image",
                "File browser: copies a supported image file to the clipboard",
            ),
            shortcut(
                "Ctrl+S",
                "Cycle sort order",
                "File browser: name, modified date, created date",
            ),
            shortcut(
                "Ctrl+.",
                "Cycle files/directories",
                "File browser: all entries, files only, directories only",
            ),
        ]);
    }
    if addons.clipboard.enabled {
        entries.push(shortcut(
            "Ctrl+R",
            "Refresh clipboard history",
            "Clipboard history: reload the list from the clipboard manager",
        ));
        entries.push(shortcut(
            "Ctrl+I",
            "Show only images",
            "Clipboard history: switch between all items and images only",
        ));
        entries.push(shortcut(
            "Ctrl+D",
            "Delete clipboard entry",
            "Clipboard history: remove the selected item from CopyQ",
        ));
    }
    if addons.pass.enabled || addons.bitwarden.enabled {
        entries.extend([
            shortcut(
                "Ctrl+U",
                "Copy username",
                "Password managers: copies the username of the selected entry",
            ),
            shortcut(
                "Ctrl+O",
                "Copy OTP code",
                "Password managers: copies the current one-time code",
            ),
        ]);
    }
    entries.extend([
        shortcut("Alt+P/N", "Search history", "Previous / next query"),
        shortcut(
            "Tab",
            "Complete selection",
            "Completes the keyword or file browser path",
        ),
    ]);
    if file_browser.enabled && file_browser.fido_style {
        entries.extend([
            shortcut(
                "Backspace",
                "Parent directory",
                "File browser: on a trailing / goes up one directory",
            ),
            shortcut(
                "~/ or / after /",
                "Home or root",
                "File browser: starts the path over from ~/ or /",
            ),
        ]);
    }
    entries.extend([
        shortcut("Ctrl+/", "Keyword and shortcut help", "Opens this list"),
        shortcut("Esc", "Close help or quit", "Closes help, otherwise exits"),
    ]);
    let (enter_usage, ctrl_enter_usage) = if file_browser.enabled {
        (
            format!(
                "Launches, arms or activates the selection. File browser files: {}",
                describe_file_action(file_browser.action())
            ),
            format!(
                "Runs the addon's secondary action. File browser: {}",
                describe_file_action(file_browser.secondary_action())
            ),
        )
    } else {
        (
            "Launches, arms or activates the selection".to_string(),
            "Runs the addon's secondary action".to_string(),
        )
    };
    entries.extend([
        shortcut("Enter", "Use selected entry", &enter_usage),
        shortcut("Ctrl+Enter", "Secondary action", &ctrl_enter_usage),
    ]);
    entries
}

/// List Ctrl+Tab after Tab, for a configured `general.main_view`.
pub(super) fn add_main_view_shortcut(entries: &mut Vec<HelpEntry>) {
    let at = entries
        .iter()
        .position(|entry| entry.section == HelpSection::KeyboardShortcuts && entry.trigger == "Tab")
        .map_or(entries.len(), |index| index + 1);
    entries.insert(
        at,
        HelpEntry {
            section: HelpSection::KeyboardShortcuts,
            trigger: "Ctrl+Tab".to_string(),
            title: "Cycle main view sources".to_string(),
            usage: "Lists one main_view source at a time, then all of them; Shift goes back"
                .to_string(),
            icon: None,
            completion: None,
        },
    );
}

/// Return the indices of the help rows matching `query`.
///
/// Matching is a case-insensitive substring test over the trigger, title and
/// usage text. An empty query keeps every row, in declaration order.
pub(super) fn filter_help_entries(entries: &[HelpEntry], query: &str) -> Vec<usize> {
    let needle = query.trim().to_lowercase();
    if needle.is_empty() {
        return (0..entries.len()).collect();
    }

    entries
        .iter()
        .enumerate()
        .filter(|(_, entry)| {
            entry.trigger.to_lowercase().contains(&needle)
                || entry.title.to_lowercase().contains(&needle)
                || entry.usage.to_lowercase().contains(&needle)
        })
        .map(|(index, _)| index)
        .collect()
}