summaryrefslogtreecommitdiffstats
path: root/src/main.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/main.rs
parent9b4f0bb63002a2657d89c7519697aabe515282b4 (diff)
feat: impl history file and sorting
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs63
1 files changed, 27 insertions, 36 deletions
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<String, Timestamp> = 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<Session> = sessions
+ .into_iter()
+ .map(|(name, timestamp)| Session { name, timestamp })
+ .collect();
+
+ sessions.sort();
-fn tmux_sessions() -> anyhow::Result<Vec<Session>> {
- 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<String>) -> anyhow::Result<()> {