From 5aa4b4e55b2113853a2d98e86b16de1bad19bcb8 Mon Sep 17 00:00:00 2001 From: Peter Simonsson Date: Sat, 9 Nov 2024 15:59:26 +0100 Subject: [PATCH] Add command to open a named session fixes #90 --- README.md | 32 +++++++++++++++++--------------- src/cli.rs | 24 ++++++++++++++++++++++++ src/error.rs | 2 ++ 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ee96da0..ab70a9d 100644 --- a/README.md +++ b/README.md @@ -69,25 +69,27 @@ Use `tms --help` ``` Scan for all git folders in specified directorires, select one and open it as a new tmux session -Usage: tms [COMMAND] +Usage: tms [OPTIONS] [COMMAND] Commands: - config Configure the defaults for search paths and excluded directories - start Initialize tmux with the default sessions - switch Display other sessions with a fuzzy finder and a preview window - windows Display the current session's windows with a fuzzy finder and a preview window - kill Kill the current tmux session and jump to another - sessions Show running tmux sessions with asterisk on the current session - rename Rename the active session and the working directory - refresh Creates new worktree windows for the selected session - clone-repo Clone repository and create a new session for it - init-repo Initialize empty repository - bookmark Bookmark a directory so it is available to select along with the Git repositories - help Print this message or the help of the given subcommand(s) + config Configure the defaults for search paths and excluded directories + start Initialize tmux with the default sessions + switch Display other sessions with a fuzzy finder and a preview window + windows Display the current session's windows with a fuzzy finder and a preview window + kill Kill the current tmux session and jump to another + sessions Show running tmux sessions with asterisk on the current session + rename Rename the active session and the working directory + refresh Creates new worktree windows for the selected session + clone-repo Clone repository and create a new session for it + init-repo Initialize empty repository + bookmark Bookmark a directory so it is available to select along with the Git repositories + open-session Open a session + help Print this message or the help of the given subcommand(s) Options: - -h, --help Print help - -V, --version Print version + --generate [possible values: bash, elvish, fish, powershell, zsh] + -h, --help Print help + -V, --version Print version ``` ### Configuring defaults diff --git a/src/cli.rs b/src/cli.rs index 57d4398..66770ee 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -56,6 +56,8 @@ pub enum CliCommand { InitRepo(InitRepoCommand), /// Bookmark a directory so it is available to select along with the Git repositories Bookmark(BookmarkCommand), + /// Open a session + OpenSession(OpenSessionCommand), } #[derive(Debug, Args)] @@ -141,6 +143,12 @@ pub struct BookmarkCommand { path: Option, } +#[derive(Debug, Args)] +pub struct OpenSessionCommand { + /// Name of the session to open. + session: Box, +} + impl Cli { pub fn handle_sub_commands(&self, tmux: &Tmux) -> Result { if let Some(generator) = self.generator { @@ -212,6 +220,11 @@ impl Cli { Ok(SubCommandGiven::Yes) } + Some(CliCommand::OpenSession(args)) => { + open_session_command(args, config, tmux)?; + Ok(SubCommandGiven::Yes) + } + None => Ok(SubCommandGiven::No(config.into())), } } @@ -739,6 +752,17 @@ fn bookmark_command(args: &BookmarkCommand, mut config: Config) -> Result<()> { Ok(()) } +fn open_session_command(args: &OpenSessionCommand, config: Config, tmux: &Tmux) -> Result<()> { + let sessions = create_sessions(&config)?; + + if let Some(session) = sessions.find_session(&args.session) { + session.switch_to(tmux, &config)?; + Ok(()) + } else { + Err(TmsError::SessionNotFound(args.session.to_string()).into()) + } +} + fn print_completions(gen: G, cmd: &mut Command) { let name = if let Ok(exe) = std::env::current_exe() { if let Some(exe) = exe.file_name() { diff --git a/src/error.rs b/src/error.rs index 9bb0af1..68c8475 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,6 +9,7 @@ pub enum TmsError { TuiError(String), IoError, ConfigError, + SessionNotFound(String), } impl Display for TmsError { @@ -19,6 +20,7 @@ impl Display for TmsError { Self::NonUtf8Path => write!(f, "Non Utf-8 Path"), Self::IoError => write!(f, "IO Error"), Self::TuiError(inner) => write!(f, "TUI error: {inner}"), + Self::SessionNotFound(inner) => write!(f, "Session {inner} not found"), } } }