refactor: split cli subcommand into separate modules

This commit is contained in:
Wez Furlong committed 2023-04-02 09:21:33 -07:00
1 parent 50172f2e88
commit 5be7f93317
18 files changed
+1568 -1378

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
use crate::cli::resolve_pane_id;
use clap::Parser;
use mux::pane::PaneId;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct ActivatePane {
/// Specify the target pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
#[arg(long)]
pane_id: Option<PaneId>,
}
impl ActivatePane {
pub async fn run(&self, client: Client) -> anyhow::Result<()> {
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
client
.set_focused_pane_id(codec::SetFocusedPane { pane_id })
.await?;
Ok(())
}
}
@@ -0,0 +1,53 @@
use crate::cli::resolve_pane_id;
use clap::builder::PossibleValue;
use clap::Parser;
use config::keyassignment::PaneDirection;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct ActivatePaneDirection {
/// The direction to switch to.
#[arg(value_parser=PaneDirectionParser{})]
direction: PaneDirection,
}
impl ActivatePaneDirection {
pub async fn run(&self, client: Client) -> anyhow::Result<()> {
let pane_id = resolve_pane_id(&client, None).await?;
client
.activate_pane_direction(codec::ActivatePaneDirection {
pane_id,
direction: self.direction,
})
.await?;
Ok(())
}
}
#[derive(Clone, Copy)]
struct PaneDirectionParser {}
impl clap::builder::TypedValueParser for PaneDirectionParser {
type Value = PaneDirection;
fn parse_ref(
&self,
_cmd: &clap::Command,
_arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
use clap::error::*;
let value = value
.to_str()
.ok_or_else(|| Error::raw(ErrorKind::InvalidUtf8, "value must be a utf8 string\n"))?;
PaneDirection::direction_from_str(value)
.map_err(|e| Error::raw(ErrorKind::InvalidValue, format!("{e}\n")))
}
fn possible_values(&self) -> Option<Box<dyn Iterator<Item = PossibleValue>>> {
Some(Box::new(
PaneDirection::variants().iter().map(PossibleValue::new),
))
}
}
+160
View File
@@ -0,0 +1,160 @@
use crate::cli::resolve_pane_id;
use clap::Parser;
use mux::pane::PaneId;
use mux::tab::TabId;
use std::collections::HashMap;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct ActivateTab {
/// Specify the target tab by its id
#[arg(long, conflicts_with_all=&["tab_index", "tab_relative", "no_wrap", "pane_id"])]
tab_id: Option<TabId>,
/// Specify the target tab by its index within the window
/// that holds the current pane.
/// Indices are 0-based, with 0 being the left-most tab.
/// Negative numbers can be used to reference the right-most
/// tab, so -1 is the right-most tab, -2 is the penultimate
/// tab and so on.
#[arg(long, allow_hyphen_values = true)]
tab_index: Option<isize>,
/// Specify the target tab by its relative offset.
/// -1 selects the tab to the left. -2 two tabs to the left.
/// 1 is one tab to the right and so on.
///
/// Unless `--no-wrap` is specified, relative moves wrap
/// around from the left-most to right-most and vice versa.
#[arg(long, allow_hyphen_values = true)]
tab_relative: Option<isize>,
/// When used with tab-relative, prevents wrapping around
/// and will instead clamp to the left-most when moving left
/// or right-most when moving right.
#[arg(long, requires = "tab_relative")]
no_wrap: bool,
/// Specify the current pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
///
/// The pane is used to figure out which window
/// contains appropriate tabs
#[arg(long)]
pane_id: Option<PaneId>,
}
impl ActivateTab {
pub async fn run(&self, client: Client) -> anyhow::Result<()> {
let panes = client.list_panes().await?;
let mut pane_id_to_tab_id = HashMap::new();
let mut tab_id_to_active_pane_id = HashMap::new();
let mut tabs_by_window = HashMap::new();
let mut window_by_tab_id = HashMap::new();
for tabroot in panes.tabs {
let mut cursor = tabroot.into_tree().cursor();
loop {
if let Some(entry) = cursor.leaf_mut() {
pane_id_to_tab_id.insert(entry.pane_id, entry.tab_id);
if entry.is_active_pane {
tab_id_to_active_pane_id.insert(entry.tab_id, entry.pane_id);
}
window_by_tab_id.insert(entry.tab_id, entry.window_id);
let win = tabs_by_window
.entry(entry.window_id)
.or_insert_with(Vec::new);
if win.last().copied() != Some(entry.tab_id) {
win.push(entry.tab_id);
}
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(_) => break,
}
}
}
let tab_id = if let Some(tab_id) = self.tab_id {
tab_id
} else {
// Find the current tab from the pane id
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
let current_tab_id = pane_id_to_tab_id
.get(&pane_id)
.copied()
.ok_or_else(|| anyhow::anyhow!("unable to resolve current tab"))?;
let window = window_by_tab_id
.get(&current_tab_id)
.copied()
.ok_or_else(|| anyhow::anyhow!("unable to resolve current window"))?;
let tabs = tabs_by_window
.get(&window)
.ok_or_else(|| anyhow::anyhow!("unable to resolve tabs for current window"))?;
let max = tabs.len();
anyhow::ensure!(max > 0, "window has no tabs!?");
if let Some(tab_index) = self.tab_index {
// This logic is coupled with TermWindow::activate_tab
// If you update this, update that!
let tab_idx = if tab_index < 0 {
max.saturating_sub(tab_index.abs() as usize)
} else {
tab_index as usize
};
tabs.get(tab_idx)
.copied()
.ok_or_else(|| anyhow::anyhow!("tab index {tab_index} is invalid"))?
} else if let Some(delta) = self.tab_relative {
// This logic is coupled with TermWindow::activate_tab_relative
// If you update this, update that!
let wrap = !self.no_wrap;
let active = tabs
.iter()
.position(|&tab_id| tab_id == current_tab_id)
.ok_or_else(|| anyhow::anyhow!("current tab is not in window!?"))?
as isize;
let tab = active + delta;
let tab_idx = if wrap {
let tab = if tab < 0 { max as isize + tab } else { tab };
(tab as usize % max) as isize
} else {
if tab < 0 {
0
} else if tab >= max as isize {
max as isize - 1
} else {
tab
}
};
tabs.get(tab_idx as usize)
.copied()
.ok_or_else(|| anyhow::anyhow!("tab index {tab_idx} is invalid"))?
} else {
anyhow::bail!("impossible arguments!");
}
};
// Now that we know which tab we want to activate, figure out
// which pane will be the active pane
let target_pane = tab_id_to_active_pane_id
.get(&tab_id)
.copied()
.ok_or_else(|| {
anyhow::anyhow!("could not determine which pane should be active for tab {tab_id}")
})?;
client
.set_focused_pane_id(codec::SetFocusedPane {
pane_id: target_pane,
})
.await?;
Ok(())
}
}
+94
View File
@@ -0,0 +1,94 @@
use crate::cli::resolve_pane_id;
use clap::Parser;
use mux::pane::PaneId;
use termwiz_funcs::lines_to_escapes;
use wezterm_client::client::Client;
use wezterm_term::{ScrollbackOrVisibleRowIndex, StableRowIndex};
#[derive(Debug, Parser, Clone)]
pub struct GetText {
/// Specify the target pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
#[arg(long)]
pane_id: Option<PaneId>,
/// The starting line number.
/// 0 is the first line of terminal screen.
/// Negative numbers proceed backwards into the scrollback.
/// The default value is unspecified is 0, the first line of
/// the terminal screen.
#[arg(long, allow_hyphen_values = true)]
start_line: Option<ScrollbackOrVisibleRowIndex>,
/// The ending line number.
/// 0 is the first line of terminal screen.
/// Negative numbers proceed backwards into the scrollback.
/// The default value if unspecified is the bottom of the
/// the terminal screen.
#[arg(long, allow_hyphen_values = true)]
end_line: Option<ScrollbackOrVisibleRowIndex>,
/// Include escape sequences that color and style the text.
/// If omitted, unattributed text will be returned.
#[arg(long)]
escapes: bool,
}
impl GetText {
pub async fn run(self, client: Client) -> anyhow::Result<()> {
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
let info = client
.get_dimensions(codec::GetPaneRenderableDimensions { pane_id })
.await?;
let start_line = match self.start_line {
None => info.dimensions.physical_top,
Some(n) if n >= 0 => info.dimensions.physical_top + n as StableRowIndex,
Some(n) => {
let line = info.dimensions.physical_top as isize + n as isize;
if line < info.dimensions.scrollback_top as isize {
info.dimensions.scrollback_top
} else {
line as StableRowIndex
}
}
};
let end_line = match self.end_line {
None => info.dimensions.physical_top + info.dimensions.viewport_rows as StableRowIndex,
Some(n) if n >= 0 => info.dimensions.physical_top + n as StableRowIndex,
Some(n) => {
let line = info.dimensions.physical_top as isize + n as isize;
if line < info.dimensions.scrollback_top as isize {
info.dimensions.scrollback_top
} else {
line as StableRowIndex
}
}
};
let lines = client
.get_lines(codec::GetLines {
pane_id: pane_id.into(),
lines: vec![start_line..end_line + 1],
})
.await?;
let lines = lines
.lines
.extract_data()
.0
.into_iter()
.map(|(_idx, line)| line)
.collect();
if self.escapes {
println!("{}", lines_to_escapes(lines)?);
} else {
lines.iter().for_each(|line| println!("{}", line.as_str()));
}
Ok(())
}
}
+21
View File
@@ -0,0 +1,21 @@
use crate::cli::resolve_pane_id;
use clap::Parser;
use mux::pane::PaneId;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct KillPane {
/// Specify the target pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
#[arg(long)]
pane_id: Option<PaneId>,
}
impl KillPane {
pub async fn run(&self, client: Client) -> anyhow::Result<()> {
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
client.kill_pane(codec::KillPane { pane_id }).await?;
Ok(())
}
}
+193
View File
@@ -0,0 +1,193 @@
use crate::cli::CliOutputFormatKind;
use clap::Parser;
use serde::Serializer as _;
use tabout::{tabulate_output, Alignment, Column};
use wezterm_client::client::Client;
use wezterm_term::TerminalSize;
#[derive(Debug, Parser, Clone, Copy)]
pub struct ListCommand {
/// Controls the output format.
/// "table" and "json" are possible formats.
#[arg(long = "format", default_value = "table")]
format: CliOutputFormatKind,
}
impl ListCommand {
pub async fn run(&self, client: Client) -> anyhow::Result<()> {
let out = std::io::stdout();
let mut output_items = vec![];
let panes = client.list_panes().await?;
for (tabroot, tab_title) in panes.tabs.into_iter().zip(panes.tab_titles.iter()) {
let mut cursor = tabroot.into_tree().cursor();
loop {
if let Some(entry) = cursor.leaf_mut() {
let window_title = panes
.window_titles
.get(&entry.window_id)
.map(|s| s.as_str())
.unwrap_or("");
output_items.push(CliListResultItem::from(
entry.clone(),
tab_title,
window_title,
));
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(_) => break,
}
}
}
match self.format {
CliOutputFormatKind::Json => {
let mut writer = serde_json::Serializer::pretty(out.lock());
writer.collect_seq(output_items.iter())?;
}
CliOutputFormatKind::Table => {
let cols = vec![
Column {
name: "WINID".to_string(),
alignment: Alignment::Right,
},
Column {
name: "TABID".to_string(),
alignment: Alignment::Right,
},
Column {
name: "PANEID".to_string(),
alignment: Alignment::Right,
},
Column {
name: "WORKSPACE".to_string(),
alignment: Alignment::Left,
},
Column {
name: "SIZE".to_string(),
alignment: Alignment::Left,
},
Column {
name: "TITLE".to_string(),
alignment: Alignment::Left,
},
Column {
name: "CWD".to_string(),
alignment: Alignment::Left,
},
];
let data = output_items
.iter()
.map(|output_item| {
vec![
output_item.window_id.to_string(),
output_item.tab_id.to_string(),
output_item.pane_id.to_string(),
output_item.workspace.to_string(),
format!("{}x{}", output_item.size.cols, output_item.size.rows),
output_item.title.to_string(),
output_item.cwd.to_string(),
]
})
.collect::<Vec<_>>();
tabulate_output(&cols, &data, &mut std::io::stdout().lock())?;
}
}
Ok(())
}
}
#[derive(serde::Serialize)]
struct CliListResultPtySize {
rows: usize,
cols: usize,
/// Pixel width of the pane, if known (can be zero)
pixel_width: usize,
/// Pixel height of the pane, if known (can be zero)
pixel_height: usize,
/// dpi of the pane, if known (can be zero)
dpi: u32,
}
// This will be serialized to JSON via the 'List' command.
// As such it is intended to be a stable output format,
// Thus we need to be careful about both the fields and their types,
// herein as they are directly reflected in the output.
#[derive(serde::Serialize)]
struct CliListResultItem {
window_id: mux::window::WindowId,
tab_id: mux::tab::TabId,
pane_id: mux::pane::PaneId,
workspace: String,
size: CliListResultPtySize,
title: String,
cwd: String,
/// Cursor x coordinate from top left of non-scrollback pane area
cursor_x: usize,
/// Cursor y coordinate from top left of non-scrollback pane area
cursor_y: usize,
cursor_shape: termwiz::surface::CursorShape,
cursor_visibility: termwiz::surface::CursorVisibility,
/// Number of cols from the left of the tab area to the left of this pane
left_col: usize,
/// Number of rows from the top of the tab area to the top of this pane
top_row: usize,
tab_title: String,
window_title: String,
}
impl CliListResultItem {
fn from(pane: mux::tab::PaneEntry, tab_title: &str, window_title: &str) -> CliListResultItem {
let mux::tab::PaneEntry {
window_id,
tab_id,
pane_id,
workspace,
title,
working_dir,
cursor_pos,
physical_top,
left_col,
top_row,
size:
TerminalSize {
rows,
cols,
pixel_width,
pixel_height,
dpi,
},
..
} = pane;
CliListResultItem {
window_id,
tab_id,
pane_id,
workspace,
size: CliListResultPtySize {
rows,
cols,
pixel_width,
pixel_height,
dpi,
},
title,
cwd: working_dir
.as_ref()
.map(|url| url.url.as_str())
.unwrap_or("")
.to_string(),
cursor_x: cursor_pos.x,
cursor_y: cursor_pos.y.saturating_sub(physical_top) as usize,
cursor_shape: cursor_pos.shape,
cursor_visibility: cursor_pos.visibility,
left_col,
top_row,
tab_title: tab_title.to_string(),
window_title: window_title.to_string(),
}
}
}
+145
View File
@@ -0,0 +1,145 @@
use crate::cli::CliOutputFormatKind;
use chrono::{DateTime, Utc};
use clap::Parser;
use serde::Serializer as _;
use tabout::{tabulate_output, Alignment, Column};
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone, Copy)]
pub struct ListClientsCommand {
/// Controls the output format.
/// "table" and "json" are possible formats.
#[arg(long = "format", default_value = "table")]
format: CliOutputFormatKind,
}
impl ListClientsCommand {
pub async fn run(&self, client: Client) -> anyhow::Result<()> {
let out = std::io::stdout();
let clients = client.list_clients(codec::GetClientList).await?;
match self.format {
CliOutputFormatKind::Json => {
let clients = clients
.clients
.iter()
.cloned()
.map(CliListClientsResultItem::from);
let mut writer = serde_json::Serializer::pretty(out.lock());
writer.collect_seq(clients)?;
}
CliOutputFormatKind::Table => {
let cols = vec![
Column {
name: "USER".to_string(),
alignment: Alignment::Left,
},
Column {
name: "HOST".to_string(),
alignment: Alignment::Left,
},
Column {
name: "PID".to_string(),
alignment: Alignment::Right,
},
Column {
name: "CONNECTED".to_string(),
alignment: Alignment::Left,
},
Column {
name: "IDLE".to_string(),
alignment: Alignment::Left,
},
Column {
name: "WORKSPACE".to_string(),
alignment: Alignment::Left,
},
Column {
name: "FOCUS".to_string(),
alignment: Alignment::Right,
},
];
let mut data = vec![];
let now: DateTime<Utc> = Utc::now();
fn duration_string(d: chrono::Duration) -> String {
if let Ok(d) = d.to_std() {
format!("{:?}", d)
} else {
d.to_string()
}
}
for info in clients.clients {
let connected = now - info.connected_at;
let idle = now - info.last_input;
data.push(vec![
info.client_id.username.to_string(),
info.client_id.hostname.to_string(),
info.client_id.pid.to_string(),
duration_string(connected),
duration_string(idle),
info.active_workspace.as_deref().unwrap_or("").to_string(),
info.focused_pane_id
.map(|id| id.to_string())
.unwrap_or_else(String::new),
]);
}
tabulate_output(&cols, &data, &mut out.lock())?;
}
}
Ok(())
}
}
// This will be serialized to JSON via the 'ListClients' command.
// As such it is intended to be a stable output format,
// Thus we need to be careful about the stability of the fields and types
// herein as they are directly reflected in the output.
#[derive(serde::Serialize)]
struct CliListClientsResultItem {
username: String,
hostname: String,
pid: u32,
connection_elapsed: std::time::Duration,
idle_time: std::time::Duration,
workspace: String,
focused_pane_id: Option<mux::pane::PaneId>,
}
impl From<mux::client::ClientInfo> for CliListClientsResultItem {
fn from(client_info: mux::client::ClientInfo) -> CliListClientsResultItem {
let now: DateTime<Utc> = Utc::now();
let mux::client::ClientInfo {
connected_at,
last_input,
active_workspace,
focused_pane_id,
client_id,
..
} = client_info;
let mux::client::ClientId {
username,
hostname,
pid,
..
} = client_id.as_ref();
let connection_elapsed = now - connected_at;
let idle_time = now - last_input;
CliListClientsResultItem {
username: username.to_string(),
hostname: hostname.to_string(),
pid: *pid,
connection_elapsed: connection_elapsed
.to_std()
.unwrap_or(std::time::Duration::ZERO),
idle_time: idle_time.to_std().unwrap_or(std::time::Duration::ZERO),
workspace: active_workspace.as_deref().unwrap_or("").to_string(),
focused_pane_id: focused_pane_id,
}
}
}
+231
View File
@@ -0,0 +1,231 @@
use anyhow::anyhow;
use clap::Parser;
use mux::pane::PaneId;
use std::ffi::OsString;
use wezterm_client::client::Client;
mod activate_pane;
mod activate_pane_direction;
mod activate_tab;
mod get_text;
mod kill_pane;
mod list;
mod list_clients;
mod move_pane_to_new_tab;
mod proxy;
mod rename_workspace;
mod send_text;
mod set_tab_title;
mod set_window_title;
mod spawn_command;
mod split_pane;
mod tls_creds;
#[derive(Debug, Parser, Clone, Copy)]
enum CliOutputFormatKind {
#[command(name = "table", about = "multi line space separated table")]
Table,
#[command(name = "json", about = "JSON format")]
Json,
}
impl std::str::FromStr for CliOutputFormatKind {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<CliOutputFormatKind, Self::Err> {
match s {
"json" => Ok(CliOutputFormatKind::Json),
"table" => Ok(CliOutputFormatKind::Table),
_ => Err(anyhow::anyhow!("unknown output format")),
}
}
}
#[derive(Debug, Parser, Clone, Copy)]
struct CliOutputFormat {
/// Controls the output format.
/// "table" and "json" are possible formats.
#[arg(long = "format", default_value = "table")]
format: CliOutputFormatKind,
}
#[derive(Debug, Parser, Clone)]
pub struct CliCommand {
/// Don't automatically start the server
#[arg(long = "no-auto-start")]
no_auto_start: bool,
/// Prefer connecting to a background mux server.
/// The default is to prefer connecting to a running
/// wezterm gui instance
#[arg(long = "prefer-mux")]
prefer_mux: bool,
/// When connecting to a gui instance, if you started the
/// gui with `--class SOMETHING`, you should also pass
/// that same value here in order for the client to find
/// the correct gui instance.
#[arg(long = "class")]
class: Option<String>,
#[command(subcommand)]
sub: CliSubCommand,
}
#[derive(Debug, Parser, Clone)]
enum CliSubCommand {
#[command(name = "list", about = "list windows, tabs and panes")]
List(list::ListCommand),
#[command(name = "list-clients", about = "list clients")]
ListClients(list_clients::ListClientsCommand),
#[command(name = "proxy", about = "start rpc proxy pipe")]
Proxy(proxy::ProxyCommand),
#[command(name = "tlscreds", about = "obtain tls credentials")]
TlsCreds(tls_creds::TlsCredsCommand),
#[command(
name = "move-pane-to-new-tab",
rename_all = "kebab",
about = "Move a pane into a new tab"
)]
MovePaneToNewTab(move_pane_to_new_tab::MovePaneToNewTab),
#[command(
name = "split-pane",
rename_all = "kebab",
trailing_var_arg = true,
about = "split the current pane.
Outputs the pane-id for the newly created pane on success"
)]
SplitPane(split_pane::SplitPane),
#[command(
name = "spawn",
trailing_var_arg = true,
about = "Spawn a command into a new window or tab
Outputs the pane-id for the newly created pane on success"
)]
SpawnCommand(spawn_command::SpawnCommand),
/// Send text to a pane as though it were pasted.
/// If bracketed paste mode is enabled in the pane, then the
/// text will be sent as a bracketed paste.
#[command(name = "send-text", rename_all = "kebab")]
SendText(send_text::SendText),
/// Retrieves the textual content of a pane and output it to stdout
#[command(name = "get-text", rename_all = "kebab")]
GetText(get_text::GetText),
/// Activate an adjacent pane in the specified direction.
#[command(name = "activate-pane-direction", rename_all = "kebab")]
ActivatePaneDirection(activate_pane_direction::ActivatePaneDirection),
/// Kill a pane
#[command(name = "kill-pane", rename_all = "kebab")]
KillPane(kill_pane::KillPane),
/// Activate (focus) a pane
#[command(name = "activate-pane", rename_all = "kebab")]
ActivatePane(activate_pane::ActivatePane),
/// Activate a tab
#[command(name = "activate-tab", rename_all = "kebab")]
ActivateTab(activate_tab::ActivateTab),
/// Change the title of a tab
#[command(name = "set-tab-title", rename_all = "kebab")]
SetTabTitle(set_tab_title::SetTabTitle),
/// Change the title of a window
#[command(name = "set-window-title", rename_all = "kebab")]
SetWindowTitle(set_window_title::SetWindowTitle),
/// Rename a workspace
#[command(name = "rename-workspace", rename_all = "kebab")]
RenameWorkspace(rename_workspace::RenameWorkspace),
}
async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow::Result<()> {
let mut ui = mux::connui::ConnectionUI::new_headless();
let initial = true;
let client = Client::new_default_unix_domain(
initial,
&mut ui,
cli.no_auto_start,
cli.prefer_mux,
cli.class
.as_deref()
.unwrap_or(wezterm_gui_subcommands::DEFAULT_WINDOW_CLASS),
)?;
match cli.sub {
CliSubCommand::ListClients(cmd) => cmd.run(client).await,
CliSubCommand::List(cmd) => cmd.run(client).await,
CliSubCommand::MovePaneToNewTab(cmd) => cmd.run(client).await,
CliSubCommand::SplitPane(cmd) => cmd.run(client).await,
CliSubCommand::SendText(cmd) => cmd.run(client).await,
CliSubCommand::GetText(cmd) => cmd.run(client).await,
CliSubCommand::SpawnCommand(cmd) => cmd.run(client, &config).await,
CliSubCommand::Proxy(cmd) => cmd.run(client, &config).await,
CliSubCommand::TlsCreds(cmd) => cmd.run(client).await,
CliSubCommand::ActivatePaneDirection(cmd) => cmd.run(client).await,
CliSubCommand::KillPane(cmd) => cmd.run(client).await,
CliSubCommand::ActivatePane(cmd) => cmd.run(client).await,
CliSubCommand::ActivateTab(cmd) => cmd.run(client).await,
CliSubCommand::SetTabTitle(cmd) => cmd.run(client).await,
CliSubCommand::SetWindowTitle(cmd) => cmd.run(client).await,
CliSubCommand::RenameWorkspace(cmd) => cmd.run(client).await,
}
}
pub fn run_cli(config: config::ConfigHandle, cli: CliCommand) -> anyhow::Result<()> {
let executor = promise::spawn::ScopedExecutor::new();
match promise::spawn::block_on(executor.run(async move { run_cli_async(config, cli).await })) {
Ok(_) => Ok(()),
Err(err) => crate::terminate_with_error(err),
}
}
pub async fn resolve_pane_id(client: &Client, pane_id: Option<PaneId>) -> anyhow::Result<PaneId> {
let pane_id: PaneId = match pane_id {
Some(p) => p,
None => {
if let Ok(pane) = std::env::var("WEZTERM_PANE") {
pane.parse()?
} else {
let mut clients = client.list_clients(codec::GetClientList).await?.clients;
clients.retain(|client| client.focused_pane_id.is_some());
clients.sort_by(|a, b| b.last_input.cmp(&a.last_input));
if clients.is_empty() {
anyhow::bail!(
"--pane-id was not specified and $WEZTERM_PANE
is not set in the environment, and I couldn't
determine which pane was currently focused"
);
}
clients[0]
.focused_pane_id
.expect("to have filtered out above")
}
}
};
Ok(pane_id)
}
pub fn resolve_relative_cwd(cwd: Option<OsString>) -> anyhow::Result<Option<String>> {
match cwd {
None => Ok(None),
Some(cwd) => Ok(Some(
std::env::current_dir()?
.join(cwd)
.to_str()
.ok_or_else(|| anyhow!("path is not representable as String"))?
.to_string(),
)),
}
}
+76
View File
@@ -0,0 +1,76 @@
use crate::cli::resolve_pane_id;
use clap::Parser;
use mux::pane::PaneId;
use mux::window::WindowId;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct MovePaneToNewTab {
/// Specify the pane that should be moved.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
#[arg(long)]
pane_id: Option<PaneId>,
/// Specify the window into which the new tab will be
/// created.
/// If omitted, the window associated with the current
/// pane is used.
#[arg(long)]
window_id: Option<WindowId>,
/// Create tab in a new window, rather than the window
/// currently containing the pane.
#[arg(long, conflicts_with = "window_id")]
new_window: bool,
/// If creating a new window, override the default workspace name
/// with the provided name. The default name is "default".
#[arg(long)]
workspace: Option<String>,
}
impl MovePaneToNewTab {
pub async fn run(&self, client: Client) -> anyhow::Result<()> {
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
let window_id = if self.new_window {
None
} else {
match self.window_id {
Some(w) => Some(w),
None => {
let panes = client.list_panes().await?;
let mut window_id = None;
'outer_move: for tabroot in panes.tabs {
let mut cursor = tabroot.into_tree().cursor();
loop {
if let Some(entry) = cursor.leaf_mut() {
if entry.pane_id == pane_id {
window_id.replace(entry.window_id);
break 'outer_move;
}
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(_) => break,
}
}
}
window_id
}
}
};
let moved = client
.move_pane_to_new_tab(codec::MovePaneToNewTab {
pane_id,
window_id,
workspace_for_new_window: self.workspace.clone(),
})
.await?;
log::debug!("{:?}", moved);
Ok(())
}
}
+69
View File
@@ -0,0 +1,69 @@
use clap::Parser;
use config::ConfigHandle;
use mux::activity::Activity;
use mux::Mux;
use std::io::{Read, Write};
use std::sync::Arc;
use wezterm_client::client::{unix_connect_with_retry, Client};
#[derive(Debug, Parser, Clone)]
pub struct ProxyCommand {}
impl ProxyCommand {
pub async fn run(&self, client: Client, config: &ConfigHandle) -> anyhow::Result<()> {
// The client object we created above will have spawned
// the server if needed, so now all we need to do is turn
// ourselves into basically netcat.
drop(client);
let mux = Arc::new(mux::Mux::new(None));
Mux::set_mux(&mux);
let unix_dom = config.unix_domains.first().unwrap();
let target = unix_dom.target();
let stream = unix_connect_with_retry(&target, false, None)?;
// Spawn a thread to pull data from the socket and write
// it to stdout
let duped = stream.try_clone()?;
let activity = Activity::new();
std::thread::spawn(move || {
let stdout = std::io::stdout();
consume_stream_then_exit_process(duped, stdout.lock(), activity);
});
// and pull data from stdin and write it to the socket
let activity = Activity::new();
std::thread::spawn(move || {
let stdin = std::io::stdin();
consume_stream_then_exit_process(stdin.lock(), stream, activity);
});
// Wait forever; the stdio threads will terminate on EOF
smol::future::pending().await
}
}
fn consume_stream<F: Read, T: Write>(mut from_stream: F, mut to_stream: T) -> anyhow::Result<()> {
let mut buf = [0u8; 8192];
loop {
let size = from_stream.read(&mut buf)?;
if size == 0 {
break;
}
to_stream.write_all(&buf[0..size])?;
to_stream.flush()?;
}
Ok(())
}
fn consume_stream_then_exit_process<F: Read, T: Write>(
from_stream: F,
to_stream: T,
activity: Activity,
) -> ! {
consume_stream(from_stream, to_stream).ok();
std::thread::sleep(std::time::Duration::new(2, 0));
drop(activity);
std::process::exit(0);
}
+65
View File
@@ -0,0 +1,65 @@
use crate::cli::resolve_pane_id;
use clap::Parser;
use mux::pane::PaneId;
use std::collections::HashMap;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct RenameWorkspace {
/// Specify the workspace to rename
#[arg(long)]
workspace: Option<String>,
/// Specify the current pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
///
/// The pane is used to figure out which workspace
/// should be renamed.
#[arg(long)]
pane_id: Option<PaneId>,
/// The new name for the workspace
new_workspace: String,
}
impl RenameWorkspace {
pub async fn run(self, client: Client) -> anyhow::Result<()> {
let panes = client.list_panes().await?;
let mut pane_id_to_workspace = HashMap::new();
for tabroot in panes.tabs {
let mut cursor = tabroot.into_tree().cursor();
loop {
if let Some(entry) = cursor.leaf_mut() {
pane_id_to_workspace.insert(entry.pane_id, entry.workspace.to_string());
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(_) => break,
}
}
}
let old_workspace = if let Some(workspace) = self.workspace {
workspace
} else {
// Find the current tab from the pane id
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
pane_id_to_workspace
.get(&pane_id)
.ok_or_else(|| anyhow::anyhow!("unable to resolve current workspace"))?
.to_string()
};
client
.rename_workspace(codec::RenameWorkspace {
old_workspace,
new_workspace: self.new_workspace,
})
.await?;
Ok(())
}
}
+53
View File
@@ -0,0 +1,53 @@
use crate::cli::resolve_pane_id;
use anyhow::Context;
use clap::Parser;
use mux::pane::PaneId;
use std::io::Read;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct SendText {
/// Specify the target pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
#[arg(long)]
pane_id: Option<PaneId>,
/// Send the text directly, rather than as a bracketed paste.
#[arg(long)]
no_paste: bool,
/// The text to send. If omitted, will read the text from stdin.
text: Option<String>,
}
impl SendText {
pub async fn run(self, client: Client) -> anyhow::Result<()> {
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
let data = match self.text {
Some(text) => text,
None => {
let mut text = String::new();
std::io::stdin()
.read_to_string(&mut text)
.context("reading stdin")?;
text
}
};
if self.no_paste {
client
.write_to_pane(codec::WriteToPane {
pane_id,
data: data.as_bytes().to_vec(),
})
.await?;
} else {
client
.send_paste(codec::SendPaste { pane_id, data })
.await?;
}
Ok(())
}
}
+64
View File
@@ -0,0 +1,64 @@
use crate::cli::resolve_pane_id;
use clap::Parser;
use mux::pane::PaneId;
use mux::tab::TabId;
use std::collections::HashMap;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct SetTabTitle {
/// Specify the target tab by its id
#[arg(long, conflicts_with_all=&["pane_id"])]
tab_id: Option<TabId>,
/// Specify the current pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
///
/// The pane is used to figure out which tab should be renamed.
#[arg(long)]
pane_id: Option<PaneId>,
/// The new title for the tab
title: String,
}
impl SetTabTitle {
pub async fn run(self, client: Client) -> anyhow::Result<()> {
let panes = client.list_panes().await?;
let mut pane_id_to_tab_id = HashMap::new();
for tabroot in panes.tabs {
let mut cursor = tabroot.into_tree().cursor();
loop {
if let Some(entry) = cursor.leaf_mut() {
pane_id_to_tab_id.insert(entry.pane_id, entry.tab_id);
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(_) => break,
}
}
}
let tab_id = if let Some(tab_id) = self.tab_id {
tab_id
} else {
// Find the current tab from the pane id
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
pane_id_to_tab_id
.get(&pane_id)
.copied()
.ok_or_else(|| anyhow::anyhow!("unable to resolve current tab"))?
};
client
.set_tab_title(codec::TabTitleChanged {
tab_id,
title: self.title,
})
.await?;
Ok(())
}
}
+65
View File
@@ -0,0 +1,65 @@
use crate::cli::resolve_pane_id;
use clap::Parser;
use mux::pane::PaneId;
use mux::window::WindowId;
use std::collections::HashMap;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct SetWindowTitle {
/// Specify the target window by its id
#[arg(long, conflicts_with_all=&["pane_id"])]
window_id: Option<WindowId>,
/// Specify the current pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
///
/// The pane is used to figure out which window
/// should be renamed.
#[arg(long)]
pane_id: Option<PaneId>,
/// The new title for the window
title: String,
}
impl SetWindowTitle {
pub async fn run(self, client: Client) -> anyhow::Result<()> {
let panes = client.list_panes().await?;
let mut pane_id_to_window_id = HashMap::new();
for tabroot in panes.tabs {
let mut cursor = tabroot.into_tree().cursor();
loop {
if let Some(entry) = cursor.leaf_mut() {
pane_id_to_window_id.insert(entry.pane_id, entry.window_id);
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(_) => break,
}
}
}
let window_id = if let Some(window_id) = self.window_id {
window_id
} else {
// Find the current tab from the pane id
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
pane_id_to_window_id
.get(&pane_id)
.copied()
.ok_or_else(|| anyhow::anyhow!("unable to resolve current window"))?
};
client
.set_window_title(codec::WindowTitleChanged {
window_id,
title: self.title,
})
.await?;
Ok(())
}
}
+123
View File
@@ -0,0 +1,123 @@
use crate::cli::{resolve_pane_id, resolve_relative_cwd};
use clap::{Parser, ValueHint};
use config::keyassignment::SpawnTabDomain;
use config::ConfigHandle;
use mux::pane::PaneId;
use mux::window::WindowId;
use portable_pty::cmdbuilder::CommandBuilder;
use std::ffi::OsString;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct SpawnCommand {
/// Specify the current pane.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
/// The pane is used to determine the current domain
/// and window.
#[arg(long)]
pane_id: Option<PaneId>,
#[arg(long)]
domain_name: Option<String>,
/// Specify the window into which to spawn a tab.
/// If omitted, the window associated with the current
/// pane is used.
/// Cannot be used with `--workspace` or `--new-window`.
#[arg(long, conflicts_with_all=&["workspace", "new_window"])]
window_id: Option<WindowId>,
/// Spawn into a new window, rather than a new tab.
#[arg(long)]
new_window: bool,
/// Specify the current working directory for the initially
/// spawned program
#[arg(long, value_parser, value_hint=ValueHint::DirPath)]
cwd: Option<OsString>,
/// When creating a new window, override the default workspace name
/// with the provided name. The default name is "default".
/// Requires `--new-window`.
#[arg(long, requires = "new_window")]
workspace: Option<String>,
/// Instead of executing your shell, run PROG.
/// For example: `wezterm cli spawn -- bash -l` will spawn bash
/// as if it were a login shell.
#[arg(value_parser, value_hint=ValueHint::CommandWithArguments, num_args=1..)]
prog: Vec<OsString>,
}
impl SpawnCommand {
pub async fn run(self, client: Client, config: &ConfigHandle) -> anyhow::Result<()> {
let window_id = if self.new_window {
None
} else {
match self.window_id {
Some(w) => Some(w),
None => {
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
let panes = client.list_panes().await?;
let mut window_id = None;
'outer: for tabroot in panes.tabs {
let mut cursor = tabroot.into_tree().cursor();
loop {
if let Some(entry) = cursor.leaf_mut() {
if entry.pane_id == pane_id {
window_id.replace(entry.window_id);
break 'outer;
}
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(_) => break,
}
}
}
window_id
}
}
};
let workspace = self
.workspace
.as_deref()
.unwrap_or(
config
.default_workspace
.as_deref()
.unwrap_or(mux::DEFAULT_WORKSPACE),
)
.to_string();
let size = config.initial_size(0);
let spawned = client
.spawn_v2(codec::SpawnV2 {
domain: self
.domain_name
.map_or(SpawnTabDomain::DefaultDomain, |name| {
SpawnTabDomain::DomainName(name)
}),
window_id,
command: if self.prog.is_empty() {
None
} else {
let builder = CommandBuilder::from_argv(self.prog);
Some(builder)
},
command_dir: resolve_relative_cwd(self.cwd)?,
size,
workspace,
})
.await?;
log::debug!("{:?}", spawned);
println!("{}", spawned.pane_id);
Ok(())
}
}
+115
View File
@@ -0,0 +1,115 @@
use crate::cli::{resolve_pane_id, resolve_relative_cwd};
use clap::{Parser, ValueHint};
use mux::pane::PaneId;
use mux::tab::{SplitDirection, SplitRequest, SplitSize};
use portable_pty::cmdbuilder::CommandBuilder;
use std::ffi::OsString;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct SplitPane {
/// Specify the pane that should be split.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
#[arg(long)]
pane_id: Option<PaneId>,
/// Equivalent to `--right`. If neither this nor any other direction
/// is specified, the default is equivalent to `--bottom`.
#[arg(long, conflicts_with_all=&["left", "right", "top", "bottom"])]
horizontal: bool,
/// Split horizontally, with the new pane on the left
#[arg(long, conflicts_with_all=&["right", "top", "bottom"])]
left: bool,
/// Split horizontally, with the new pane on the right
#[arg(long, conflicts_with_all=&["left", "top", "bottom"])]
right: bool,
/// Split vertically, with the new pane on the top
#[arg(long, conflicts_with_all=&["left", "right", "bottom"])]
top: bool,
/// Split vertically, with the new pane on the bottom
#[arg(long, conflicts_with_all=&["left", "right", "top"])]
bottom: bool,
/// Rather than splitting the active pane, split the entire
/// window.
#[arg(long)]
top_level: bool,
/// The number of cells that the new split should have.
/// If omitted, 50% of the available space is used.
#[arg(long)]
cells: Option<usize>,
/// Specify the number of cells that the new split should
/// have, expressed as a percentage of the available space.
#[arg(long, conflicts_with = "cells")]
percent: Option<u8>,
/// Specify the current working directory for the initially
/// spawned program
#[arg(long, value_parser, value_hint=ValueHint::DirPath)]
cwd: Option<OsString>,
/// Instead of spawning a new command, move the specified
/// pane into the newly created split.
#[arg(long, conflicts_with_all=&["cwd", "prog"])]
move_pane_id: Option<PaneId>,
/// Instead of executing your shell, run PROG.
/// For example: `wezterm cli split-pane -- bash -l` will spawn bash
/// as if it were a login shell.
#[arg(value_parser, value_hint=ValueHint::CommandWithArguments, num_args=1..)]
prog: Vec<OsString>,
}
impl SplitPane {
pub async fn run(self, client: Client) -> anyhow::Result<()> {
let pane_id = resolve_pane_id(&client, self.pane_id).await?;
let direction = if self.left || self.right || self.horizontal {
SplitDirection::Horizontal
} else if self.top || self.bottom {
SplitDirection::Vertical
} else {
SplitDirection::Vertical
};
let target_is_second = !(self.left || self.top);
let size = match (self.cells, self.percent) {
(Some(c), _) => SplitSize::Cells(c),
(_, Some(p)) => SplitSize::Percent(p),
(None, None) => SplitSize::Percent(50),
};
let split_request = SplitRequest {
direction,
target_is_second,
size,
top_level: self.top_level,
};
let spawned = client
.split_pane(codec::SplitPane {
pane_id,
split_request,
domain: config::keyassignment::SpawnTabDomain::CurrentPaneDomain,
command: if self.prog.is_empty() {
None
} else {
let builder = CommandBuilder::from_argv(self.prog);
Some(builder)
},
command_dir: resolve_relative_cwd(self.cwd)?,
move_pane_id: self.move_pane_id,
})
.await?;
log::debug!("{:?}", spawned);
println!("{}", spawned.pane_id);
Ok(())
}
}
+13
View File
@@ -0,0 +1,13 @@
use clap::Parser;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct TlsCredsCommand {}
impl TlsCredsCommand {
pub async fn run(self, client: Client) -> anyhow::Result<()> {
let creds = client.get_tls_creds().await?;
codec::Pdu::GetTlsCredsResponse(creds).encode(std::io::stdout().lock(), 0)?;
Ok(())
}
}
+5 -1378
View File
File diff suppressed because it is too large. Load diff