summaryrefslogtreecommitdiffstats
path: root/src/tmux.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2023-03-18 19:01:16 -0500
committerToby Vincent <tobyv13@gmail.com>2023-03-18 19:01:16 -0500
commitb9890b578d8bcdb0d0a9c12ba3901b4e34514286 (patch)
tree6efbe30d3dfb5a26c7ea45d29bf357b60eb63015 /src/tmux.rs
parent9b4f0bb63002a2657d89c7519697aabe515282b4 (diff)
feat: impl history file and sorting
Diffstat (limited to 'src/tmux.rs')
-rw-r--r--src/tmux.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/tmux.rs b/src/tmux.rs
new file mode 100644
index 0000000..c6e83b1
--- /dev/null
+++ b/src/tmux.rs
@@ -0,0 +1,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", timestamp: Some(#{?session_last_attached,#{session_last_attached},#{session_created}}))"##;
+
+ 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)
+ }
+}