'switch' command now defaults to previous session if present

This commit is contained in:
Valerie Wolfe 2024-07-17 11:07:14 -04:00
parent af33e82415
commit a9a73314af
3 changed files with 23 additions and 16 deletions

View file

@ -8,21 +8,18 @@ use tmux_interface::{
use crate::{ use crate::{
error, error,
state::State, state::State,
util::{ self, NULL } util::{
self,
message,
MSG_PREVIOUS, MSG_SESSION_PATH, NULL
}
}; };
pub fn path(state: &mut State) { pub fn path(state: &mut State) {
state.session_enforce("path"); state.session_enforce("path");
let message = commands::DisplayMessage::new().print().message("#{session_path}"); if let Some(message) = message(MSG_SESSION_PATH) {
println!("{message}");
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]);
} }
} }
@ -36,8 +33,13 @@ pub fn switch(state: &mut State) {
//TODO: -d flag handling needs to be done manually //TODO: -d flag handling needs to be done manually
let args = state.args.clone().finish(); let args = state.args.clone().finish();
if args.len() < 1 { error::missing_target(); } let target: String = match args.get(0).map(|s| s.to_str().unwrap()) {
let target = args.get(0).unwrap().to_string_lossy().to_string(); 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()); let exists = util::session_exists(target.clone());
if !exists { error::no_target(target.clone()); } if !exists { error::no_target(target.clone()); }

View file

@ -9,7 +9,7 @@ use crate::{
env::{ env_var, REPO_FILE, TMUX }, env::{ env_var, REPO_FILE, TMUX },
error, error,
flag::Flags, flag::Flags,
util::{ find, session_name } util::{ find, message, MSG_SESSION_NAME }
}; };
pub struct State<'a> { pub struct State<'a> {
@ -29,7 +29,7 @@ impl State<'_> {
let flags = Flags::from(args); let flags = Flags::from(args);
let tmux_var = env::var(TMUX).ok(); let tmux_var = env::var(TMUX).ok();
let session = tmux_var.is_some(); 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(); let repository = Repository::find();
State { State {

View file

@ -14,8 +14,13 @@ use crate::error;
pub const NULL: Option<StdIO> = Some(StdIO::Null); pub const NULL: Option<StdIO> = Some(StdIO::Null);
pub fn session_name() -> Option<String> { pub const MSG_PREVIOUS: &str = "#{client_last_session}";
let message = commands::DisplayMessage::new().print().message("#{session_name}"); 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<String> {
let message = commands::DisplayMessage::new().print().message(fstr);
let result = Tmux::new().add_command(message).output(); let result = Tmux::new().add_command(message).output();
if let Ok(output) = result { if let Ok(output) = result {