From b9890b578d8bcdb0d0a9c12ba3901b4e34514286 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sat, 18 Mar 2023 19:01:16 -0500 Subject: feat: impl history file and sorting --- src/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index d465d78..8d33eac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,50 @@ +use std::{collections::HashMap, fmt::Display, iter::IntoIterator}; + +use serde::Deserialize; + pub use config::Config; +pub use history::History; +pub use tmux::Tmux; + +mod config; +mod history; +mod tmux; + +pub type Timestamp = Option; + +pub trait SessionSource { + type Error: std::error::Error; -mod config { - use clap::Parser; + type Iter: IntoIterator; - #[derive(Debug, Clone, Parser)] - pub struct Config { - pub target: Option, + fn sessions(&self) -> Result; + + fn update( + &self, + mut hash_map: HashMap, + ) -> Result, Self::Error> { + for session in self.sessions()? { + hash_map + .entry(session.name) + .and_modify(|o| { + if let Some(timestamp) = session.timestamp { + *o = o.map(|t| timestamp.max(t)); + } + }) + .or_insert(session.timestamp); + } + Ok(hash_map) + } +} + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize)] +pub struct Session { + pub timestamp: Timestamp, + pub name: String, +} - #[arg(short, long)] - pub list: bool, +impl Display for Session { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.name) } } -- cgit v1.2.3-70-g09d2