From 3aa349f6141975f96adb624ee2f9e5300e90d3aa Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Sat, 8 May 2021 13:01:19 +0530 Subject: [PATCH] Don't refresh pipes on every iteration From this commit, the app state will be written to the output pipes only when invoking a command. For auto refreshing pipes, we can brainstorm on `service`s concept. --- benches/navigation.rs | 59 +++++++++++++++++------------------- src/app.rs | 70 +++++++++++++++++++------------------------ src/runner.rs | 6 ++-- 3 files changed, 63 insertions(+), 72 deletions(-) diff --git a/benches/navigation.rs b/benches/navigation.rs index 4c3a054..0902bec 100644 --- a/benches/navigation.rs +++ b/benches/navigation.rs @@ -12,22 +12,19 @@ fn criterion_benchmark(c: &mut Criterion) { app = app .clone() - .handle_task( - app::Task::new( - app::MsgIn::External(app::ExternalMsg::ChangeDirectory("/tmp/xplr_bench".into())), - None, - ), - &app.clone(), - ) + .handle_task(app::Task::new( + app::MsgIn::External(app::ExternalMsg::ChangeDirectory("/tmp/xplr_bench".into())), + None, + )) .unwrap(); c.bench_function("focus next item", |b| { b.iter(|| { app.clone() - .handle_task( - app::Task::new(app::MsgIn::External(app::ExternalMsg::FocusNext), None), - &app.clone(), - ) + .handle_task(app::Task::new( + app::MsgIn::External(app::ExternalMsg::FocusNext), + None, + )) .unwrap() }) }); @@ -35,10 +32,10 @@ fn criterion_benchmark(c: &mut Criterion) { c.bench_function("focus previous item", |b| { b.iter(|| { app.clone() - .handle_task( - app::Task::new(app::MsgIn::External(app::ExternalMsg::FocusPrevious), None), - &app.clone(), - ) + .handle_task(app::Task::new( + app::MsgIn::External(app::ExternalMsg::FocusPrevious), + None, + )) .unwrap() }) }); @@ -46,10 +43,10 @@ fn criterion_benchmark(c: &mut Criterion) { c.bench_function("focus first item", |b| { b.iter(|| { app.clone() - .handle_task( - app::Task::new(app::MsgIn::External(app::ExternalMsg::FocusFirst), None), - &app.clone(), - ) + .handle_task(app::Task::new( + app::MsgIn::External(app::ExternalMsg::FocusFirst), + None, + )) .unwrap() }) }); @@ -57,10 +54,10 @@ fn criterion_benchmark(c: &mut Criterion) { c.bench_function("focus last item", |b| { b.iter(|| { app.clone() - .handle_task( - app::Task::new(app::MsgIn::External(app::ExternalMsg::FocusLast), None), - &app.clone(), - ) + .handle_task(app::Task::new( + app::MsgIn::External(app::ExternalMsg::FocusLast), + None, + )) .unwrap() }) }); @@ -68,15 +65,15 @@ fn criterion_benchmark(c: &mut Criterion) { c.bench_function("leave and enter directory", |b| { b.iter(|| { app.clone() - .handle_task( - app::Task::new(app::MsgIn::External(app::ExternalMsg::Back), None), - &app.clone(), - ) + .handle_task(app::Task::new( + app::MsgIn::External(app::ExternalMsg::Back), + None, + )) .unwrap() - .handle_task( - app::Task::new(app::MsgIn::External(app::ExternalMsg::Enter), None), - &app.clone(), - ) + .handle_task(app::Task::new( + app::MsgIn::External(app::ExternalMsg::Enter), + None, + )) .unwrap() }) }); diff --git a/src/app.rs b/src/app.rs index f60cff9..199d989 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,7 +12,6 @@ use std::collections::VecDeque; use std::env; use std::fs; use std::io; -use std::io::prelude::*; use std::path::PathBuf; pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW"; @@ -25,7 +24,6 @@ pub struct Pipe { focus_out: String, selection_out: String, result_out: String, - mode_out: String, directory_nodes_out: String, global_help_menu_out: String, logs_out: String, @@ -46,8 +44,6 @@ impl Pipe { let result_out = pipesdir.join("result_out").to_string_lossy().to_string(); - let mode_out = pipesdir.join("mode_out").to_string_lossy().to_string(); - let directory_nodes_out = pipesdir .join("directory_nodes_out") .to_string_lossy() @@ -65,7 +61,6 @@ impl Pipe { fs::write(&msg_in, "")?; fs::write(&focus_out, "")?; fs::write(&selection_out, "")?; - fs::write(&mode_out, "")?; fs::write(&directory_nodes_out, "")?; fs::write(&global_help_menu_out, "")?; fs::write(&result_out, "")?; @@ -77,7 +72,6 @@ impl Pipe { focus_out, selection_out, result_out, - mode_out, directory_nodes_out, global_help_menu_out, logs_out, @@ -105,11 +99,6 @@ impl Pipe { &self.result_out } - /// Get a reference to the pipe's mode out. - pub fn mode_out(&self) -> &String { - &self.mode_out - } - /// Get a reference to the pipe's directory nodes out. pub fn directory_nodes_out(&self) -> &String { &self.directory_nodes_out @@ -1542,8 +1531,8 @@ impl App { )); } - fs::write(&app.pipe().global_help_menu_out, app.global_help_menu_str())?; - app.write_pipes(None) + app.write_pipes()?; + Ok(app) } pub fn focused_node(&self) -> Option<&Node> { @@ -1561,10 +1550,10 @@ impl App { self } - pub fn handle_task(self, task: Task, last_app: &Self) -> Result { + pub fn handle_task(self, task: Task) -> Result { match task.msg { MsgIn::Internal(msg) => self.handle_internal(msg), - MsgIn::External(msg) => self.handle_external(msg, task.key, last_app), + MsgIn::External(msg) => self.handle_external(msg, task.key), } } @@ -1575,7 +1564,7 @@ impl App { } } - fn handle_external(self, msg: ExternalMsg, key: Option, last_app: &Self) -> Result { + fn handle_external(self, msg: ExternalMsg, key: Option) -> Result { if self.config().general().read_only().unwrap_or_default() && !msg.is_read_only() { self.log_error("Cannot call shell command in read-only mode.".into()) } else { @@ -1664,7 +1653,6 @@ impl App { } }? .refresh_selection() - .write_pipes(Some(&last_app)) } fn handle_key(mut self, key: Key) -> Result { @@ -2359,10 +2347,10 @@ impl App { &self.session_path } - fn refresh_selection(mut self) -> Self { + fn refresh_selection(mut self) -> Result { self.selection .retain(|n| PathBuf::from(&n.absolute_path).exists()); - self + Ok(self) } pub fn result(&self) -> Vec<&Node> { @@ -2411,6 +2399,14 @@ impl App { &self.logs } + pub fn logs_str(&self) -> String { + self.logs() + .iter() + .map(|l| format!("{}\n", l)) + .collect::>() + .join("") + } + pub fn global_help_menu_str(&self) -> String { let builtin = self.config().modes().builtin().clone(); let custom = self.config().modes().custom().clone(); @@ -2487,7 +2483,7 @@ impl App { .join("") } - fn write_pipes(self, last_app: Option<&Self>) -> Result { + pub fn write_pipes(&self) -> Result<()> { // TODO optimize and test let focused_node_str = self.focused_node_str(); @@ -2514,11 +2510,6 @@ impl App { fs::write(&self.pipe().history_out, history_str)?; // }; - let mode_str = self.mode_str(); - // if last_app.map(|a| a.mode_str() != mode_str).unwrap_or(true) { - fs::write(&self.pipe().mode_out, mode_str)?; - // }; - let directory_nodes_str = self.directory_nodes_str(); // if last_app // .map(|a| a.directory_nodes_str() != directory_nodes_str) @@ -2531,20 +2522,21 @@ impl App { // .map(|a| a.logs().len() != self.logs().len()) // .unwrap_or(true) // { - let new_logs = self - .logs() - .iter() - .skip(last_app.map(|a| a.logs().len()).unwrap_or(0)) - .map(|l| format!("{}\n", l)) - .collect::>() - .join(""); - - let mut file = fs::OpenOptions::new() - .append(true) - .open(&self.pipe().logs_out)?; - - file.write_all(new_logs.as_bytes())?; + // let new_logs = self + // .logs() + // .iter() + // .skip(last_app.map(|a| a.logs().len()).unwrap_or(0)) + // .map(|l| format!("{}\n", l)) + // .collect::>() + // .join(""); + + // let mut file = fs::OpenOptions::new() + // .append(true) + // .open(&self.pipe().logs_out)?; + // file.write_all(new_logs.as_bytes())?; // }; + let logs_str = self.logs_str(); + fs::write(&self.pipe().logs_out, logs_str)?; let result_str = self.result_str(); // if last_app @@ -2553,7 +2545,7 @@ impl App { // { fs::write(&self.pipe().result_out, result_str)?; // }; - Ok(self) + Ok(()) } /// Get a reference to the app's layout. diff --git a/src/runner.rs b/src/runner.rs index dadfb9b..caf910b 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -48,7 +48,7 @@ fn call(app: &app::App, cmd: app::Command, silent: bool) -> io::Result) -> Result (a, Ok(None)), Err(err) => (last_app.clone(), Err(err)), }; @@ -184,6 +184,7 @@ pub fn run(mut app: app::App, focused_path: Option) -> Result { tx_event_reader.send(true)?; + app.write_pipes()?; let status = call(&app, cmd, false) .map(|s| { if s.success() { @@ -210,6 +211,7 @@ pub fn run(mut app: app::App, focused_path: Option) -> Result