Skip to content

Commit

Permalink
add ability to check whether a preview is outdated
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Jul 1, 2022
1 parent 27d624c commit 18e13c9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/config/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ pub struct JoshutoPreviewEntry {
}

#[derive(Debug, Default, Deserialize)]
struct JoshutoPreviewCrude {
struct JoshutoPreviewRaw {
pub extension: Option<HashMap<String, JoshutoPreviewEntry>>,
pub mimetype: Option<HashMap<String, JoshutoPreviewEntry>>,
}

impl From<JoshutoPreviewCrude> for JoshutoPreview {
fn from(crude: JoshutoPreviewCrude) -> Self {
impl From<JoshutoPreviewRaw> for JoshutoPreview {
fn from(crude: JoshutoPreviewRaw) -> Self {
let extension = crude.extension.unwrap_or_default();
let mimetype = crude.mimetype.unwrap_or_default();

Expand All @@ -35,6 +35,6 @@ pub struct JoshutoPreview {

impl TomlConfigFile for JoshutoPreview {
fn get_config(file_name: &str) -> Self {
parse_to_config_file::<JoshutoPreviewCrude, JoshutoPreview>(file_name).unwrap_or_default()
parse_to_config_file::<JoshutoPreviewRaw, JoshutoPreview>(file_name).unwrap_or_default()
}
}
7 changes: 6 additions & 1 deletion src/preview/preview_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ pub fn load_preview_path(context: &mut AppContext, p: path::PathBuf, metadata: J
let need_to_load = context
.preview_context_ref()
.get_preview_ref(p.as_path())
.is_none();
.map(|p| {
p.as_ref()
.map(|p| p.modified < metadata.modified())
.unwrap_or(true)
})
.unwrap_or(true);

if need_to_load {
preview_file::Background::preview_path_with_script(context, p);
Expand Down
4 changes: 4 additions & 0 deletions src/preview/preview_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path;
use std::process::{Command, Output};
use std::thread;
use std::time;

use crate::context::AppContext;
use crate::event::AppEvent;
Expand All @@ -10,16 +11,19 @@ pub struct FilePreview {
pub status: std::process::ExitStatus,
pub output: String,
pub index: usize,
pub modified: time::SystemTime,
}

impl std::convert::From<Output> for FilePreview {
fn from(output: Output) -> Self {
let s = String::from_utf8_lossy(&output.stdout).to_string();
let s2 = s.replace('\t', " ");
let status = output.status;
let modified = time::SystemTime::now();
Self {
status,
output: s2,
modified,
index: 0,
}
}
Expand Down

0 comments on commit 18e13c9

Please sign in to comment.