Fix MacOS error directory not empty

With this change, xplr will delete the pipe files when command execution
is over.
pull/170/head
Arijit Basu 3 years ago committed by Arijit Basu
parent 003e90a7d1
commit e977aeb7d3

@ -29,8 +29,8 @@ fn to_humansize(size: u64) -> String {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pipe {
path: String,
msg_in: String,
focus_out: String,
selection_out: String,
result_out: String,
directory_nodes_out: String,
@ -41,44 +41,31 @@ pub struct Pipe {
impl Pipe {
fn from_session_path(path: &str) -> Result<Self> {
let pipesdir = PathBuf::from(path).join("pipe");
let path = PathBuf::from(path).join("pipe");
fs::create_dir_all(&pipesdir).unwrap();
let msg_in = path.join("msg_in").to_string_lossy().to_string();
let msg_in = pipesdir.join("msg_in").to_string_lossy().to_string();
let selection_out = path.join("selection_out").to_string_lossy().to_string();
let focus_out = pipesdir.join("focus_out").to_string_lossy().to_string();
let result_out = path.join("result_out").to_string_lossy().to_string();
let selection_out = pipesdir.join("selection_out").to_string_lossy().to_string();
let result_out = pipesdir.join("result_out").to_string_lossy().to_string();
let directory_nodes_out = pipesdir
let directory_nodes_out = path
.join("directory_nodes_out")
.to_string_lossy()
.to_string();
let global_help_menu_out = pipesdir
let global_help_menu_out = path
.join("global_help_menu_out")
.to_string_lossy()
.to_string();
let logs_out = pipesdir.join("logs_out").to_string_lossy().to_string();
let logs_out = path.join("logs_out").to_string_lossy().to_string();
let history_out = pipesdir.join("history_out").to_string_lossy().to_string();
fs::write(&msg_in, "")?;
fs::write(&focus_out, "")?;
fs::write(&selection_out, "")?;
fs::write(&directory_nodes_out, "")?;
fs::write(&global_help_menu_out, "")?;
fs::write(&result_out, "")?;
fs::write(&logs_out, "")?;
fs::write(&history_out, "")?;
let history_out = path.join("history_out").to_string_lossy().to_string();
Ok(Self {
path: path.to_string_lossy().to_string(),
msg_in,
focus_out,
selection_out,
result_out,
directory_nodes_out,
@ -122,6 +109,11 @@ impl Pipe {
pub fn history_out(&self) -> &String {
&self.history_out
}
/// Get a reference to the pipe's path.
pub fn path(&self) -> &String {
&self.path
}
}
#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
@ -1616,7 +1608,6 @@ impl App {
// ));
// }
app.write_pipes()?;
Ok(app)
}
@ -2702,13 +2693,9 @@ impl App {
pub fn write_pipes(&self) -> Result<()> {
// TODO optimize and test
let focused_node_str = self.focused_node_str();
// if last_app
// .map(|a| a.focused_node_str() != focused_node_str)
// .unwrap_or(true)
// {
fs::write(&self.pipe().focus_out, focused_node_str)?;
// };
fs::create_dir_all(self.pipe().path())?;
fs::write(&self.pipe().msg_in, "")?;
let selection_str = self.selection_str();
// if last_app
@ -2771,6 +2758,23 @@ impl App {
Ok(())
}
pub fn cleanup_pipes(&self) -> Result<()> {
while !fs::read_to_string(self.pipe().msg_in())?.is_empty() {
std::thread::sleep(std::time::Duration::from_millis(1));
}
fs::remove_file(self.pipe().msg_in())?;
fs::remove_file(self.pipe().selection_out())?;
fs::remove_file(self.pipe().result_out())?;
fs::remove_file(self.pipe().directory_nodes_out())?;
fs::remove_file(self.pipe().global_help_menu_out())?;
fs::remove_file(self.pipe().logs_out())?;
fs::remove_file(self.pipe().history_out())?;
fs::remove_dir(self.pipe().path())?;
Ok(())
}
/// Get a reference to the app's layout.
pub fn layout(&self) -> &Layout {
&self.layout

@ -9,17 +9,21 @@ use std::time::Duration;
pub fn keep_reading(pipe: String, tx: Sender<Task>) {
let mut last_modified = None;
thread::spawn(move || loop {
let modified = PathBuf::from(&pipe)
.metadata()
.and_then(|m| m.modified())
.ok();
let path = PathBuf::from(&pipe);
if !path.exists() {
thread::sleep(Duration::from_millis(50));
continue;
}
let modified = path.metadata().and_then(|m| m.modified()).ok();
if modified == last_modified {
thread::sleep(Duration::from_millis(50));
} else if let Ok(mut file) = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.create(false)
.open(&pipe)
{
let mut in_str = String::new();

@ -67,6 +67,8 @@ pub fn run(
focused_path: Option<String>,
lua: mlua::Lua,
) -> Result<Option<String>> {
fs::create_dir_all(app.session_path())?;
let (tx_msg_in, rx_msg_in) = mpsc::channel();
let (tx_event_reader, rx_event_reader) = mpsc::channel();
let (tx_pwd_watcher, rx_pwd_watcher) = mpsc::channel();
@ -181,6 +183,7 @@ pub fn run(
}
})
.unwrap_or_else(|e| Err(e.to_string()));
app.cleanup_pipes()?;
if let Err(e) = status {
app = app.log_error(e.to_string())?;
@ -210,6 +213,7 @@ pub fn run(
}
})
.unwrap_or_else(|e| Err(e.to_string()));
app.cleanup_pipes()?;
if let Err(e) = status {
app = app.log_error(e.to_string())?;
@ -241,7 +245,7 @@ pub fn run(
term::disable_raw_mode()?;
terminal.show_cursor()?;
fs::remove_dir_all(session_path)?;
fs::remove_dir(session_path)?;
result
}

Loading…
Cancel
Save