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/main.rs | 63 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 36 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index ebd0526..7534bb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,57 +1,48 @@ -use std::{process::Command, time::Duration}; +use std::collections::HashMap; use clap::Parser; -use serde::Deserialize; -use sshr::Config; + +use sshr::{Config, History, Session, SessionSource, Timestamp, Tmux}; fn main() -> anyhow::Result<()> { let config = Config::parse(); + tracing_subscriber::fmt::fmt() + .with_max_level(&config.verbosity) + .without_time() + .init(); + if config.list { - list() + list_sessions(config) } else { switch(config.target) } } -#[derive(Debug, Deserialize)] -struct Session { - pub name: String, - pub created: u64, - pub attached: u64, -} +fn list_sessions(config: Config) -> anyhow::Result<()> { + let mut sessions: HashMap = HashMap::new(); -fn list() -> anyhow::Result<()> { - let sessions = tmux_sessions()?; + let tmux = Tmux::new(config.socket_name); + sessions = tmux.update(sessions)?; - for session in sessions { - println!("{session:?}"); + if let Some(history_file) = config.history_file.or_else(History::default_path) { + if history_file.exists() { + sessions = History::new(history_file).update(sessions)?; + } } - Ok(()) -} + let mut sessions: Vec = sessions + .into_iter() + .map(|(name, timestamp)| Session { name, timestamp }) + .collect(); + + sessions.sort(); -fn tmux_sessions() -> anyhow::Result> { - let format = indoc::indoc! {r##"[ - #{S: Session( - name: "#S", - created: #{session_created}, - attached: #{session_last_attached} - ),} - ]"##}; - - let output = Command::new("tmux") - .arg("-L") - .arg("ssh") - .arg("display") - .arg("-p") - .arg(format) - .output()?; - - match std::str::from_utf8(&output.stdout)? { - "" => Ok(Vec::new()), - s => ron::from_str(s).map_err(Into::into), + for session in sessions { + println!("{session}"); } + + Ok(()) } fn switch(_target: Option) -> anyhow::Result<()> { -- cgit v1.2.3-70-g09d2