diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-03-20 01:58:49 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-03-20 01:59:28 -0500 |
commit | e11e8bbf14be8f84b57013e4ace1e61071853c12 (patch) | |
tree | 5518d4c5cd133929c10c59dbc004096610fc9793 /src/main.rs | |
parent | d17bdb1e841f9d39bd7c3142afc71ccb86bcc69d (diff) |
feat: impl tmux session switching writing to history file
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index 763194c..b546377 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,20 +2,21 @@ use std::collections::HashMap; use clap::Parser; -use sshr::{Config, History, KnownHosts, Session, SessionSource, Tmux}; +use sshr::{Commands, Config, History, KnownHosts, Session, SessionSource, Tmux}; fn main() -> anyhow::Result<()> { - let config = Config::parse(); + let mut config = Config::parse(); tracing_subscriber::fmt::fmt() .with_max_level(&config.verbosity) .without_time() .init(); - if config.list { - list_sessions(config) - } else { - switch(config.target) + config.history_file = config.history_file.or_else(History::default_path); + + match config.command.to_owned() { + Commands::List => list_sessions(config), + Commands::Switch { name } => switch(config, name), } } @@ -25,9 +26,9 @@ fn list_sessions(config: Config) -> anyhow::Result<()> { sessions = KnownHosts::default().update(sessions)?; sessions = Tmux::new(config.socket_name).update(sessions)?; - if let Some(history_file) = config.history_file.or_else(History::default_path) { - if history_file.exists() { - sessions = History::new(history_file).update(sessions)?; + if let Some(history) = config.history_file.map(History::new) { + if history.exists() { + sessions = history.update(sessions)?; } } @@ -42,6 +43,15 @@ fn list_sessions(config: Config) -> anyhow::Result<()> { Ok(()) } -fn switch(_target: Option<String>) -> anyhow::Result<()> { - todo!() +fn switch(config: Config, name: String) -> anyhow::Result<()> { + let tmux = Tmux::new(config.socket_name); + + if let Some(history) = config.history_file.map(History::new) { + if tmux.switch(&name)?.success() { + let session = tmux.show(&name)?; + history.update_session(session)?; + } + } + + Ok(()) } |