summaryrefslogtreecommitdiffstats
path: root/src/history.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2023-03-20 01:58:49 -0500
committerToby Vincent <tobyv13@gmail.com>2023-03-20 01:59:28 -0500
commite11e8bbf14be8f84b57013e4ace1e61071853c12 (patch)
tree5518d4c5cd133929c10c59dbc004096610fc9793 /src/history.rs
parentd17bdb1e841f9d39bd7c3142afc71ccb86bcc69d (diff)
feat: impl tmux session switching writing to history file
Diffstat (limited to 'src/history.rs')
-rw-r--r--src/history.rs46
1 files changed, 43 insertions, 3 deletions
diff --git a/src/history.rs b/src/history.rs
index 9d29468..d7e7063 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -1,14 +1,25 @@
use std::{
fs::File,
- io::{BufRead, BufReader},
+ io::{BufRead, BufReader, BufWriter, Write},
path::PathBuf,
};
use directories::ProjectDirs;
-use crate::Session;
+use crate::{Session, SessionSource};
-use super::SessionSource;
+pub use error::Error;
+
+mod error {
+ #[derive(Debug, thiserror::Error)]
+ pub enum Error {
+ #[error("IO error: {0}")]
+ IO(#[from] std::io::Error),
+
+ #[error(transparent)]
+ Format(#[from] ron::Error),
+ }
+}
#[derive(Debug)]
pub struct History(PathBuf);
@@ -24,6 +35,27 @@ impl History {
.join("history")
.into()
}
+
+ pub fn update_session(&self, session: Session) -> Result<(), Error> {
+ std::fs::create_dir_all(&self.0)?;
+
+ let mut sessions: Vec<Session> = self
+ .sessions()?
+ .into_iter()
+ .filter(|s| s.name == session.name)
+ .collect();
+
+ sessions.push(session);
+
+ let mut writer = BufWriter::new(File::create(&self.0)?);
+
+ Ok(sessions
+ .into_iter()
+ .try_for_each(|s| match ron::to_string(&s) {
+ Ok(ser) => writeln!(writer, "{ser}"),
+ Err(err) => Ok(tracing::warn!(?err, "Failed to re-serialize session")),
+ })?)
+ }
}
impl SessionSource for History {
@@ -53,3 +85,11 @@ impl SessionSource for History {
Ok(sessions)
}
}
+
+impl std::ops::Deref for History {
+ type Target = PathBuf;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}