Skip to content

Commit

Permalink
feat: add flags for printing workspace name and running an adhoc comm…
Browse files Browse the repository at this point in the history
…and in the new workspace
  • Loading branch information
vinnymeller committed Jan 22, 2025
1 parent 497456a commit 98fb189
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cargo-features = ["edition2024"]
[package]
name = "twm"
description = "A customizable workspace manager for tmux"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <COMMAND>
Override any layouts and open the workspace with the given command instead
--make-default-config
Make default configuration file.
Expand Down
11 changes: 11 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

#[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<String>,

#[clap(long)]
/// Make default configuration file.
///
Expand Down
23 changes: 15 additions & 8 deletions src/tmux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)?;
}
Expand Down

0 comments on commit 98fb189

Please sign in to comment.