television_previewers/previewers/
env.rs

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
use rustc_hash::FxHashMap;
use std::sync::Arc;

use crate::previewers::{Preview, PreviewContent};
use television_channels::entry;

#[derive(Debug, Default)]
pub struct EnvVarPreviewer {
    cache: FxHashMap<entry::Entry, Arc<Preview>>,
    _config: EnvVarPreviewerConfig,
}

#[derive(Debug, Default)]
pub struct EnvVarPreviewerConfig {}

impl EnvVarPreviewer {
    pub fn new(config: Option<EnvVarPreviewerConfig>) -> Self {
        EnvVarPreviewer {
            cache: FxHashMap::default(),
            _config: config.unwrap_or_default(),
        }
    }

    pub fn preview(&mut self, entry: &entry::Entry) -> Arc<Preview> {
        // check if we have that preview in the cache
        if let Some(preview) = self.cache.get(entry) {
            return preview.clone();
        }
        let preview = Arc::new(Preview {
            title: entry.name.clone(),
            content: if let Some(preview) = &entry.value {
                PreviewContent::PlainTextWrapped(
                    maybe_add_newline_after_colon(preview, &entry.name),
                )
            } else {
                PreviewContent::Empty
            },
            icon: entry.icon,
            ..Default::default()
        });
        self.cache.insert(entry.clone(), preview.clone());
        preview
    }
}

const PATH: &str = "PATH";

fn maybe_add_newline_after_colon(s: &str, name: &str) -> String {
    if name.contains(PATH) {
        return s.replace(':', "\n");
    }
    s.to_string()
}