-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d028163
commit 5948f67
Showing
12 changed files
with
185 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
use super::{display::StyledString, finders::Entry}; | ||
use super::finders::Entry; | ||
|
||
mod env; | ||
|
||
// previewer types | ||
pub use env::EnvVarPreviewer; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum PreviewContent { | ||
PlainText(String), | ||
HighlightedText(String), | ||
Empty, | ||
} | ||
|
||
/// A preview of an entry. | ||
/// | ||
/// # Fields | ||
/// - `title`: The title of the preview. | ||
/// - `content`: The content of the preview. | ||
#[derive(Debug, Clone)] | ||
pub struct Preview { | ||
pub title: String, | ||
pub content: Vec<StyledString>, | ||
// maybe images too and other formats | ||
pub content: PreviewContent, | ||
} | ||
|
||
/// A trait for a previewer that can preview entries. | ||
/// | ||
/// # Methods | ||
/// - `preview`: Preview an entry and produce a preview. | ||
pub trait Previewer { | ||
fn preview(&self, e: Entry) -> Preview; | ||
fn preview(&mut self, e: &Entry) -> Preview; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,39 @@ | ||
use crate::components::display; | ||
use std::collections::HashMap; | ||
|
||
use crate::components::finders; | ||
use crate::components::previewers; | ||
use crate::components::previewers::{Preview, PreviewContent, Previewer}; | ||
|
||
pub struct EnvVarPreviewer {} | ||
pub struct EnvVarPreviewer { | ||
cache: HashMap<finders::Entry, Preview>, | ||
} | ||
|
||
impl EnvVarPreviewer { | ||
pub fn new() -> Self { | ||
EnvVarPreviewer {} | ||
EnvVarPreviewer { | ||
cache: HashMap::new(), | ||
} | ||
} | ||
} | ||
|
||
impl previewers::Previewer for EnvVarPreviewer { | ||
fn preview(&self, e: finders::Entry) -> previewers::Preview { | ||
previewers::Preview { | ||
title: e.name.clone(), | ||
content: vec![display::StyledString { | ||
text: e.preview.unwrap().clone(), | ||
style: display::StyleGroup::Entry, | ||
}], | ||
impl Previewer for EnvVarPreviewer { | ||
fn preview(&mut self, entry: &finders::Entry) -> Preview { | ||
// check if we have that preview in the cache | ||
if let Some(preview) = self.cache.get(entry) { | ||
return preview.clone(); | ||
} | ||
let preview = Preview { | ||
title: entry.name.clone(), | ||
content: if let Some(preview) = &entry.preview { | ||
PreviewContent::PlainText(add_newline_after_colon(&preview)) | ||
} else { | ||
PreviewContent::Empty | ||
}, | ||
}; | ||
self.cache.insert(entry.clone(), preview.clone()); | ||
preview | ||
} | ||
} | ||
|
||
fn add_newline_after_colon(s: &str) -> String { | ||
s.replace(":", ":\n") | ||
} |
Oops, something went wrong.