diff options
author | rabite <rabite@posteo.de> | 2019-02-12 22:55:16 +0100 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-02-12 22:55:16 +0100 |
commit | 56d9c3521599baef6ff1472c0c95fa07f17a5a67 (patch) | |
tree | 48f16edb6cc49a71bd9d438072f7c2d4848c3ac7 /src/preview.rs | |
parent | 1c500d91cf7389850e9f32b14d8ae210a7e62888 (diff) |
async widget
Diffstat (limited to 'src/preview.rs')
-rw-r--r-- | src/preview.rs | 159 |
1 files changed, 148 insertions, 11 deletions
diff --git a/src/preview.rs b/src/preview.rs index d6bf31d..f6bad98 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -7,6 +7,7 @@ use crate::files::{File, Files, Kind}; use crate::listview::ListView; use crate::textview::TextView; use crate::widget::Widget; +use crate::async_widget::AsyncPlug; lazy_static! { @@ -23,10 +24,130 @@ fn kill_procs() { } fn is_current(file: &File) -> bool { - CURFILE.lock().unwrap().as_ref().unwrap() == file + true + //CURFILE.lock().unwrap().as_ref().unwrap() == file } +#[derive(PartialEq)] +pub struct AsyncPreviewer { + pub file: Option<File>, + pub buffer: String, + pub coordinates: Coordinates, + pub async_plug: AsyncPlug +} + +impl AsyncPreviewer { + pub fn new() -> AsyncPreviewer { + let textview = crate::textview::TextView { + lines: vec![], + buffer: "".to_string(), + coordinates: Coordinates::new(), + }; + let textview = Box::new(textview); + + AsyncPreviewer { + file: None, + buffer: String::new(), + coordinates: Coordinates::new(), + async_plug: AsyncPlug::new(textview) + } + } + pub fn set_file(&mut self, file: &File) { + let coordinates = self.coordinates.clone(); + let file = file.clone(); + let redraw = crate::term::reset() + &self.get_redraw_empty_list(0); + + self.async_plug.replace_widget(Box::new(move || { + kill_procs(); + let mut bufout = std::io::BufWriter::new(std::io::stdout()); + match &file.kind { + Kind::Directory => match Files::new_from_path(&file.path) { + Ok(files) => { + //if !is_current(&file) { return } + let len = files.len(); + //if len == 0 { return }; + let mut file_list = ListView::new(files); + file_list.set_coordinates(&coordinates); + file_list.refresh(); + //if !is_current(&file) { return } + file_list.animate_slide_up(); + return Box::new(file_list) as Box<dyn Widget + Send>; + + } + Err(err) => { + write!(bufout, "{}", redraw).unwrap(); + let textview = crate::textview::TextView { + lines: vec![], + buffer: "".to_string(), + coordinates: Coordinates::new(), + }; + return Box::new(textview) as Box<dyn Widget + Send>; + }, + } + _ => { + if file.get_mime() == Some("text".to_string()) { + let mut textview = TextView::new_from_file(&file); + //if !is_current(&file) { return } + textview.set_coordinates(&coordinates); + textview.refresh(); + //if !is_current(&file) { return } + textview.animate_slide_up(); + return Box::new(textview); + } else { + let process = + std::process::Command::new("scope.sh") + .arg(&file.name) + .arg("10".to_string()) + .arg("10".to_string()) + .arg("".to_string()) + .arg("false".to_string()) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::null()) + .spawn().unwrap(); + + // let pid = process.id(); + // PIDS.lock().unwrap().push(pid); + //if !is_current(&file) { return } + + let output = process.wait_with_output(); + //if output.is_err() { return } + let output = output.unwrap(); + + let status = output.status.code(); + //if status.is_none() { return } + let status = status.unwrap(); + + if status == 0 || status == 5 && is_current(&file) { + 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(); + textview.animate_slide_up(); + return Box::new(textview); + } + + } + + write!(bufout, "{}", redraw).unwrap(); + //std::io::stdout().flush().unwrap(); + let textview = crate::textview::TextView { + lines: vec![], + buffer: "".to_string(), + coordinates: Coordinates::new(), + }; + return Box::new(textview); + } + } + })); + } +} #[derive(PartialEq)] pub struct Previewer { @@ -129,18 +250,32 @@ impl Previewer { } impl Widget for Previewer { - fn get_size(&self) -> &Size { - &self.coordinates.size + fn get_coordinates(&self) -> &Coordinates { + &self.coordinates } - fn set_size(&mut self, size: Size) { - self.coordinates.size = size; + fn set_coordinates(&mut self, coordinates: &Coordinates) { + if self.coordinates == *coordinates { + return; + } + self.coordinates = coordinates.clone(); + self.refresh(); } - fn get_position(&self) -> &Position { - &self.coordinates.position + fn render_header(&self) -> String { + "".to_string() } - fn set_position(&mut self, pos: Position) { - self.coordinates.position = pos; + fn refresh(&mut self) { + let file = self.file.clone(); + if let Some(file) = file { + self.set_file(&file); + } + } + fn get_drawlist(&self) -> String { + self.buffer.clone() } +} + + +impl Widget for AsyncPreviewer { fn get_coordinates(&self) -> &Coordinates { &self.coordinates } @@ -149,7 +284,8 @@ impl Widget for Previewer { return; } self.coordinates = coordinates.clone(); - self.refresh(); + self.async_plug.set_coordinates(&coordinates.clone()); + self.async_plug.refresh(); } fn render_header(&self) -> String { "".to_string() @@ -161,6 +297,7 @@ impl Widget for Previewer { } } fn get_drawlist(&self) -> String { - self.buffer.clone() + self.async_plug.get_drawlist(); + "".to_string() } } |