rs-rich-interact 0.0.12

Interactive terminal components for rs-rich: event loop, viewport, item model, headless testing
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
//! The universal item model (#452): one type behind every picker.
//!
//! File pickers, command palettes, plugin browsers and history search all
//! show the same thing: a value with a label, an optional description,
//! details, a preview and actions. [`Item`] carries it, so each selector
//! renders, searches and previews items the same way instead of inventing
//! its own row type.
//!
//! Actions (#491) attach to one item, or to every target of a view through
//! [`Actions`]: list items, table rows, tree nodes and file entries alike,
//! told apart by an [`ActionTarget`]'s [`TargetKind`]. Plugins add actions
//! through `rs-rich-plugin-api`'s `CustomAction`, which
//! [`Actions::from_registry`] turns into these.

use std::fmt;
use std::sync::Arc;

use rich::{Renderable, Text};

use crate::event::Key;

/// What a preview pane shows for an item.
#[derive(Clone)]
pub enum Preview {
    /// Plain text.
    Text(String),
    /// Console markup (`[bold]…[/]`).
    Markup(String),
    /// Any renderable: a `Syntax`, a `Table`, a `Panel`.
    Renderable(Arc<dyn Renderable + Send + Sync>),
}

impl Preview {
    /// The preview as something to render.
    pub fn renderable(&self) -> Arc<dyn Renderable + Send + Sync> {
        match self {
            Preview::Text(text) => Arc::new(Text::new(text.clone())),
            Preview::Markup(markup) => {
                Arc::new(Text::from_markup(markup).unwrap_or_else(|_| Text::new(markup.clone())))
            }
            Preview::Renderable(renderable) => Arc::clone(renderable),
        }
    }
}

impl fmt::Debug for Preview {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Preview::Text(text) => f.debug_tuple("Text").field(text).finish(),
            Preview::Markup(markup) => f.debug_tuple("Markup").field(markup).finish(),
            Preview::Renderable(_) => f.write_str("Renderable(..)"),
        }
    }
}

/// Something that can be done to an item, bound to a key: "open in
/// $EDITOR" on `ctrl+e`, "delete" on `ctrl+d`. Every action is also in the
/// view's action menu; one without a key is only there.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Action {
    /// A stable name the caller matches on.
    pub id: String,
    /// The label shown in a help line and the action menu.
    pub label: String,
    pub key: Option<Key>,
}

impl Action {
    pub fn new(id: impl Into<String>, label: impl Into<String>, key: Key) -> Action {
        Action {
            id: id.into(),
            label: label.into(),
            key: Some(key),
        }
    }

    /// An action with no key of its own: picked from the action menu.
    pub fn menu(id: impl Into<String>, label: impl Into<String>) -> Action {
        Action {
            id: id.into(),
            label: label.into(),
            key: None,
        }
    }
}

/// What kind of thing an action is done to.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TargetKind {
    /// An item of a list ([`Select`](crate::Select)).
    #[default]
    Item,
    /// A row of a table ([`TableSelect`](crate::TableSelect)).
    Row,
    /// A node of a tree ([`TreeSelect`](crate::TreeSelect)).
    Node,
    /// A file or directory ([`FilePicker`](crate::FilePicker)).
    File,
    /// A region of the screen rather than an item in it: a pane, a tab, or
    /// any component wrapped in [`Overlays`](crate::overlay::Overlays)
    /// with a region of its own (#474).
    Region,
}

impl TargetKind {
    /// `item`, `row`, `node`, `file` or `region`: the name plugins see.
    pub fn as_str(self) -> &'static str {
        match self {
            TargetKind::Item => "item",
            TargetKind::Row => "row",
            TargetKind::Node => "node",
            TargetKind::File => "file",
            TargetKind::Region => "region",
        }
    }

    pub fn parse(name: &str) -> Option<TargetKind> {
        Some(match name {
            "item" => TargetKind::Item,
            "row" => TargetKind::Row,
            "node" => TargetKind::Node,
            "file" => TargetKind::File,
            "region" => TargetKind::Region,
            _ => return None,
        })
    }
}

/// The thing an action is about to be done to.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ActionTarget {
    pub kind: TargetKind,
    /// The item's index in the view's items.
    pub index: usize,
    /// Its label, as shown.
    pub label: String,
    /// What identifies it: an item's label, a row's cells joined by tabs, a
    /// node's path of labels joined by `/`, a file's path (with bytes that
    /// are not UTF-8 as `\xNN`).
    pub value: String,
}

/// Whether an action applies to a target.
pub type ActionFilter = Arc<dyn Fn(&ActionTarget) -> bool + Send + Sync>;

/// Actions a view offers on every target, beside each item's own: the
/// caller's, and those plugins registered. Each may be limited to some
/// targets.
#[derive(Clone, Default)]
pub struct Actions {
    entries: Vec<(Action, Option<ActionFilter>)>,
}

impl fmt::Debug for Actions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries(self.entries.iter().map(|(action, _)| action))
            .finish()
    }
}

impl Actions {
    pub fn new() -> Actions {
        Actions::default()
    }

    /// Offer `action` on every target.
    #[must_use]
    pub fn action(mut self, action: Action) -> Actions {
        self.entries.push((action, None));
        self
    }

    /// Offer `action` on targets of one kind.
    #[must_use]
    pub fn action_for(self, kind: TargetKind, action: Action) -> Actions {
        self.action_if(action, move |target| target.kind == kind)
    }

    /// Offer `action` where `filter` says it applies.
    #[must_use]
    pub fn action_if(
        mut self,
        action: Action,
        filter: impl Fn(&ActionTarget) -> bool + Send + Sync + 'static,
    ) -> Actions {
        self.entries.push((action, Some(Arc::new(filter))));
        self
    }

    /// Every action from `other` after these.
    #[must_use]
    pub fn extend(mut self, other: Actions) -> Actions {
        self.entries.extend(other.entries);
        self
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// The actions for `target`: the item's `own` first, then these where
    /// they apply. An id already offered is not offered again.
    pub fn for_target(&self, target: &ActionTarget, own: &[Action]) -> Vec<Action> {
        let mut out: Vec<Action> = Vec::new();
        let applies = self
            .entries
            .iter()
            .filter(|(_, filter)| filter.as_ref().is_none_or(|filter| filter(target)));
        for action in own.iter().chain(applies.map(|(action, _)| action)) {
            if !out.iter().any(|known| known.id == action.id) {
                out.push(action.clone());
            }
        }
        out
    }

    /// The custom actions plugins registered in `registry`
    /// (`PluginRegistrar::action`): each under its registered name, on the
    /// targets it says it applies to. A key name that does not parse leaves
    /// the action in the menu only.
    pub fn from_registry(registry: &rich_ext::registry::ExtensionRegistry) -> Actions {
        let mut actions = Actions::new();
        for (name, _, action) in registry.actions() {
            let entry = Action {
                id: name.to_string(),
                label: action.label(),
                key: action.key().as_deref().and_then(Key::parse),
            };
            actions = actions.action_if(entry, move |target| {
                action.applies(target.kind.as_str(), &target.value)
            });
        }
        actions
    }
}

/// One choosable thing: a value of any type, and how to show and find it.
#[derive(Clone, Debug)]
#[must_use = "builder methods return a new value; it does nothing until it is rendered or used"]
pub struct Item<T> {
    pub value: T,
    /// The main line.
    pub label: String,
    /// A dimmer second line or trailing note.
    pub description: Option<String>,
    /// Details shown as `key: value`, and searched.
    pub metadata: Vec<(String, String)>,
    pub preview: Option<Preview>,
    pub actions: Vec<Action>,
    /// Extra text that matches a search without being shown (aliases,
    /// keywords, a full path behind a short label).
    pub keywords: Vec<String>,
}

impl<T> Item<T> {
    pub fn new(value: T, label: impl Into<String>) -> Item<T> {
        Item {
            value,
            label: label.into(),
            description: None,
            metadata: Vec::new(),
            preview: None,
            actions: Vec::new(),
            keywords: Vec::new(),
        }
    }

    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn meta(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.push((key.into(), value.into()));
        self
    }

    pub fn preview(mut self, preview: Preview) -> Self {
        self.preview = Some(preview);
        self
    }

    pub fn action(mut self, action: Action) -> Self {
        self.actions.push(action);
        self
    }

    pub fn keyword(mut self, keyword: impl Into<String>) -> Self {
        self.keywords.push(keyword.into());
        self
    }

    /// Everything a search matches against, label first, one field per line.
    pub fn search_text(&self) -> String {
        let mut text = self.label.clone();
        for field in self
            .description
            .iter()
            .chain(self.metadata.iter().map(|(_, value)| value))
            .chain(&self.keywords)
        {
            text.push('\n');
            text.push_str(field);
        }
        text
    }

    /// The action bound to `key`, if any: one bound to `key` itself before
    /// one it only [`matches`](Key::matches).
    pub fn action_for(&self, key: Key) -> Option<&Action> {
        key.pick(self.actions.iter().map(|action| action.key.as_slice()))
            .map(|at| &self.actions[at])
    }

    /// The same item with its value mapped.
    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Item<U> {
        Item {
            value: f(self.value),
            label: self.label,
            description: self.description,
            metadata: self.metadata,
            preview: self.preview,
            actions: self.actions,
            keywords: self.keywords,
        }
    }
}

impl<T: fmt::Display> From<T> for Item<T> {
    /// An item labelled with its value's `Display`.
    fn from(value: T) -> Item<T> {
        let label = value.to_string();
        Item::new(value, label)
    }
}

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

    #[test]
    fn an_action_bound_to_the_key_itself_wins_over_an_earlier_match() {
        let key = |name| Key::parse(name).unwrap();
        let item = Item::new((), "row")
            .action(Action::new("indent", "Indent", key("ctrl+i")))
            .action(Action::new("next", "Next", key("tab")));
        // A legacy Tab fires both bindings; the one that names it wins.
        assert_eq!(item.action_for(key("tab")).unwrap().id, "next");
        assert_eq!(item.action_for(key("ctrl+i")).unwrap().id, "indent");
        let item = Item::new((), "row").action(Action::new("indent", "Indent", key("ctrl+i")));
        assert_eq!(item.action_for(key("tab")).unwrap().id, "indent");
    }

    #[test]
    fn builds_and_searches_items() {
        let item = Item::new(std::path::PathBuf::from("src/main.rs"), "main.rs")
            .description("the entry point")
            .meta("size", "2 KB")
            .keyword("src/main.rs")
            .preview(Preview::Text("fn main() {}".into()))
            .action(Action::new("edit", "Open in $EDITOR", Key::ctrl('e')));
        assert_eq!(
            item.search_text(),
            "main.rs\nthe entry point\n2 KB\nsrc/main.rs"
        );
        assert_eq!(item.action_for(Key::ctrl('e')).unwrap().id, "edit");
        assert!(item.action_for(Key::ctrl('d')).is_none());
        let named = item.map(|path| path.display().to_string());
        assert_eq!(named.value, "src/main.rs");
        assert_eq!(Item::from(42).label, "42");
    }

    fn target(kind: TargetKind, value: &str) -> ActionTarget {
        ActionTarget {
            kind,
            index: 0,
            label: value.into(),
            value: value.into(),
        }
    }

    #[test]
    fn view_actions_apply_by_target() {
        let actions = Actions::new()
            .action(Action::menu("copy", "Copy"))
            .action_for(
                TargetKind::File,
                Action::new("edit", "Edit", Key::ctrl('e')),
            )
            .action_if(Action::menu("rust", "Rust only"), |target| {
                target.value.ends_with(".rs")
            });
        let own = [Action::new("open", "Open", Key::ctrl('o'))];
        let ids = |target: &ActionTarget| -> Vec<String> {
            actions
                .for_target(target, &own)
                .into_iter()
                .map(|action| action.id)
                .collect()
        };
        assert_eq!(
            ids(&target(TargetKind::File, "main.rs")),
            ["open", "copy", "edit", "rust"]
        );
        assert_eq!(ids(&target(TargetKind::Row, "a\tb")), ["open", "copy"]);
        // An item's own action wins over a view's of the same id.
        let shadow = [Action::menu("copy", "Copy path")];
        let found = actions.for_target(&target(TargetKind::Item, "x"), &shadow);
        assert_eq!(found[0].label, "Copy path");
        assert_eq!(found.len(), 1);
        assert_eq!(TargetKind::parse("node"), Some(TargetKind::Node));
        assert_eq!(TargetKind::Node.as_str(), "node");
    }

    #[test]
    fn plugin_actions_come_from_the_registry() {
        use rich_plugin_api::{CustomAction, Plugin, PluginError, PluginMetadata, PluginRegistrar};
        struct Reveal;
        impl CustomAction for Reveal {
            fn label(&self) -> String {
                "Reveal".into()
            }
            fn key(&self) -> Option<String> {
                Some("ctrl+r".into())
            }
            fn applies(&self, kind: &str, value: &str) -> bool {
                kind == "file" && !value.starts_with('.')
            }
        }
        struct Files;
        impl Plugin for Files {
            fn metadata(&self) -> PluginMetadata {
                PluginMetadata::new("files", "Files", "0.0.0")
            }
            fn register(&self, registrar: &mut dyn PluginRegistrar) -> Result<(), PluginError> {
                registrar.action("reveal", Arc::new(Reveal));
                Ok(())
            }
        }
        let mut registry = rich_ext::registry::ExtensionRegistry::new();
        registry.add_plugin(&Files).unwrap();
        let actions = Actions::from_registry(&registry);
        let found = actions.for_target(&target(TargetKind::File, "a.txt"), &[]);
        assert_eq!(found, [Action::new("reveal", "Reveal", Key::ctrl('r'))]);
        assert!(
            actions
                .for_target(&target(TargetKind::File, ".env"), &[])
                .is_empty()
        );
        assert!(
            actions
                .for_target(&target(TargetKind::Item, "a.txt"), &[])
                .is_empty()
        );
    }
}