-
-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9c7d28
commit 0b5aa7d
Showing
4 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use crate::cli::resolve_pane_id; | ||
use anyhow::{anyhow, Result}; | ||
use clap::Parser; | ||
use codec::SetPaneZoomed; | ||
use mux::pane::PaneId; | ||
use std::collections::HashMap; | ||
use wezterm_client::client::Client; | ||
|
||
#[derive(Debug, Parser, Clone)] | ||
pub struct ZoomPane { | ||
/// 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>, | ||
|
||
/// Zooms the pane if it wasn't already zoomed | ||
#[arg(long, default_value = "true")] | ||
zoom: bool, | ||
|
||
/// Unzooms the pane if it was zoomed | ||
#[arg(long)] | ||
unzoom: bool, | ||
|
||
/// Toggles the zoom state of the pane | ||
#[arg(long)] | ||
toggle: bool, | ||
} | ||
|
||
impl ZoomPane { | ||
pub async fn run(&self, client: Client) -> Result<()> { | ||
let panes = client.list_panes().await?; | ||
|
||
let mut pane_id_to_tab_id = HashMap::new(); | ||
let mut tab_id_to_active_zoomed_pane_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 && entry.is_zoomed_pane { | ||
tab_id_to_active_zoomed_pane_id.insert(entry.tab_id, entry.pane_id); | ||
} | ||
} | ||
match cursor.preorder_next() { | ||
Ok(c) => cursor = c, | ||
Err(_) => break, | ||
} | ||
} | ||
} | ||
|
||
let pane_id = resolve_pane_id(&client, self.pane_id).await?; | ||
let containing_tab_id = pane_id_to_tab_id | ||
.get(&pane_id) | ||
.copied() | ||
.ok_or_else(|| anyhow!("unable to resolve current tab"))?; | ||
|
||
if self.zoom { | ||
client | ||
.set_zoomed(SetPaneZoomed { | ||
containing_tab_id, | ||
pane_id, | ||
zoomed: true, | ||
}) | ||
.await?; | ||
} | ||
|
||
if self.unzoom { | ||
client | ||
.set_zoomed(SetPaneZoomed { | ||
containing_tab_id, | ||
pane_id, | ||
zoomed: false, | ||
}) | ||
.await?; | ||
} | ||
|
||
if self.toggle { | ||
if tab_id_to_active_zoomed_pane_id.contains_key(&containing_tab_id) { | ||
let target_pane = tab_id_to_active_zoomed_pane_id | ||
.get(&containing_tab_id) | ||
.copied() | ||
.ok_or_else(|| { | ||
anyhow!("could not determine which pane should be active for tab {containing_tab_id}") | ||
})?; | ||
if target_pane == pane_id { | ||
client | ||
.set_zoomed(SetPaneZoomed { | ||
containing_tab_id, | ||
pane_id, | ||
zoomed: false, | ||
}) | ||
.await?; | ||
} | ||
} else { | ||
client | ||
.set_zoomed(SetPaneZoomed { | ||
containing_tab_id, | ||
pane_id, | ||
zoomed: true, | ||
}) | ||
.await?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |