summaryrefslogtreecommitdiffstats
path: root/src/tmux.rs
blob: 6c538316e9a74d6d89976060d4282ba5358e26c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::process::Command;

use crate::{Session, SessionSource};

pub use error::Error;

mod error;

#[derive(Debug)]
pub struct Tmux {
    socket_name: String,
}

impl Tmux {
    const SESSION_FORMAT: &str = r##"Session(name: "#S", #{?session_last_attached,timestamp: #{session_last_attached}#, state: Updated,timestamp: #{session_created}#, state: Connected})"##;

    pub fn new(socket_name: String) -> Self {
        Self { socket_name }
    }
}

impl SessionSource for Tmux {
    type Error = Error;

    type Iter = Vec<Session>;

    fn sessions(&self) -> Result<Self::Iter, Self::Error> {
        let output = Command::new("tmux")
            .arg("-L")
            .arg(&self.socket_name)
            .arg("list-sessions")
            .arg("-F")
            .arg(Self::SESSION_FORMAT)
            .output()?
            .stdout;

        let sessions = std::str::from_utf8(&output)?
            .lines()
            .filter_map(|s| match ron::from_str(s) {
                Ok(session) => Some(session),
                Err(err) => {
                    tracing::warn!(%err, "Invalid session format");
                    None
                }
            })
            .collect();

        Ok(sessions)
    }
}