perf: trying things with the preview

This commit is contained in:
alexpasmantier 2025-01-12 22:08:03 +01:00
parent cb77f5c663
commit 4a62feb524
3 changed files with 51 additions and 35 deletions

View File

@ -156,10 +156,13 @@ impl Previewer {
debug!("Preview already in cache");
return Some(preview);
}
// if we've already acknowledged the request
if let Some(initial_request) = self.requests.get(entry) {
debug!("Request already acknowledged");
// and we're past the debounce duration
// if we haven't acknowledged the request yet, acknowledge it
if !self.requests.contains_key(entry) {
self.requests.insert(entry.clone(), Instant::now());
}
let initial_request = self.requests.get(entry).unwrap();
// if we're past the debounce duration
if initial_request.elapsed() > DEBOUNCE_DURATION {
debug!("Past debounce duration");
// forward the request to the appropriate previewer
@ -167,9 +170,7 @@ impl Previewer {
PreviewType::Basic => Some(self.basic.preview(entry)),
PreviewType::EnvVar => Some(self.env_var.preview(entry)),
PreviewType::Files => self.file.preview(entry),
PreviewType::Command(cmd) => {
self.command.preview(entry, cmd)
}
PreviewType::Command(cmd) => self.command.preview(entry, cmd),
PreviewType::None => Some(Arc::new(Preview::default())),
};
// if we got a preview, cache it
@ -181,14 +182,17 @@ impl Previewer {
}
} else {
debug!("Not past debounce duration");
None
// partial preview
let preview = match &entry.preview_type {
PreviewType::Basic => Some(self.basic.preview(entry)),
PreviewType::EnvVar => Some(self.env_var.preview(entry)),
PreviewType::Files => self.file.preview(entry),
PreviewType::Command(cmd) => {
self.command.partial_preview(entry, cmd)
}
}
// if we haven't acknowledged the request yet
else {
debug!("Request not acknowledged, acknowledging");
self.requests.insert(entry.clone(), Instant::now());
None
PreviewType::None => Some(Arc::new(Preview::default())),
};
Some(preview)
}
}

View File

@ -150,10 +150,11 @@ pub fn try_preview(
Ok(file) => {
// compute the highlighted version in the background
let mut reader = BufReader::new(file);
reader.seek(std::io::SeekFrom::Start(0)).unwrap();
let preview = compute_highlighted_text_preview(
entry,
reader,
reader.lines().map_while(Result::ok).collect(),
syntax_set,
syntax_theme,
);
@ -176,7 +177,7 @@ pub fn try_preview(
fn compute_highlighted_text_preview(
entry: &entry::Entry,
reader: BufReader<File>,
lines: Vec<&str>,
syntax_set: &SyntaxSet,
syntax_theme: &Theme,
) -> Arc<Preview> {
@ -184,9 +185,8 @@ fn compute_highlighted_text_preview(
"Computing highlights in the background for {:?}",
entry.name
);
let lines: Vec<String> = reader
.lines()
.map_while(Result::ok)
let lines: Vec<String> = lines
.iter()
// we need to add a newline here because sublime syntaxes expect one
// to be present at the end of each line
.map(|line| preprocess_line(&line).0 + "\n")

View File

@ -561,6 +561,18 @@ impl Television {
{
// preview content
let maybe_preview = self.previewer.preview(&selected_entry);
//// preload the next 3 previews
//if let Some(i) = self.results_picker.selected() {
// for j in 1..=5 {
// if let Some(entry) =
// self.channel.get_result((i + j).try_into().unwrap())
// {
// let _ = self.previewer.preview(&entry);
// }
// }
//}
let _ = self.previewer.preview(&selected_entry);
if let Some(preview) = &maybe_preview {
self.current_preview_total_lines = preview.total_lines();