Skip to content

Commit

Permalink
Add command to open a named session
Browse files Browse the repository at this point in the history
fixes #90
  • Loading branch information
petersimonsson committed Nov 12, 2024
1 parent 9a384e5 commit 5aa4b4e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <GENERATOR> [possible values: bash, elvish, fish, powershell, zsh]
-h, --help Print help
-V, --version Print version
```

### Configuring defaults
Expand Down
24 changes: 24 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -141,6 +143,12 @@ pub struct BookmarkCommand {
path: Option<String>,
}

#[derive(Debug, Args)]
pub struct OpenSessionCommand {
/// Name of the session to open.
session: Box<str>,
}

impl Cli {
pub fn handle_sub_commands(&self, tmux: &Tmux) -> Result<SubCommandGiven> {
if let Some(generator) = self.generator {
Expand Down Expand Up @@ -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())),
}
}
Expand Down Expand Up @@ -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<G: Generator>(gen: G, cmd: &mut Command) {
let name = if let Ok(exe) = std::env::current_exe() {
if let Some(exe) = exe.file_name() {
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum TmsError {
TuiError(String),
IoError,
ConfigError,
SessionNotFound(String),
}

impl Display for TmsError {
Expand All @@ -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"),
}
}
}
Expand Down

0 comments on commit 5aa4b4e

Please sign in to comment.