diff options
-rw-r--r-- | src/config.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 8 | ||||
-rw-r--r-- | src/tmux.rs | 15 |
3 files changed, 26 insertions, 1 deletions
diff --git a/src/config.rs b/src/config.rs index df31de4..c504590 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,6 +18,10 @@ pub struct Config { #[arg(short = 'T', long = "sessions")] pub tmux_sessions: bool, + /// Exclude currently attached tmux session from output + #[arg(long)] + pub exclude_attached: bool, + /// Include arbitrary directories in output /// /// Note: Directories added by this flag are still filtered and sorted based on supplied diff --git a/src/main.rs b/src/main.rs index 25a051e..a50d3a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use clap::Parser; use projectr::{config::Config, project::Projects, tmux::Tmux, Search}; fn main() -> Result<()> { - let config = Config::parse(); + let mut config = Config::parse(); tracing_subscriber::fmt::fmt() .pretty() @@ -11,6 +11,12 @@ fn main() -> Result<()> { .with_max_level(&config.verbosity) .init(); + if config.exclude_attached { + if let Ok(path) = Tmux::attached() { + config.parsers.excludes.push(path) + } + } + let mut projects = Projects::from(config.parsers); projects.extend(config.include); diff --git a/src/tmux.rs b/src/tmux.rs index 559242b..6993c2c 100644 --- a/src/tmux.rs +++ b/src/tmux.rs @@ -24,6 +24,21 @@ impl Tmux { .collect()) } + pub fn attached() -> Result<PathBuf, Error> { + let stdout = Command::new("tmux") + .arg("display") + .arg("-p") + .arg("#{session_path}") + .output()? + .stdout; + + std::str::from_utf8(&stdout)? + .lines() + .map(Into::into) + .last() + .ok_or(Error::NotFound) + } + pub fn get_session(&self, path_buf: PathBuf) -> Result<Project, Error> { let mut filter = OsString::from("#{==:#{session_path},"); filter.push(path_buf.as_os_str()); |