Remove per directory buffer

Closes: https://github.com/sayanarijit/xplr/issues/289
pull/314/head
Arijit Basu 3 years ago committed by Arijit Basu
parent 7d0605479e
commit 35c18a25dc

@ -4,9 +4,9 @@ use criterion::{criterion_group, criterion_main, Criterion};
use crossterm::execute; use crossterm::execute;
use crossterm::terminal as term; use crossterm::terminal as term;
use std::fs; use std::fs;
use termion::get_tty;
use tui::backend::CrosstermBackend; use tui::backend::CrosstermBackend;
use tui::Terminal; use tui::Terminal;
use xplr::runner::get_tty;
use xplr::*; use xplr::*;
const PWD: &str = "/tmp/xplr_bench"; const PWD: &str = "/tmp/xplr_bench";

@ -419,7 +419,8 @@ impl DirectoryBuffer {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum InternalMsg { pub enum InternalMsg {
AddDirectory(String, DirectoryBuffer), AddLastFocus(String, Option<String>),
SetDirectory(DirectoryBuffer),
HandleKey(Key), HandleKey(Key),
} }
@ -1517,7 +1518,8 @@ pub struct App {
pub version: String, pub version: String,
pub config: Config, pub config: Config,
pub pwd: String, pub pwd: String,
pub directory_buffers: HashMap<String, DirectoryBuffer>, pub directory_buffer: Option<DirectoryBuffer>,
pub last_focus: HashMap<String, Option<String>>,
pub selection: IndexSet<Node>, pub selection: IndexSet<Node>,
pub msg_out: VecDeque<MsgOut>, pub msg_out: VecDeque<MsgOut>,
pub mode: Mode, pub mode: Mode,
@ -1608,7 +1610,8 @@ impl App {
version: VERSION.to_string(), version: VERSION.to_string(),
config, config,
pwd, pwd,
directory_buffers: Default::default(), directory_buffer: Default::default(),
last_focus: Default::default(),
selection: Default::default(), selection: Default::default(),
msg_out: Default::default(), msg_out: Default::default(),
mode, mode,
@ -1658,7 +1661,10 @@ impl App {
fn handle_internal(self, msg: InternalMsg) -> Result<Self> { fn handle_internal(self, msg: InternalMsg) -> Result<Self> {
match msg { 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), InternalMsg::HandleKey(key) => self.handle_key(key),
} }
} }
@ -1812,14 +1818,17 @@ impl App {
Ok(self) Ok(self)
} }
pub fn explore_pwd(self) -> Result<Self> { pub fn explore_pwd(mut self) -> Result<Self> {
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( let dir = explorer::explore_sync(
self.explorer_config().clone(), self.explorer_config().clone(),
self.pwd().into(), 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.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<Self> { fn explore_pwd_async(mut self) -> Result<Self> {
@ -1965,6 +1974,9 @@ impl App {
match env::set_current_dir(&dir) { match env::set_current_dir(&dir) {
Ok(()) => { 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(); self.pwd = dir.to_string_lossy().to_string();
if save_history { if save_history {
self.history = self.history.push(format!("{}/", self.pwd)); 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> { pub fn set_directory(mut self, dir: DirectoryBuffer) -> Result<Self> {
self.directory_buffers.insert(parent, dir); 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<String>) -> Result<Self> {
self.last_focus.insert(parent, focused_path);
Ok(self) Ok(self)
} }
@ -2593,7 +2616,7 @@ impl App {
} }
fn directory_buffer_mut(&mut self) -> Option<&mut DirectoryBuffer> { 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. /// Get a reference to the app's pwd.
@ -2603,7 +2626,7 @@ impl App {
/// Get a reference to the app's current directory buffer. /// Get a reference to the app's current directory buffer.
pub fn directory_buffer(&self) -> Option<&DirectoryBuffer> { 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. /// Get a reference to the app's config.
@ -2629,11 +2652,6 @@ impl App {
format!("{}\n", &self.mode.name()) format!("{}\n", &self.mode.name())
} }
/// Get a reference to the app's directory buffers.
pub fn directory_buffers(&self) -> &HashMap<String, DirectoryBuffer> {
&self.directory_buffers
}
/// Get a reference to the app's input buffer. /// Get a reference to the app's input buffer.
pub fn input_buffer(&self) -> Option<String> { pub fn input_buffer(&self) -> Option<String> {
self.input_buffer.clone() self.input_buffer.clone()

@ -36,7 +36,7 @@ pub(crate) fn explore_sync(
.map(|(i, _)| i) .map(|(i, _)| i)
.unwrap_or_else(|| fallback_focus.min(nodes.len().max(1) - 1)) .unwrap_or_else(|| fallback_focus.min(nodes.len().max(1) - 1))
} else { } else {
fallback_focus.min(nodes.len().max(1) - 1) 0
}; };
Ok(DirectoryBuffer::new( Ok(DirectoryBuffer::new(
@ -58,10 +58,7 @@ pub(crate) fn explore_async(
.map(|buf| { .map(|buf| {
tx_msg_in tx_msg_in
.send(Task::new( .send(Task::new(
MsgIn::Internal(InternalMsg::AddDirectory( MsgIn::Internal(InternalMsg::SetDirectory(buf)),
parent.to_string_lossy().to_string(),
buf,
)),
None, None,
)) ))
.unwrap_or_default(); .unwrap_or_default();

@ -21,7 +21,7 @@ use std::sync::mpsc;
use tui::backend::CrosstermBackend; use tui::backend::CrosstermBackend;
use tui::Terminal; use tui::Terminal;
fn get_tty() -> io::Result<fs::File> { pub fn get_tty() -> io::Result<fs::File> {
fs::OpenOptions::new() fs::OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)

Loading…
Cancel
Save