diff options
Diffstat (limited to 'src/tmux.rs')
-rw-r--r-- | src/tmux.rs | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/src/tmux.rs b/src/tmux.rs index fee0f49..88f521f 100644 --- a/src/tmux.rs +++ b/src/tmux.rs @@ -33,7 +33,7 @@ impl Tmux { .output()? .stdout; - let sessions = std::str::from_utf8(&stdout)? + let sessions: Vec<Session> = std::str::from_utf8(&stdout)? .lines() .filter_map(|s| match ron::from_str(s) { Ok(session) => Some(session), @@ -46,6 +46,24 @@ impl Tmux { Ok(sessions) } + + pub fn host(&self) -> Result<Session, Error> { + let stdout = Command::new("tmux") + .arg("-L") + .arg(&self.socket_name) + .arg("display") + .arg("-p") + .arg("#h") + .output()? + .stdout; + + let name = std::str::from_utf8(&stdout)?.trim().into(); + + Ok(Session { + state: crate::State::LocalHost, + name, + }) + } } #[cfg(test)] @@ -81,4 +99,33 @@ mod tests { Ok(()) } + + #[test] + fn test_tmux_host() -> Result<(), Error> { + Command::new("tmux") + .arg("-L") + .arg(SOCKET_NAME) + .arg("new-session") + .arg("-d") + .status()?; + + let name = hostname::get()?.to_string_lossy().into(); + let expected_session = Session { + state: crate::State::LocalHost, + name, + }; + + let tmux = Tmux::new(SOCKET_NAME.to_owned()); + let session = tmux.host()?; + + Command::new("tmux") + .arg("-L") + .arg(SOCKET_NAME) + .arg("kill-server") + .status()?; + + assert_eq!(expected_session, session); + + Ok(()) + } } |