diff options
author | rabite <rabite@posteo.de> | 2019-02-04 18:51:07 +0100 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-02-04 23:06:59 +0100 |
commit | 9cd395d8f77134b6c041739a8f89d3d18a3f6a7e (patch) | |
tree | 17205b52fbe51cfc0e9cf26da3904a324da77035 /src/preview.rs | |
parent | 1b95fb704f4a1d4674776df638a1a2158144580d (diff) |
prototype async previews
Diffstat (limited to 'src/preview.rs')
-rw-r--r-- | src/preview.rs | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/src/preview.rs b/src/preview.rs index bbada42..8245c34 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -1,7 +1,4 @@ -use rayon as rayon; - -use std::io::{stdout, Write}; -use std::sync::atomic::AtomicUsize; +use std::io::Write; use std::sync::Mutex; use crate::coordinates::{Coordinates, Position, Size}; @@ -10,11 +7,23 @@ use crate::listview::ListView; use crate::textview::TextView; use crate::widget::Widget; + +pub struct InstanceCounter(Mutex<usize>); +impl PartialEq for InstanceCounter { + fn eq(&self, other: &Self) -> bool { + let instance = self.0.lock().unwrap(); + let other = other.0.lock().unwrap(); + *instance == *other + } +} + + +#[derive(PartialEq)] pub struct Previewer { pub file: Option<File>, pub buffer: String, pub coordinates: Coordinates, - pub instances: Mutex<usize> + pub instances: InstanceCounter } impl Previewer { @@ -23,17 +32,17 @@ impl Previewer { file: None, buffer: String::new(), coordinates: Coordinates::new(), - instances: From::from(0) + instances: InstanceCounter(Mutex::new(0)) } } pub fn set_file(&mut self, file: &File) { //return; - let mut instance = self.instances.try_lock().unwrap(); + let mut instance = self.instances.0.try_lock().unwrap(); if *instance > 2 { return } *instance = *instance + 1; let coordinates = self.coordinates.clone(); let file = file.clone(); - + let redraw = crate::term::reset() + &self.get_redraw_empty_list(0); //self.threads.install(|| { @@ -66,6 +75,33 @@ impl Previewer { let output = textview.get_drawlist() + &textview.get_redraw_empty_list(len - 1); write!(std::io::stdout(), "{}", output).unwrap(); + } else { + let output = + std::process::Command::new("scope.sh").arg(&file.name) + .arg("10".to_string()) + .arg("10".to_string()) + .arg("".to_string()) + .arg("false".to_string()) + .output().unwrap(); + + + if output.status.code().unwrap() == 5 { + write!(std::io::stdout(), "{}", redraw).unwrap(); + } else { + let output = std::str::from_utf8(&output.stdout) + .unwrap() + .to_string(); + let mut textview = TextView { + lines: output.lines().map(|s| s.to_string()).collect(), + buffer: String::new(), + coordinates: Coordinates::new() }; + textview.set_coordinates(&coordinates); + textview.refresh(); + let len = textview.lines.len(); + let output = textview.get_drawlist() + + &textview.get_redraw_empty_list(len - 1); + write!(std::io::stdout(), "{}", output).unwrap(); + } } } |