aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock31
-rw-r--r--Cargo.toml1
-rw-r--r--src/defer.rs16
-rw-r--r--src/export_fun.rs22
-rw-r--r--src/main.rs6
5 files changed, 60 insertions, 16 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1c8a615..5a64a8f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,12 @@
version = 4
[[package]]
+name = "bitflags"
+version = "2.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
+
+[[package]]
name = "cc"
version = "1.2.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -13,6 +19,18 @@ dependencies = [
]
[[package]]
+name = "cfg-if"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
+
+[[package]]
+name = "cfg_aliases"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
+
+[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -25,10 +43,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
[[package]]
+name = "nix"
+version = "0.31.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5d6d0705320c1e6ba1d912b5e37cf18071b6c2e9b7fa8215a1e8a7651966f5d3"
+dependencies = [
+ "bitflags",
+ "cfg-if",
+ "cfg_aliases",
+ "libc",
+]
+
+[[package]]
name = "pish"
version = "0.3.0"
dependencies = [
"libc",
+ "nix",
"pish_derive",
"sqlite",
"termios",
diff --git a/Cargo.toml b/Cargo.toml
index 3707d2a..0557e81 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,3 +10,4 @@ libc = "0.2.182"
sqlite = "0.37.0"
termios = "0.3"
pish_derive = { path = "./pish_derive" }
+nix = { version = "0.31.2", features = ["poll"] }
diff --git a/src/defer.rs b/src/defer.rs
new file mode 100644
index 0000000..362c7d1
--- /dev/null
+++ b/src/defer.rs
@@ -0,0 +1,16 @@
+pub struct Defer<T: FnOnce()>(pub Option<T>);
+
+impl<T: FnOnce()> Drop for Defer<T> {
+ fn drop(&mut self) {
+ if let Some(f) = self.0.take() {
+ f();
+ }
+ }
+}
+
+#[macro_export]
+macro_rules! defer {
+ ($($x:tt)*) => {
+ let _defer = crate::defer::Defer(Some(move || {$($x)*}));
+ };
+}
diff --git a/src/export_fun.rs b/src/export_fun.rs
index ecbdf46..a98699c 100644
--- a/src/export_fun.rs
+++ b/src/export_fun.rs
@@ -5,6 +5,7 @@ use nix::poll::PollFlags;
use nix::poll::poll;
use crate::Session;
+use crate::defer;
use crate::run::Input;
use crate::run::Output;
use crate::run::get_command_kind;
@@ -32,7 +33,6 @@ use std::sync::Arc;
use std::sync::Mutex;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
-use std::sync::mpsc::Sender;
use std::thread;
use std::time::Duration;
@@ -204,7 +204,9 @@ impl Drop for SocketDropper {
// wait 1s for background to exit
if let Err(e) = sr.recv.recv_timeout(Duration::from_secs(1)) {
- eprintln!("background thread is still running({e:?}, session might not be cleaned up\r");
+ eprintln!(
+ "background thread is still running({e:?}, session might not be cleaned up\r"
+ );
}
}
}
@@ -231,19 +233,9 @@ pub fn listen(session: Arc<Mutex<Session>>) -> impl Drop {
let se = session.clone();
thread::spawn(move || {
- struct SessionRemover {
- send: Sender<()>,
- path: PathBuf,
- }
- impl Drop for SessionRemover {
- fn drop(&mut self) {
- let _ = fs::remove_dir_all(&self.path);
- let _ = self.send.send(());
- }
- }
- let _session_remover = SessionRemover {
- path: session_dir,
- send,
+ defer! {
+ let _ = fs::remove_dir_all(session_dir);
+ let _ = send.send(());
};
let listener = UnixListener::bind(socket_path).unwrap();
diff --git a/src/main.rs b/src/main.rs
index 17c4ba0..d822a03 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,6 +16,7 @@ pub mod basedir;
pub mod completion;
pub mod cursor;
pub mod date;
+pub mod defer;
pub mod export_fun;
pub mod history;
pub mod linebuf;
@@ -350,7 +351,10 @@ fn event_loop() {
b'\t' => {
let cmd = se.line.into_bytes();
drop(se);
- let comp = parse::completion_context(&cmd, &mut Executor::new_for_completion(session.clone()));
+ let comp = parse::completion_context(
+ &cmd,
+ &mut Executor::new_for_completion(session.clone()),
+ );
let mut se = session.lock().unwrap();
match comp.kind {
parse::CompletionKind::Command => todo!(),