Skip to content

Commit 98fb189

Browse files
committed
feat: add flags for printing workspace name and running an adhoc command in the new workspace
1 parent 497456a commit 98fb189

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["edition2024"]
21
[package]
32
name = "twm"
43
description = "A customizable workspace manager for tmux"

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ Options:
5353
5454
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.
5555
56+
-N, --print-workspace-name
57+
Print the name of the workspace generated for the given path to stdout.
58+
59+
This can be used with other options.
60+
61+
-c, --command <COMMAND>
62+
Override any layouts and open the workspace with the given command instead
63+
5664
--make-default-config
5765
Make default configuration file.
5866

src/cli.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ pub struct Arguments {
5353
/// 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.
5454
pub name: Option<String>,
5555

56+
#[clap(short, long)]
57+
#[clap(short = 'N')]
58+
/// Print the name of the workspace generated for the given path to stdout.
59+
///
60+
/// This can be used with other options.
61+
pub print_workspace_name: bool,
62+
63+
#[clap(short, long)]
64+
/// Override any layouts and open the workspace with the given command instead.
65+
pub command: Option<String>,
66+
5667
#[clap(long)]
5768
/// Make default configuration file.
5869
///

src/tmux.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::config::{TwmGlobal, TwmLayout};
33
use crate::layout::{get_commands_from_layout, get_commands_from_layout_name, get_layout_names};
44
use crate::ui::Tui;
55
use crate::ui::{Picker, PickerSelection};
6-
use anyhow::{bail, Context, Result};
6+
use anyhow::{Context, Result, bail};
77
use std::os::unix::process::CommandExt;
88
use std::path::Path;
99
use std::process::{Command, Output};
@@ -315,20 +315,27 @@ pub fn open_workspace(
315315
Some(name) => SessionName::from(name.as_str()),
316316
None => get_session_name_recursive(workspace_path, config.session_name_path_components)?,
317317
};
318+
if args.print_workspace_name {
319+
println!("{}", tmux_name.as_str());
320+
}
318321
if !tmux_has_session(&tmux_name) {
319322
create_tmux_session(&tmux_name, workspace_type, workspace_path)?;
320323
let local_config = find_config_file(Path::new(workspace_path))?;
321-
let cli_layout = if args.layout {
324+
let cli_layout = if args.command.is_none() && args.layout {
322325
Some(get_layout_selection(config, tui)?)
323326
} else {
324327
None
325328
};
326-
let commands = get_workspace_commands(
327-
workspace_type,
328-
config,
329-
cli_layout.as_deref(),
330-
local_config.as_ref(),
331-
)?;
329+
let commands = if let Some(command) = &args.command {
330+
Some(vec![command.as_str()])
331+
} else {
332+
get_workspace_commands(
333+
workspace_type,
334+
config,
335+
cli_layout.as_deref(),
336+
local_config.as_ref(),
337+
)?
338+
};
332339
if let Some(layout_commands) = commands {
333340
send_commands_to_session(&tmux_name.name, &layout_commands)?;
334341
}

0 commit comments

Comments
 (0)