From a9a73314afe6d1a6f7deb70302c3c59c248c3d71 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 17 Jul 2024 11:07:14 -0400 Subject: [PATCH] 'switch' command now defaults to previous session if present --- src/command/session.rs | 26 ++++++++++++++------------ src/state.rs | 4 ++-- src/util.rs | 9 +++++++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/command/session.rs b/src/command/session.rs index 0ececee..9d66b16 100644 --- a/src/command/session.rs +++ b/src/command/session.rs @@ -8,21 +8,18 @@ use tmux_interface::{ use crate::{ error, state::State, - util::{ self, NULL } + util::{ + self, + message, + MSG_PREVIOUS, MSG_SESSION_PATH, NULL + } }; pub fn path(state: &mut State) { state.session_enforce("path"); - let message = commands::DisplayMessage::new().print().message("#{session_path}"); - - let result = Tmux::new().add_command(message).output().unwrap(); - let text = String::from_utf8(result.0.stdout); - - if let Ok(output) = text { - // trim the trailing line break - let target = output.len() - 1; - println!("{}", &output[0..target]); + if let Some(message) = message(MSG_SESSION_PATH) { + println!("{message}"); } } @@ -36,8 +33,13 @@ pub fn switch(state: &mut State) { //TODO: -d flag handling needs to be done manually let args = state.args.clone().finish(); - if args.len() < 1 { error::missing_target(); } - let target = args.get(0).unwrap().to_string_lossy().to_string(); + let target: String = match args.get(0).map(|s| s.to_str().unwrap()) { + None | + Some("-") => if let Some(prev) = message(MSG_PREVIOUS) { prev } + else { error::missing_target(); panic!() }, + + Some(inner) => inner.to_owned() + }; let exists = util::session_exists(target.clone()); if !exists { error::no_target(target.clone()); } diff --git a/src/state.rs b/src/state.rs index 4d00743..10ccbd5 100644 --- a/src/state.rs +++ b/src/state.rs @@ -9,7 +9,7 @@ use crate::{ env::{ env_var, REPO_FILE, TMUX }, error, flag::Flags, - util::{ find, session_name } + util::{ find, message, MSG_SESSION_NAME } }; pub struct State<'a> { @@ -29,7 +29,7 @@ impl State<'_> { let flags = Flags::from(args); let tmux_var = env::var(TMUX).ok(); let session = tmux_var.is_some(); - let title = if session { session_name() } else { None }; + let title = if session { message(MSG_SESSION_NAME) } else { None }; let repository = Repository::find(); State { diff --git a/src/util.rs b/src/util.rs index ccee203..dbaa1d1 100644 --- a/src/util.rs +++ b/src/util.rs @@ -14,8 +14,13 @@ use crate::error; pub const NULL: Option = Some(StdIO::Null); -pub fn session_name() -> Option { - let message = commands::DisplayMessage::new().print().message("#{session_name}"); +pub const MSG_PREVIOUS: &str = "#{client_last_session}"; +pub const MSG_SESSION_NAME: &str = "#S"; +pub const MSG_SESSION_PATH: &str = "#{session_path}"; +pub const MSG_WINDOW_NAME: &str = "#{window_name}"; + +pub fn message(fstr: &str) -> Option { + let message = commands::DisplayMessage::new().print().message(fstr); let result = Tmux::new().add_command(message).output(); if let Ok(output) = result {