Move pipe writing logic from runner to app

Ref: https://github.com/sayanarijit/xplr/issues/103
pull/109/head
Arijit Basu 3 years ago committed by Arijit Basu
parent eca35c73a2
commit 33e500a16d

@ -8,21 +8,26 @@ fn criterion_benchmark(c: &mut Criterion) {
fs::File::create(format!("/tmp/xplr_bench/{}", i)).unwrap(); fs::File::create(format!("/tmp/xplr_bench/{}", i)).unwrap();
}); });
let app = app::App::create("/tmp/xplr_bench".into()) let mut app = app::App::create("/tmp/xplr_bench".into()).expect("failed to create app");
.expect("failed to create app")
.handle_task(app::Task::new( app = app
app::MsgIn::External(app::ExternalMsg::ChangeDirectory("/tmp/xplr_bench".into())), .clone()
None, .handle_task(
)) app::Task::new(
app::MsgIn::External(app::ExternalMsg::ChangeDirectory("/tmp/xplr_bench".into())),
None,
),
&app.clone(),
)
.unwrap(); .unwrap();
c.bench_function("focus next item", |b| { c.bench_function("focus next item", |b| {
b.iter(|| { b.iter(|| {
app.clone() app.clone()
.handle_task(app::Task::new( .handle_task(
app::MsgIn::External(app::ExternalMsg::FocusNext), app::Task::new(app::MsgIn::External(app::ExternalMsg::FocusNext), None),
None, &app.clone(),
)) )
.unwrap() .unwrap()
}) })
}); });
@ -30,10 +35,10 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("focus previous item", |b| { c.bench_function("focus previous item", |b| {
b.iter(|| { b.iter(|| {
app.clone() app.clone()
.handle_task(app::Task::new( .handle_task(
app::MsgIn::External(app::ExternalMsg::FocusPrevious), app::Task::new(app::MsgIn::External(app::ExternalMsg::FocusPrevious), None),
None, &app.clone(),
)) )
.unwrap() .unwrap()
}) })
}); });
@ -41,10 +46,10 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("focus first item", |b| { c.bench_function("focus first item", |b| {
b.iter(|| { b.iter(|| {
app.clone() app.clone()
.handle_task(app::Task::new( .handle_task(
app::MsgIn::External(app::ExternalMsg::FocusFirst), app::Task::new(app::MsgIn::External(app::ExternalMsg::FocusFirst), None),
None, &app.clone(),
)) )
.unwrap() .unwrap()
}) })
}); });
@ -52,10 +57,10 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("focus last item", |b| { c.bench_function("focus last item", |b| {
b.iter(|| { b.iter(|| {
app.clone() app.clone()
.handle_task(app::Task::new( .handle_task(
app::MsgIn::External(app::ExternalMsg::FocusLast), app::Task::new(app::MsgIn::External(app::ExternalMsg::FocusLast), None),
None, &app.clone(),
)) )
.unwrap() .unwrap()
}) })
}); });
@ -63,15 +68,15 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("leave and enter directory", |b| { c.bench_function("leave and enter directory", |b| {
b.iter(|| { b.iter(|| {
app.clone() app.clone()
.handle_task(app::Task::new( .handle_task(
app::MsgIn::External(app::ExternalMsg::Back), app::Task::new(app::MsgIn::External(app::ExternalMsg::Back), None),
None, &app.clone(),
)) )
.unwrap() .unwrap()
.handle_task(app::Task::new( .handle_task(
app::MsgIn::External(app::ExternalMsg::Enter), app::Task::new(app::MsgIn::External(app::ExternalMsg::Enter), None),
None, &app.clone(),
)) )
.unwrap() .unwrap()
}) })
}); });

@ -11,6 +11,7 @@ use std::collections::VecDeque;
use std::env; use std::env;
use std::fs; use std::fs;
use std::io; use std::io;
use std::io::prelude::*;
use std::path::PathBuf; use std::path::PathBuf;
pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW"; pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW";
@ -1269,6 +1270,8 @@ impl App {
)); ));
} }
fs::write(&app.pipe().global_help_menu_out, app.global_help_menu_str())?;
Ok(app) Ok(app)
} }
@ -1287,10 +1290,10 @@ impl App {
self self
} }
pub fn handle_task(self, task: Task) -> Result<Self> { pub fn handle_task(self, task: Task, last_app: &Self) -> Result<Self> {
match task.msg { match task.msg {
MsgIn::Internal(msg) => self.handle_internal(msg), MsgIn::Internal(msg) => self.handle_internal(msg),
MsgIn::External(msg) => self.handle_external(msg, task.key), MsgIn::External(msg) => self.handle_external(msg, task.key, last_app),
} }
} }
@ -1301,7 +1304,7 @@ impl App {
} }
} }
fn handle_external(self, msg: ExternalMsg, key: Option<Key>) -> Result<Self> { fn handle_external(self, msg: ExternalMsg, key: Option<Key>, last_app: &Self) -> Result<Self> {
if self.config().general.read_only.unwrap_or_default() && !msg.is_read_only() { 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()) self.log_error("Cannot call shell command in read-only mode.".into())
} else { } else {
@ -1383,7 +1386,9 @@ impl App {
ExternalMsg::Debug(path) => self.debug(path), ExternalMsg::Debug(path) => self.debug(path),
ExternalMsg::Terminate => bail!(""), ExternalMsg::Terminate => bail!(""),
} }
} }?
.refresh_selection()
.write_pipes(&last_app)
} }
fn handle_key(mut self, key: Key) -> Result<Self> { fn handle_key(mut self, key: Key) -> Result<Self> {
@ -2018,13 +2023,10 @@ impl App {
&self.session_path &self.session_path
} }
pub fn refresh_selection(mut self) -> Result<Self> { fn refresh_selection(mut self) -> Self {
self.selection = self self.selection
.selection .retain(|n| PathBuf::from(&n.absolute_path).exists());
.into_iter() self
.filter(|n| PathBuf::from(&n.absolute_path).exists())
.collect();
Ok(self)
} }
pub fn result(&self) -> Vec<&Node> { pub fn result(&self) -> Vec<&Node> {
@ -2148,4 +2150,47 @@ impl App {
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("") .join("")
} }
pub fn write_pipes(self, last_app: &Self) -> Result<Self> {
if self.focused_node() != last_app.focused_node() {
fs::write(&self.pipe().focus_out, self.focused_node_str())?;
};
if self.selection() != last_app.selection() {
fs::write(&self.pipe().selection_out, self.selection_str())?;
};
if self.history_str() != last_app.history_str() {
fs::write(&self.pipe().history_out, self.history_str())?;
};
if self.mode_str() != last_app.mode_str() {
fs::write(&self.pipe().mode_out, self.mode_str())?;
};
if self.directory_buffer() != last_app.directory_buffer() {
fs::write(&self.pipe().directory_nodes_out, self.directory_nodes_str())?;
};
if self.logs().len() != last_app.logs().len() {
let new_logs = self
.logs()
.iter()
.skip(last_app.logs().len())
.map(|l| format!("{}\n", l))
.collect::<Vec<String>>()
.join("");
let mut file = fs::OpenOptions::new()
.append(true)
.open(&self.pipe().logs_out)?;
file.write_all(new_logs.as_bytes())?;
};
if self.result() != last_app.result() {
fs::write(&self.pipe().result_out, self.result_str())?;
};
Ok(self)
}
} }

@ -78,8 +78,6 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
let (tx_event_reader, rx_event_reader) = mpsc::channel(); let (tx_event_reader, rx_event_reader) = mpsc::channel();
let (tx_pwd_watcher, rx_pwd_watcher) = mpsc::channel(); let (tx_pwd_watcher, rx_pwd_watcher) = mpsc::channel();
fs::write(&app.pipe().global_help_menu_out, app.global_help_menu_str())?;
explorer::explore( explorer::explore(
app.explorer_config().clone(), app.explorer_config().clone(),
app.pwd().clone(), app.pwd().clone(),
@ -124,7 +122,7 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
'outer: for task in rx_msg_in { 'outer: for task in rx_msg_in {
let last_app = app.clone(); let last_app = app.clone();
let (new_app, new_result) = match app.handle_task(task) { let (new_app, new_result) = match app.handle_task(task, &last_app) {
Ok(a) => (a, Ok(None)), Ok(a) => (a, Ok(None)),
Err(err) => (last_app.clone(), Err(err)), Err(err) => (last_app.clone(), Err(err)),
}; };
@ -176,7 +174,6 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
} }
app::MsgOut::Refresh => { app::MsgOut::Refresh => {
app = app.refresh_selection()?;
if app.pwd() != last_app.pwd() { if app.pwd() != last_app.pwd() {
tx_pwd_watcher.send(app.pwd().clone())?; tx_pwd_watcher.send(app.pwd().clone())?;
explorer::explore( explorer::explore(
@ -242,46 +239,6 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
} }
}; };
} }
if app.focused_node() != last_app.focused_node() {
fs::write(&app.pipe().focus_out, app.focused_node_str())?;
};
if app.selection() != last_app.selection() {
fs::write(&app.pipe().selection_out, app.selection_str())?;
};
if app.history_str() != last_app.history_str() {
fs::write(&app.pipe().history_out, app.history_str())?;
};
if app.mode_str() != last_app.mode_str() {
fs::write(&app.pipe().mode_out, app.mode_str())?;
};
if app.directory_buffer() != last_app.directory_buffer() {
fs::write(&app.pipe().directory_nodes_out, app.directory_nodes_str())?;
};
if app.logs().len() != last_app.logs().len() {
let new_logs = app
.logs()
.iter()
.skip(last_app.logs().len())
.map(|l| format!("{}\n", l))
.collect::<Vec<String>>()
.join("");
let mut file = fs::OpenOptions::new()
.append(true)
.open(&app.pipe().logs_out)?;
file.write_all(new_logs.as_bytes())?;
};
if app.result() != last_app.result() {
fs::write(&app.pipe().result_out, app.result_str())?;
};
} }
terminal.clear()?; terminal.clear()?;

Loading…
Cancel
Save