From 98fb189ed008ab345629c1bc17bf7457009b02df Mon Sep 17 00:00:00 2001 From: Vinny Meller Date: Wed, 22 Jan 2025 03:46:48 +0000 Subject: [PATCH] feat: add flags for printing workspace name and running an adhoc command in the new workspace --- Cargo.toml | 1 - README.md | 8 ++++++++ src/cli.rs | 11 +++++++++++ src/tmux.rs | 23 +++++++++++++++-------- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 71ad9d0..27027ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,3 @@ -cargo-features = ["edition2024"] [package] name = "twm" description = "A customizable workspace manager for tmux" diff --git a/README.md b/README.md index 97700fe..d86e9e9 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,14 @@ Options: When setting this option, you should be aware that twm will not "see" this session when performing other automatic actions. For example, if you have a workspace at ~/foobar and run `twm -n jimbob -p ~/foobar`, and then run `twm` and select `~/foobar` from the picker, a new session `foobar` will be created. If you then run `twm -g` and select `foobar`, `foobar-1` will be created in the `foobar` group. + -N, --print-workspace-name + Print the name of the workspace generated for the given path to stdout. + + This can be used with other options. + + -c, --command + Override any layouts and open the workspace with the given command instead + --make-default-config Make default configuration file. diff --git a/src/cli.rs b/src/cli.rs index 708cbb5..40e7e43 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -53,6 +53,17 @@ pub struct Arguments { /// For example, if you have a workspace at ~/foobar and run `twm -n jimbob -p ~/foobar`, and then run `twm` and select `~/foobar` from the picker, a new session `foobar` will be created. If you then run `twm -g` and select `foobar`, `foobar-1` will be created in the `foobar` group. pub name: Option, + #[clap(short, long)] + #[clap(short = 'N')] + /// Print the name of the workspace generated for the given path to stdout. + /// + /// This can be used with other options. + pub print_workspace_name: bool, + + #[clap(short, long)] + /// Override any layouts and open the workspace with the given command instead. + pub command: Option, + #[clap(long)] /// Make default configuration file. /// diff --git a/src/tmux.rs b/src/tmux.rs index 7a726d1..1d5b916 100644 --- a/src/tmux.rs +++ b/src/tmux.rs @@ -3,7 +3,7 @@ use crate::config::{TwmGlobal, TwmLayout}; use crate::layout::{get_commands_from_layout, get_commands_from_layout_name, get_layout_names}; use crate::ui::Tui; use crate::ui::{Picker, PickerSelection}; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use std::os::unix::process::CommandExt; use std::path::Path; use std::process::{Command, Output}; @@ -315,20 +315,27 @@ pub fn open_workspace( Some(name) => SessionName::from(name.as_str()), None => get_session_name_recursive(workspace_path, config.session_name_path_components)?, }; + if args.print_workspace_name { + println!("{}", tmux_name.as_str()); + } if !tmux_has_session(&tmux_name) { create_tmux_session(&tmux_name, workspace_type, workspace_path)?; let local_config = find_config_file(Path::new(workspace_path))?; - let cli_layout = if args.layout { + let cli_layout = if args.command.is_none() && args.layout { Some(get_layout_selection(config, tui)?) } else { None }; - let commands = get_workspace_commands( - workspace_type, - config, - cli_layout.as_deref(), - local_config.as_ref(), - )?; + let commands = if let Some(command) = &args.command { + Some(vec![command.as_str()]) + } else { + get_workspace_commands( + workspace_type, + config, + cli_layout.as_deref(), + local_config.as_ref(), + )? + }; if let Some(layout_commands) = commands { send_commands_to_session(&tmux_name.name, &layout_commands)?; }