diff --git a/benches/criterion.rs b/benches/criterion.rs index fb49f9d..93a36d6 100644 --- a/benches/criterion.rs +++ b/benches/criterion.rs @@ -4,9 +4,9 @@ use criterion::{criterion_group, criterion_main, Criterion}; use crossterm::execute; use crossterm::terminal as term; use std::fs; -use termion::get_tty; use tui::backend::CrosstermBackend; use tui::Terminal; +use xplr::runner::get_tty; use xplr::*; const PWD: &str = "/tmp/xplr_bench"; diff --git a/src/app.rs b/src/app.rs index f0bd439..00605f3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -419,7 +419,8 @@ impl DirectoryBuffer { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub enum InternalMsg { - AddDirectory(String, DirectoryBuffer), + AddLastFocus(String, Option), + SetDirectory(DirectoryBuffer), HandleKey(Key), } @@ -1517,7 +1518,8 @@ pub struct App { pub version: String, pub config: Config, pub pwd: String, - pub directory_buffers: HashMap, + pub directory_buffer: Option, + pub last_focus: HashMap>, pub selection: IndexSet, pub msg_out: VecDeque, pub mode: Mode, @@ -1608,7 +1610,8 @@ impl App { version: VERSION.to_string(), config, pwd, - directory_buffers: Default::default(), + directory_buffer: Default::default(), + last_focus: Default::default(), selection: Default::default(), msg_out: Default::default(), mode, @@ -1658,7 +1661,10 @@ impl App { fn handle_internal(self, msg: InternalMsg) -> Result { match msg { - InternalMsg::AddDirectory(parent, dir) => self.add_directory(parent, dir), + InternalMsg::SetDirectory(dir) => self.set_directory(dir), + InternalMsg::AddLastFocus(parent, focus_path) => { + self.add_last_focus(parent, focus_path) + } InternalMsg::HandleKey(key) => self.handle_key(key), } } @@ -1812,14 +1818,17 @@ impl App { Ok(self) } - pub fn explore_pwd(self) -> Result { + pub fn explore_pwd(mut self) -> Result { + let focus = self.last_focus.get(self.pwd()).cloned().unwrap_or(None); + let pwd = self.pwd().clone(); + self = self.add_last_focus(pwd, focus.clone())?; let dir = explorer::explore_sync( self.explorer_config().clone(), self.pwd().into(), - self.focused_node().map(|n| n.absolute_path().into()), + focus.map(PathBuf::from), self.directory_buffer().map(|d| d.focus()).unwrap_or(0), )?; - self.add_directory(dir.parent().clone(), dir) + self.set_directory(dir) } fn explore_pwd_async(mut self) -> Result { @@ -1965,6 +1974,9 @@ impl App { match env::set_current_dir(&dir) { Ok(()) => { + let pwd = self.pwd().clone(); + let focus = self.focused_node().map(|n| n.relative_path().clone()); + self = self.add_last_focus(pwd, focus)?; self.pwd = dir.to_string_lossy().to_string(); if save_history { self.history = self.history.push(format!("{}/", self.pwd)); @@ -2281,8 +2293,19 @@ impl App { }) } - pub fn add_directory(mut self, parent: String, dir: DirectoryBuffer) -> Result { - self.directory_buffers.insert(parent, dir); + pub fn set_directory(mut self, dir: DirectoryBuffer) -> Result { + self = self.add_last_focus( + dir.parent.clone(), + dir.focused_node().map(|n| n.relative_path().clone()), + )?; + if dir.parent == self.pwd { + self.directory_buffer = Some(dir); + } + Ok(self) + } + + pub fn add_last_focus(mut self, parent: String, focused_path: Option) -> Result { + self.last_focus.insert(parent, focused_path); Ok(self) } @@ -2593,7 +2616,7 @@ impl App { } fn directory_buffer_mut(&mut self) -> Option<&mut DirectoryBuffer> { - self.directory_buffers.get_mut(&self.pwd) + self.directory_buffer.as_mut() } /// Get a reference to the app's pwd. @@ -2603,7 +2626,7 @@ impl App { /// Get a reference to the app's current directory buffer. pub fn directory_buffer(&self) -> Option<&DirectoryBuffer> { - self.directory_buffers.get(&self.pwd) + self.directory_buffer.as_ref() } /// Get a reference to the app's config. @@ -2629,11 +2652,6 @@ impl App { format!("{}\n", &self.mode.name()) } - /// Get a reference to the app's directory buffers. - pub fn directory_buffers(&self) -> &HashMap { - &self.directory_buffers - } - /// Get a reference to the app's input buffer. pub fn input_buffer(&self) -> Option { self.input_buffer.clone() diff --git a/src/explorer.rs b/src/explorer.rs index e7df53b..40f3627 100644 --- a/src/explorer.rs +++ b/src/explorer.rs @@ -36,7 +36,7 @@ pub(crate) fn explore_sync( .map(|(i, _)| i) .unwrap_or_else(|| fallback_focus.min(nodes.len().max(1) - 1)) } else { - fallback_focus.min(nodes.len().max(1) - 1) + 0 }; Ok(DirectoryBuffer::new( @@ -58,10 +58,7 @@ pub(crate) fn explore_async( .map(|buf| { tx_msg_in .send(Task::new( - MsgIn::Internal(InternalMsg::AddDirectory( - parent.to_string_lossy().to_string(), - buf, - )), + MsgIn::Internal(InternalMsg::SetDirectory(buf)), None, )) .unwrap_or_default(); diff --git a/src/runner.rs b/src/runner.rs index ce74975..9058fa7 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -21,7 +21,7 @@ use std::sync::mpsc; use tui::backend::CrosstermBackend; use tui::Terminal; -fn get_tty() -> io::Result { +pub fn get_tty() -> io::Result { fs::OpenOptions::new() .read(true) .write(true)