# crates/libtmux/src/window.rs

libtmux/libtmux-rs@6a256a08d97d

````rust
//! Window handles and their snapshot getters.

use std::ffi::{OsStr, OsString};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::SystemTime;

use crate::formats::TmuxText;
use crate::internal::core::Core;
use crate::internal::listing;
use crate::internal::scoped;
use crate::pane::Pane;
#[cfg(feature = "query")]
use crate::query::{FilterSchema, Filterable, ReadField};
use crate::session::Session;
#[cfg(feature = "query")]
use crate::snapshot::{Availability, FieldRef, WindowFields, WindowInfo};
use crate::snapshot::{WindowProjection, stored_time};
use crate::target::{ServerIdentity, SessionId, WindowId};
use crate::{Command, CommandResult, Error, ObjectKind, TmuxArg};

mod navigation;
mod settings;

/// Which way to move focus among a window's panes.
///
/// tmux decides what is "up" from a pane's position on screen rather than
/// from its index, so these follow the layout rather than the pane order.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::{PaneDirection, SplitDirection};
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("work").await?;
/// let window = session.active_window().await?.expect("a session has a window");
/// let lower = window.split(SplitDirection::Below).await?;
///
/// // Focus follows the layout, and tmux wraps at the edge rather than
/// // refusing, so this always names a pane.
/// let focused = window.focus_direction(PaneDirection::Below).await?;
/// assert_eq!(focused.id(), lower.id());
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum PaneDirection {
    /// The pane above.
    Above,
    /// The pane below.
    Below,
    /// The pane to the left.
    Left,
    /// The pane to the right.
    Right,
}

impl PaneDirection {
    /// The tmux flag that selects this direction.
    const fn flag(self) -> &'static str {
        match self {
            Self::Above => "-U",
            Self::Below => "-D",
            Self::Left => "-L",
            Self::Right => "-R",
        }
    }
}

/// One tmux window, as reached through one session that links it.
///
/// A window is not owned by a single session. `link-window` makes the same
/// underlying window appear in several sessions at once, so discovery returns
/// one `Window` per link rather than per window. Two handles for the same
/// window reached through different sessions are **not** equal: they describe
/// different places in the hierarchy.
///
/// Getters that describe the window itself, such as [`Window::name`], read the
/// window. Getters that describe its place in a session, such as
/// [`Window::index`] and [`Window::is_active`], read the link.
///
/// # What is on here
///
/// The methods on a `Window` fall into a few groups:
///
/// **Finding the window you are in.** Resolve the window this process is
/// running in, through the pane `TMUX_PANE` names or through a value you pass.
/// Both report `Ok(None)` when tmux no longer has that pane, and fail when the
/// value is absent or not a pane ID.
///
/// [`from_env`], [`from_env_value`]
///
/// **Reading the snapshot.** Getters are synchronous: they read the snapshot
/// this handle owns and send nothing to tmux. `last_activity` is when the
/// window last produced output, whatever its options say, while `has_activity`
/// is an alert that stays false unless `monitor-activity` is on, which it is
/// not by default. Refreshing updates this handle in place or returns a new
/// one, keeps following the same link, and fails with [`Error::ObjectGone`]
/// when the session no longer links the window; `get` reads any snapshot
/// field, including those with no getter, and needs the `query` feature, which
/// is on by default.
///
/// - Identity: [`id`], [`session_id`], [`index`], [`name`]
/// - Shape: [`pane_count`], [`width`], [`height`], [`layout`], [`is_zoomed`]
/// - State: [`is_active`], [`is_linked`], [`has_activity`], [`has_bell`],
///   [`last_activity`]
/// - Any field: [`get`]
/// - Refresh: [`refresh`], [`refreshed`]
///
/// **Panes and sessions.** `panes` returns the same panes through every link
/// to the window, and a failed listing is an error, not an empty list;
/// `search_panes` needs the `query` feature, which is on by default. `pane_at`
/// reports `Ok(None)` when nothing matches, and `active_pane` does so only when
/// the window has gone, because a window always has an active pane; `session`
/// re-reads tmux and reports `Ok(None)` when the session is gone. Focus follows
/// the layout rather than pane order: `focus_direction` wraps at the edge and
/// always returns a pane, and `last_pane` reports `Ok(None)` when no other pane
/// has been active.
///
/// - List: [`panes`]
/// - Find one: [`pane_at`], [`active_pane`]
/// - Narrow: [`search_panes`]
/// - Move focus: [`focus_direction`], [`last_pane`]
/// - Sessions: [`session`], [`linked_sessions`]
///
/// **Adding and arranging panes.** `split` returns the new pane, takes a
/// [`SplitOptions`] or a bare [`SplitDirection`], and leaves focus where it was
/// unless asked. `with_pane` creates a pane, runs an operation with it, and
/// kills the pane afterwards: a failure to create it, to run the operation or
/// to kill it comes back as a `ScopeError`, and a cleanup failure carries
/// [`Error::AfterEffect`]. If the future is dropped, the pane is still killed
/// while the Tokio runtime is alive. A layout is a [`Layout`] by name, or a
/// saved string from [`Window::layout`] that restores pane sizes exactly; the
/// mirrored layouts need tmux 3.5, and a saved string that tmux 3.8 produced
/// is refused by an older tmux. `rotate` moves the panes round one position and
/// keeps the layout.
///
/// - Add: [`split`], [`with_pane`]
/// - Layout: [`select_layout`], [`next_layout`], [`previous_layout`]
/// - Order: [`rotate`]
///
/// **Changing the window.** Renaming changes the name for every session that
/// links the window. A name is sent as text, and only a [`TmuxArg::format`]
/// value is expanded: see
/// [the crate documentation][crate#a-name-reaches-tmux-as-text]. Selecting
/// makes the window active in the session this handle came through;
/// respawning restarts the window's command and replaces every pane with the
/// one it runs in, and fails while a pane is still running unless
/// [`Respawn::Replacing`] says to kill it. `resize` sets the window to an
/// exact size in cells, and `resize_by` moves one of its edges. Killing
/// consumes the handle and closes the window in every session that links it.
///
/// [`rename`], [`select`], [`resize`], [`resize_by`], [`respawn`], [`kill`]
///
/// **Linking and moving.** Linking puts the same window in another session, so
/// a rename or a split shows in both; unlinking removes only this session's
/// link, and tmux refuses when only one session holds the window. Moving
/// places the window at an index, possibly in another session, and fails when
/// the destination is occupied or absent; swapping trades places with another
/// window. Linking, moving and swapping return [`Error::ServerMismatch`] for a
/// window or session on another server.
///
/// [`link_to`], [`unlink`], [`move_to`], [`swap_with`]
///
/// **Options and hooks.** Read and set options at this window's scope:
/// listings report what this window has set, not what it inherits, and
/// `typed_option` reads `Ok(None)` for an option that holds nothing here.
/// `set_typed_option` checks the value against tmux's option table before
/// anything is sent, `set_option` sends text and leaves tmux to refuse it, and
/// [`OptionValue`] says how the reads and writes fit together. tmux does not
/// enumerate the hooks set on a window, so there is no listing: read one by
/// name with `hook`, which gives `Ok(None)` when it holds nothing.
///
/// - Read: [`option_names`], [`options`], [`typed_option`]
/// - Write: [`set_typed_option`], [`set_option`], [`append_option`],
///   [`unset_option`]
/// - Hooks: [`set_hook`], [`unset_hook`], [`set_hooks`], [`hook`]
///
/// **Formats and messages.** Expand a tmux format in this window's context and
/// get the text back, or show a message to the clients viewing it. Both read
/// their text as a format, so command and recursive formats can run shell
/// commands, and `display` succeeds when nobody is watching.
///
/// [`format`], [`display`]
///
/// Anything tmux can do to a window that is not here is reachable through
/// [`cmd`], which runs an arbitrary command against it and hands back its
/// result.
///
/// [`from_env`]: Window::from_env
/// [`from_env_value`]: Window::from_env_value
/// [`id`]: Window::id
/// [`session_id`]: Window::session_id
/// [`index`]: Window::index
/// [`name`]: Window::name
/// [`pane_count`]: Window::pane_count
/// [`width`]: Window::width
/// [`height`]: Window::height
/// [`layout`]: Window::layout
/// [`is_zoomed`]: Window::is_zoomed
/// [`is_active`]: Window::is_active
/// [`is_linked`]: Window::is_linked
/// [`has_activity`]: Window::has_activity
/// [`has_bell`]: Window::has_bell
/// [`last_activity`]: Window::last_activity
/// [`get`]: Window::get
/// [`refresh`]: Window::refresh
/// [`refreshed`]: Window::refreshed
/// [`panes`]: Window::panes
/// [`pane_at`]: Window::pane_at
/// [`active_pane`]: Window::active_pane
/// [`search_panes`]: Window::search_panes
/// [`focus_direction`]: Window::focus_direction
/// [`last_pane`]: Window::last_pane
/// [`session`]: Window::session
/// [`linked_sessions`]: Window::linked_sessions
/// [`split`]: Window::split
/// [`with_pane`]: Window::with_pane
/// [`select_layout`]: Window::select_layout
/// [`next_layout`]: Window::next_layout
/// [`previous_layout`]: Window::previous_layout
/// [`rotate`]: Window::rotate
/// [`resize`]: Window::resize
/// [`resize_by`]: Window::resize_by
/// [`rename`]: Window::rename
/// [`select`]: Window::select
/// [`respawn`]: Window::respawn
/// [`kill`]: Window::kill
/// [`link_to`]: Window::link_to
/// [`unlink`]: Window::unlink
/// [`move_to`]: Window::move_to
/// [`swap_with`]: Window::swap_with
/// [`option_names`]: Window::option_names
/// [`options`]: Window::options
/// [`typed_option`]: Window::typed_option
/// [`set_typed_option`]: Window::set_typed_option
/// [`set_option`]: Window::set_option
/// [`append_option`]: Window::append_option
/// [`unset_option`]: Window::unset_option
/// [`set_hook`]: Window::set_hook
/// [`unset_hook`]: Window::unset_hook
/// [`set_hooks`]: Window::set_hooks
/// [`hook`]: Window::hook
/// [`format`]: Window::format
/// [`display`]: Window::display
/// [`cmd`]: Window::cmd
/// [`Error::ObjectGone`]: crate::Error::ObjectGone
/// [`Error::ServerMismatch`]: crate::Error::ServerMismatch
/// [`Error::AfterEffect`]: crate::Error::AfterEffect
/// [`SplitOptions`]: crate::SplitOptions
/// [`SplitDirection`]: crate::SplitDirection
/// [`Layout`]: crate::Layout
/// [`Respawn::Replacing`]: crate::Respawn::Replacing
/// [`OptionValue`]: crate::OptionValue
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::SplitDirection;
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("work").await?;
/// let window = session.active_window().await?.expect("a session has a window");
///
/// // Splitting is detached by default, so focus stays where it was.
/// window.split(SplitDirection::Below).await?;
/// assert_eq!(window.panes().await?.len(), 2);
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct Window {
    core: Arc<Core>,
    projection: WindowProjection,
}

impl Window {
    /// Build a handle from a hydrated projection.
    pub(crate) const fn new(core: Arc<Core>, projection: WindowProjection) -> Self {
        Self { core, projection }
    }

    /// Find the window this process is running in.
    ///
    /// Resolved through the pane named by `TMUX_PANE`, which is the only
    /// value tmux gives a process that identifies exactly where it is.
    ///
    /// `Ok(None)` means tmux no longer has that pane.
    ///
    /// # Errors
    ///
    /// Returns an error when `TMUX_PANE` is absent or is not a pane ID, or
    /// when the listing fails.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// # let guard = libtmux::test::TestServer::new().await?;
    /// # let server = guard.server();
    /// use libtmux::Window;
    ///
    /// let session = server.new_session("locating-window").await?;
    /// let pane = session.panes().await?.remove(0);
    ///
    /// // Standing in for the environment tmux gives a process it starts.
    /// let found = Window::from_env_value(server, Some(pane.id().as_ref())).await?;
    ///
    /// assert_eq!(found.expect("the window exists").id(), pane.window_id());
    /// # guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn from_env(server: &crate::Server) -> Result<Option<Self>, Error> {
        Self::from_env_value(server, std::env::var_os("TMUX_PANE")).await
    }

    /// Find a window from an explicit `TMUX_PANE` value.
    ///
    /// # Errors
    ///
    /// Returns an error when the value is absent or is not a pane ID, or when
    /// the listing fails.
    pub async fn from_env_value(
        server: &crate::Server,
        value: Option<impl AsRef<OsStr>>,
    ) -> Result<Option<Self>, Error> {
        let Some(pane) = Pane::from_env_value(server, value).await? else {
            return Ok(None);
        };

        server.window_by_id(pane.window_id()).await
    }

    /// Return the tmux window identity.
    ///
    /// This is the `@`-prefixed id, which is shared by every link to the same
    /// window.
    #[must_use]
    pub const fn id(&self) -> &WindowId {
        self.projection.window().window_id()
    }

    /// Return the session this handle reached the window through.
    #[must_use]
    pub const fn session_id(&self) -> &SessionId {
        self.projection.link().identity().session_id()
    }

    /// Return the window's index within [`Window::session_id`].
    ///
    /// A linked window can hold a different index in each session, so this is
    /// a property of the link rather than of the window.
    #[must_use]
    pub const fn index(&self) -> i32 {
        self.projection.link().identity().window_index()
    }

    /// Return the window name.
    #[must_use]
    pub fn name(&self) -> &TmuxText {
        self.projection.window().window_name()
    }

    /// Return how many panes the window contains.
    #[must_use]
    pub fn pane_count(&self) -> u32 {
        *self.projection.window().window_panes()
    }

    /// Return the window width in cells.
    #[must_use]
    pub fn width(&self) -> u32 {
        *self.projection.window().window_width()
    }

    /// Return the window height in cells.
    #[must_use]
    pub fn height(&self) -> u32 {
        *self.projection.window().window_height()
    }

    /// Return the window's pane layout string.
    #[must_use]
    pub fn layout(&self) -> &TmuxText {
        self.projection.window().window_layout()
    }

    /// Report whether this window is the active one in its session.
    #[must_use]
    pub const fn is_active(&self) -> bool {
        self.projection.link().is_active()
    }

    /// Report whether the window is linked into more than one session.
    #[must_use]
    pub const fn is_linked(&self) -> bool {
        self.projection.link().is_linked()
    }

    /// Report whether the window has unseen activity in this session.
    #[must_use]
    pub const fn has_activity(&self) -> bool {
        self.projection.link().has_activity()
    }

    /// Report whether the window rang a bell in this session.
    #[must_use]
    pub const fn has_bell(&self) -> bool {
        self.projection.link().has_bell()
    }

    /// Return when the window last produced output.
    ///
    /// tmux stamps this on every byte a pane in the window writes, whatever
    /// the window options say. That is what separates it from
    /// [`Self::has_activity`], which is an alert and stays false unless
    /// `monitor-activity` was turned on -- and it is off by default.
    ///
    /// tmux keeps whole seconds. `WindowFields::window_activity` filters and
    /// reads the same field as the `i64` of Unix seconds tmux reports, because
    /// the query grammar compares integers.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// use std::time::{Duration, SystemTime};
    ///
    /// let guard = libtmux::test::TestServer::new().await?;
    /// let session = guard.server().new_session("busy").await?;
    /// let window = session.active_window().await?.expect("a window");
    ///
    /// // A window that has just been made has already produced output.
    /// let idle = SystemTime::now().duration_since(window.last_activity())?;
    /// assert!(idle < Duration::from_secs(3600));
    ///
    /// guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn last_activity(&self) -> SystemTime {
        stored_time(*self.projection.window().window_activity())
    }

    /// Report whether one of the window's panes is zoomed to fill it.
    ///
    /// This is the window's own flag rather than the pane's, because tmux
    /// only reports `pane_zoomed_flag` from 3.7 onwards.
    #[must_use]
    pub fn is_zoomed(&self) -> bool {
        *self.projection.window().window_zoomed_flag()
    }

    /// Return the identity of the server this window belongs to.
    pub(crate) fn server_identity(&self) -> &ServerIdentity {
        self.core.configuration().identity()
    }

    fn link_target(&self) -> String {
        format!("{}:{}", self.session_id(), self.id())
    }

    /// Replace this handle's snapshot with the window's current state.
    ///
    /// The handle keeps following the same link, so a window that moved to a
    /// different index in the same session is found at its new index.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ObjectGone`] when this session no longer links the
    /// window, or a listing error when tmux could not be read.
    pub async fn refresh(&mut self) -> Result<&mut Self, Error> {
        let session = self.session_id().clone();
        self.refresh_in(&session).await
    }

    async fn refresh_in(&mut self, session: &SessionId) -> Result<&mut Self, Error> {
        let session = session.to_string();
        let projection = listing::windows(&self.core, listing::Scope::Target(&session), None)
            .await?
            .into_iter()
            .find(|projection| projection.window().window_id() == self.id())
            .ok_or_else(|| Error::ObjectGone {
                kind: ObjectKind::Window,
                id: self.id().to_string(),
            })?;

        self.projection = projection;
        Ok(self)
    }

    /// Return a new handle holding the window's current state.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ObjectGone`] when this session no longer links the
    /// window, or a listing error when tmux could not be read.
    pub async fn refreshed(&self) -> Result<Self, Error> {
        let mut refreshed = self.clone();
        refreshed.refresh().await?;
        Ok(refreshed)
    }

    /// Split this window and return the new pane.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the split, which includes a target
    /// pane that is too small to divide.
    pub async fn split(&self, options: impl Into<SplitOptions>) -> Result<Pane, Error> {
        let options = options.into();
        let window = self.id().to_string();
        let projection =
            listing::create_pane(&self.core, |format| options.into_command(&window, format))
                .await?;

        Ok(Pane::new(Arc::clone(&self.core), projection))
    }

    /// Rename the window and update this handle.
    ///
    /// The name belongs to the window, so every session linking it sees the
    /// change.
    ///
    /// A name given as text reaches tmux as that text, `#` included. Only a
    /// [`TmuxArg::format`] value is expanded as a format, so `#(command)` in
    /// one runs a shell command. See
    /// [the crate documentation][crate#a-name-reaches-tmux-as-text].
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the name.
    pub async fn rename(&mut self, name: impl Into<TmuxArg>) -> Result<&mut Self, Error> {
        listing::mutate(
            &self.core,
            "rename-window",
            Command::new("rename-window")
                .arg("-t")
                .arg(self.id().to_string())
                .arg("--")
                .arg(name.into().into_os_string()),
        )
        .await?;

        self.refresh()
            .await
            .map_err(|error| error.after_effect("rename-window"))?;
        Ok(self)
    }

    /// Make this window active in the session it was reached through.
    ///
    /// Targeted by id rather than by index. An index is a slot, not an
    /// identity: anything that renumbers windows -- breaking a lone pane out
    /// of one moves it to a free index -- leaves a handle's cached index
    /// pointing at whatever occupies that slot now, which is a different live
    /// window rather than nothing. tmux answers such a target without
    /// complaint, so an index-scoped select would switch to the wrong window
    /// and report success.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the command, and
    /// [`Error::ObjectGone`] when the window is no longer on the server.
    pub async fn select(&mut self) -> Result<&mut Self, Error> {
        listing::mutate(
            &self.core,
            "select-window",
            Command::new("select-window")
                .arg("-t")
                .arg(self.id().to_string()),
        )
        .await?;

        self.refresh()
            .await
            .map_err(|error| error.after_effect("select-window"))?;
        Ok(self)
    }

    /// Set the window's pane layout.
    ///
    /// Takes a [`Layout`] tmux knows by name, or a layout string tmux itself
    /// produced through [`LayoutSpec::Saved`]. A `&str`, `String`, or
    /// `OsString` is read as a saved layout, so a caller who already had one
    /// keeps working. Strings also accept unique named-layout abbreviations.
    /// Checksums and nonempty trees are checked before dispatch; tmux owns
    /// geometry correction and pruning. Nesting is limited to 256 groups.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidLayout`] for unsafe syntax or an ambiguous name,
    /// an error when tmux refuses the layout, and
    /// [`crate::ErrorKind::UnsupportedVersion`] when a named layout needs a
    /// newer tmux than this endpoint runs. See [`Layout::minimum_release`].
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// use libtmux::Layout;
    ///
    /// let guard = libtmux::test::TestServer::new().await?;
    /// let session = guard.server().new_session("arranged").await?;
    /// let mut window = session.active_window().await?.expect("a window");
    ///
    /// window.select_layout(Layout::Tiled).await?;
    ///
    /// guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn select_layout(
        &mut self,
        layout: impl Into<LayoutSpec>,
    ) -> Result<&mut Self, Error> {
        let layout = layout.into();
        let argument = match &layout {
            LayoutSpec::Named(named) => {
                crate::Server::from_core(Arc::clone(&self.core))
                    .require(named.as_str(), named.minimum_release())
                    .await?;
                OsString::from(named.as_str())
            }
            LayoutSpec::Saved(saved) => {
                Self::validate_saved_layout(&self.core.format_version().await?, saved)?
            }
        };
        crate::Server::from_core(Arc::clone(&self.core))
            .validate_layouts([(argument.as_os_str(), 1)])
            .await?;

        listing::mutate(
            &self.core,
            "select-layout",
            Command::new("select-layout")
                .arg("-t")
                .arg(self.id().to_string())
                .arg("--")
                .arg(argument),
        )
        .await?;

        self.refresh()
            .await
            .map_err(|error| error.after_effect("select-layout"))?;
        Ok(self)
    }

    /// Validate a saved layout string before it reaches tmux, and return the
    /// argument to send.
    ///
    /// The guard [`Self::select_layout`] applies to a [`LayoutSpec::Saved`]
    /// value, factored out so every caller that dispatches `select-layout`
    /// with a caller-supplied string goes through the same refusal rather
    /// than reimplementing it: [`crate::plan::ops::SelectLayout`] validates
    /// each recorded operation this way before a plan's first command, and
    /// an MCP or other integration should call this (or [`Self::select_layout`]
    /// directly, when it already holds a [`Window`]) rather than building the
    /// `select-layout` command itself.
    ///
    /// # Errors
    ///
    /// Returns [`Error::UnrecognizedLayout`] for a value that is not a
    /// preset name, a unique preset prefix, a classic layout string, or
    /// JSON; [`Error::AmbiguousLayout`] for a prefix that names more than
    /// one preset available on the running release; and
    /// [`crate::ErrorKind::UnsupportedVersion`] for a preset or a JSON
    /// layout this release predates.
    pub(crate) fn validate_saved_layout(
        version: &crate::TmuxVersion,
        saved: &OsStr,
    ) -> Result<OsString, Error> {
        // Checked here rather than left to tmux: 3.3 and 3.3a exit on a
        // value `select-layout` cannot parse, taking every session on the
        // socket with them, and `--` does not help -- it turns `-o` from
        // the undo flag into exactly such a value.
        match SavedLayout::classify(saved, version) {
            SavedLayout::Preset(named) => {
                version.require(named.as_str(), named.minimum_release())?;
                // The resolved name, not whatever prefix the caller spelled:
                // a caller who typed `tile` gets `tiled` sent, not a second
                // round of tmux's own matching.
                Ok(OsString::from(named.as_str()))
            }
            SavedLayout::Classic => Ok(saved.to_owned()),
            SavedLayout::Json => {
                version.require("a JSON layout string", crate::version::since::JSON_LAYOUTS)?;
                Ok(saved.to_owned())
            }
            SavedLayout::Ambiguous(candidates) => Err(Error::AmbiguousLayout {
                input: saved.to_string_lossy().into_owned(),
                candidates,
            }),
            SavedLayout::Unrecognized => Err(Error::UnrecognizedLayout),
        }
    }

    /// Restart the window's command in place.
    ///
    /// Passing `None` reruns whatever the window started with. Every pane in
    /// the window is replaced by the one the command runs in, which is what
    /// makes this different from respawning a pane: [`crate::Pane::respawn`]
    /// restarts one pane and leaves its neighbours alone.
    ///
    /// # Errors
    ///
    /// Returns an error when a pane is still running and `kill` is not set.
    pub async fn respawn(
        &mut self,
        command: Option<impl Into<OsString>>,
        respawn_mode: Respawn,
    ) -> Result<&mut Self, Error> {
        let mut respawn = Command::new("respawn-window")
            .arg("-t")
            .arg(self.id().to_string());
        if respawn_mode.kills() {
            respawn = respawn.arg("-k");
        }
        if let Some(command) = command {
            respawn = respawn.arg("--").arg(command.into());
        }

        listing::mutate(&self.core, "respawn-window", respawn).await?;
        self.refresh()
            .await
            .map_err(|error| error.after_effect("respawn-window"))?;
        Ok(self)
    }

    /// Move to the next named layout, and return the window.
    ///
    /// tmux steps through its own list rather than taking a name, so this is a
    /// flag on `select-layout` and not something [`Window::select_layout`]
    /// could express.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the command.
    pub async fn next_layout(&mut self) -> Result<&mut Self, Error> {
        self.step_layout("-n").await
    }

    /// Move to the previous named layout, and return the window.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the command.
    pub async fn previous_layout(&mut self) -> Result<&mut Self, Error> {
        self.step_layout("-p").await
    }

    /// Step through tmux's layout list in one direction.
    async fn step_layout(&mut self, flag: &'static str) -> Result<&mut Self, Error> {
        listing::mutate(
            &self.core,
            "select-layout",
            Command::new("select-layout")
                .arg(flag)
                .arg("-t")
                .arg(self.id().to_string()),
        )
        .await?;

        self.refresh()
            .await
            .map_err(|error| error.after_effect("select-layout"))?;
        Ok(self)
    }

    /// Move the window's panes round one position, keeping the layout.
    ///
    /// The panes swap places within the arrangement rather than the
    /// arrangement changing, so a rotation of a three-pane window three times
    /// returns it to where it started.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux cannot be reached or refuses the rotation.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// use libtmux::{Rotation, SplitDirection, SplitOptions};
    ///
    /// let guard = libtmux::test::TestServer::new().await?;
    /// let session = guard.server().new_session("turning").await?;
    /// let mut window = session.active_window().await?.expect("a window");
    /// window.split(SplitOptions::new(SplitDirection::Below)).await?;
    ///
    /// let before: Vec<_> = window.panes().await?.iter().map(|p| p.id().clone()).collect();
    /// window.rotate(Rotation::Up).await?;
    /// let after: Vec<_> = window.panes().await?.iter().map(|p| p.id().clone()).collect();
    ///
    /// assert_ne!(before, after, "the panes moved");
    /// window.rotate(Rotation::Down).await?;
    /// let back: Vec<_> = window.panes().await?.iter().map(|p| p.id().clone()).collect();
    /// assert_eq!(before, back, "and the other way undoes it");
    ///
    /// guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn rotate(&self, rotation: Rotation) -> Result<(), Error> {
        listing::mutate(
            &self.core,
            "rotate-window",
            Command::new("rotate-window")
                .arg(rotation.flag())
                .arg("-t")
                .arg(self.id().to_string()),
        )
        .await
    }

    /// Kill the window, closing it in every session that links it.
    ///
    /// This consumes the handle. Use [`Window::unlink`] to remove only this
    /// session's link while leaving the window alive elsewhere.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the command.
    pub async fn kill(self) -> Result<(), Error> {
        listing::mutate(
            &self.core,
            "kill-window",
            Command::new("kill-window")
                .arg("-t")
                .arg(self.id().to_string()),
        )
        .await
    }

    /// Remove this session's link to the window, leaving other links intact.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the command, which includes
    /// unlinking a window that only one session holds.
    pub async fn unlink(self) -> Result<(), Error> {
        // `session:id` rather than `session:index`. A link is what this
        // removes, so the session half is needed; the window half is an
        // identity, and a cached index stops being one the moment anything
        // renumbers the session.
        let Err(error) = listing::mutate(
            &self.core,
            "unlink-window",
            Command::new("unlink-window").arg("-t").arg(format!(
                "{}:{}",
                self.session_id(),
                self.id()
            )),
        )
        .await
        else {
            return Ok(());
        };

        Err(link_or_object_gone(&self.core, error, &[(self.session_id(), self.id())]).await)
    }

    /// Run a raw tmux command against this window.
    ///
    /// The escape hatch for anything this crate does not wrap. `-t` is placed
    /// after the subcommand for you, because tmux stops reading flags at the
    /// first positional: a target appended after one is taken as text, and the
    /// command then succeeds having acted on something else.
    ///
    /// A non-zero exit status comes back in the [`CommandResult`] rather than
    /// as an error, the same as [`crate::Server::cmd`].
    ///
    /// # Errors
    ///
    /// Returns an error when the process cannot be started, captured, or
    /// awaited.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// let guard = libtmux::test::TestServer::new().await?;
    /// let session = guard.server().new_session("raw").await?;
    /// let window = session.active_window().await?.expect("a window");
    ///
    /// let result = window
    ///     .cmd(libtmux::Command::new("display-message").arg("-p").arg("#{window_id}"))
    ///     .await?;
    /// assert_eq!(result.stdout_lossy().trim(), window.id().to_string());
    ///
    /// guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn cmd(&self, command: Command) -> Result<CommandResult, Error> {
        self.core
            .execute(command.targeting(self.id().to_string()))
            .await
    }

    /// Expand a tmux format string in this window's context.
    ///
    /// This returns `display-message` output instead of showing it in front of
    /// a person with [`Self::display`]. See [`crate::Server::format`] for the
    /// shell boundary around command and recursive formats.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux cannot be reached or refuses the format.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// let guard = libtmux::test::TestServer::new().await?;
    /// let session = guard.server().new_session("shown").await?;
    /// let window = session.active_window().await?.expect("a window");
    ///
    /// let expanded = window.format("#{window_id}").await?;
    /// assert_eq!(expanded.to_string_lossy(), window.id().to_string());
    ///
    /// guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn format(&self, template: &str) -> Result<TmuxText, Error> {
        let result = self
            .cmd(
                Command::new("display-message")
                    .arg("-p")
                    .arg("--")
                    .arg(OsString::from(template)),
            )
            .await?;
        if !result.success() {
            return Err(Error::from_refused_result(
                "display-message",
                &result,
                Some(OsStr::new(&self.id().to_string())),
            ));
        }

        // `-p` ends its output with a newline that frames it rather than
        // belonging to the expansion.
        let stdout = result.stdout();
        Ok(TmuxText::from(
            stdout.strip_suffix(b"\n").unwrap_or(stdout).to_vec(),
        ))
    }

    /// Show a message on the clients viewing this window.
    ///
    /// The showing half of tmux's `display-message`. Nothing is returned
    /// because nothing is read: use [`Self::format`] to get text back.
    /// Succeeds when nobody is watching, having shown it to nobody.
    /// Like [`Self::format`], this interprets the message as a tmux format;
    /// command and recursive formats can run shell commands.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux cannot be reached or refuses the message.
    pub async fn display(&self, message: &str) -> Result<(), Error> {
        let result = self
            .cmd(
                Command::new("display-message")
                    .arg("--")
                    .arg(OsString::from(message)),
            )
            .await?;
        if result.success() {
            return Ok(());
        }

        Err(Error::from_refused_result(
            "display-message",
            &result,
            Some(OsStr::new(&self.id().to_string())),
        ))
    }

    /// Create a pane, run an operation with it, then kill it.
    ///
    /// [`crate::ScopeError`] retains creation, operation and cleanup failures
    /// separately. If operation and cleanup both fail, both original errors
    /// are returned. Cleanup errors carry [`Error::AfterEffect`] because
    /// creation succeeded.
    ///
    /// # Errors
    ///
    /// Returns [`crate::ScopeError`] when creation, the operation, or cleanup
    /// fails. The operation's error needs no conversion into [`Error`].
    ///
    /// # Cancel safety
    ///
    /// Nothing is left behind. Once polled, creation and cleanup run in tasks
    /// of their own, so the pane is killed even if this future is dropped or
    /// the operation panics, while the Tokio runtime is alive. A cleanup
    /// failure then has no caller to reach; the `tracing` feature records it.
    pub async fn with_pane<T, E>(
        &self,
        options: impl Into<SplitOptions>,
        operation: impl AsyncFnOnce(&Pane) -> Result<T, E>,
    ) -> Result<T, crate::ScopeError<T, E>> {
        let window = self.clone();
        let options = options.into();
        scoped::run(
            "with-pane",
            async move { window.split(options).await },
            Pane::kill,
            operation,
        )
        .await
    }

    /// Swap this window's position with another.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ServerMismatch`] when `other` belongs to another
    /// server, or an error when tmux refuses the swap.
    pub async fn swap_with(&mut self, other: &Self) -> Result<&mut Self, Error> {
        self.core
            .require_same_server(other.server_identity(), "swap-window")?;
        if let Err(error) = listing::mutate(
            &self.core,
            "swap-window",
            Command::new("swap-window")
                .arg("-s")
                .arg(self.link_target())
                .arg("-t")
                // `session:id`, not `session:index`: the session half says
                // which of the target's links to swap, and the id half cannot
                // go stale under a renumber the way a cached index does.
                .arg(format!("{}:{}", other.session_id(), other.id())),
        )
        .await
        {
            // Either half can be the one tmux could not resolve, and a window
            // still linked elsewhere is not a window that is gone.
            return Err(link_or_object_gone(
                &self.core,
                error,
                &[
                    (self.session_id(), self.id()),
                    (other.session_id(), other.id()),
                ],
            )
            .await);
        }

        self.refresh_in(other.session_id())
            .await
            .map_err(|error| error.after_effect("swap-window"))?;
        Ok(self)
    }

    /// Move this window to an index, possibly in another session.
    ///
    /// The destination is a session and an index rather than a target string,
    /// because those are the two things tmux needs and a string could express
    /// neither or both.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ServerMismatch`] when `session` belongs to another
    /// server, or an error when the destination is occupied or absent.
    pub async fn move_to(&mut self, session: &Session, index: i32) -> Result<&mut Self, Error> {
        self.core
            .require_same_server(session.server_identity(), "move-window")?;
        listing::mutate(
            &self.core,
            "move-window",
            Command::new("move-window")
                .arg("-s")
                .arg(self.link_target())
                .arg("-t")
                .arg(format!("{}:{index}", session.id())),
        )
        .await?;

        self.refresh_in(session.id())
            .await
            .map_err(|error| error.after_effect("move-window"))?;
        Ok(self)
    }

    /// Resize the window to an exact size in cells.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the size.
    /// Move one edge of the window by a number of cells.
    ///
    /// This is the form a keybinding uses: "make it two columns wider" rather
    /// than a size computed from the current one.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the resize.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// # let guard = libtmux::test::TestServer::new().await?;
    /// # let server = guard.server();
    /// use libtmux::ResizeDirection;
    ///
    /// let session = server.new_session("resized-window").await?;
    /// let mut window = session.windows().await?.remove(0);
    ///
    /// window.resize(80, 24).await?;
    /// window.resize_by(ResizeDirection::Down, 4).await?;
    /// assert_eq!(window.height(), 28);
    /// # guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn resize_by(
        &mut self,
        direction: ResizeDirection,
        cells: u32,
    ) -> Result<&mut Self, Error> {
        listing::mutate(
            &self.core,
            "resize-window",
            Command::new("resize-window")
                .arg("-t")
                .arg(self.id().to_string())
                .arg(direction.flag())
                .arg(cells.to_string()),
        )
        .await?;

        self.refresh()
            .await
            .map_err(|error| error.after_effect("resize-window"))?;
        Ok(self)
    }

    /// Link this window into another session, so both hold the same window.
    ///
    /// A linked window is one window with two winlinks, not a copy: renaming
    /// it or splitting it shows up in both sessions. [`Window::unlink`] takes
    /// one link away again.
    ///
    /// `index` places the link at a window index, or appends when `None`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ServerMismatch`] when `session` belongs to another
    /// server, or an error when tmux refuses the link, including an occupied
    /// index.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// # let guard = libtmux::test::TestServer::new().await?;
    /// # let server = guard.server();
    /// let source = server.new_session("linking-from").await?;
    /// let target = server.new_session("linking-to").await?;
    /// let window = source.windows().await?.remove(0);
    ///
    /// window.link_to(&target, None).await?;
    ///
    /// let linked = target.windows().await?;
    /// assert!(linked.iter().any(|other| other.id() == window.id()));
    /// # guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn link_to(&self, session: &Session, index: Option<i32>) -> Result<(), Error> {
        self.core
            .require_same_server(session.server_identity(), "link-window")?;
        let target = index.map_or_else(
            || session.id().to_string(),
            |index| format!("{}:{index}", session.id()),
        );

        listing::mutate(
            &self.core,
            "link-window",
            Command::new("link-window")
                .arg("-s")
                .arg(self.link_target())
                .arg("-t")
                .arg(target),
        )
        .await?;

        Ok(())
    }

    /// Resize the window to an exact size in cells.
    ///
    /// Use [`Window::resize_by`] to move one edge instead.
    ///
    /// # Errors
    ///
    /// Returns an error when tmux refuses the size.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// # let guard = libtmux::test::TestServer::new().await?;
    /// # let server = guard.server();
    /// let session = server.new_session("sized").await?;
    /// let mut window = session.windows().await?.remove(0);
    ///
    /// window.resize(100, 40).await?;
    /// assert_eq!((window.width(), window.height()), (100, 40));
    /// # guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn resize(&mut self, width: u32, height: u32) -> Result<&mut Self, Error> {
        listing::mutate(
            &self.core,
            "resize-window",
            Command::new("resize-window")
                .arg("-t")
                .arg(self.id().to_string())
                .arg("-x")
                .arg(width.to_string())
                .arg("-y")
                .arg(height.to_string()),
        )
        .await?;

        self.refresh()
            .await
            .map_err(|error| error.after_effect("resize-window"))?;
        Ok(self)
    }
}

/// Windows compare by server endpoint, session, index, and window id.
///
/// Equality follows the link, not the window, because a linked window occupies
/// a genuinely different position in each session that holds it. Compare
/// [`Window::id`] directly to ask whether two handles name the same underlying
/// window.
impl PartialEq for Window {
    fn eq(&self, other: &Self) -> bool {
        self.server_identity() == other.server_identity()
            && self.projection.link().identity() == other.projection.link().identity()
    }
}

impl Eq for Window {}

impl Hash for Window {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.server_identity().hash(state);
        self.projection.link().identity().hash(state);
    }
}

/// Renders identity only, never snapshot text.
impl fmt::Debug for Window {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Window")
            .field("id", &self.id())
            .field("session_id", &self.session_id())
            .field("index", &self.index())
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "query")]
impl Window {
    /// Read one field of this window's snapshot, named by the handle that
    /// filters it.
    ///
    /// Every field in [`WindowFields`] reads this way, including those with no
    /// getter of their own. Nothing is sent to tmux, so the value is as old as
    /// the snapshot. The result says why a field holds no value: see
    /// [`Availability`].
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
    /// # runtime.block_on(async {
    /// use libtmux::query::Filterable as _;
    /// use libtmux::{Availability, Window};
    ///
    /// let guard = libtmux::test::TestServer::new().await?;
    /// let session = guard.server().new_session("read").await?;
    /// let window = session.active_window().await?.expect("a session has a window");
    /// let fields = Window::filter_fields();
    ///
    /// // The layout as shown, which only differs from the layout while zoomed.
    /// assert_eq!(
    ///     window.get(fields.window_visible_layout),
    ///     Availability::Available(window.layout()),
    /// );
    /// assert_eq!(window.get(fields.window_id), Availability::Available(window.id()));
    ///
    /// guard.shutdown().await?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// # })?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn get<F: ReadField<Self>>(&self, field: F) -> Availability<F::Value<'_>> {
        field.__read(self).unwrap_or(Availability::Absent)
    }

    /// Return the stored field tmux names `name`.
    pub(crate) fn stored(&self, name: &str) -> Option<Availability<FieldRef<'_>>> {
        self.projection.window().stored(name)
    }
}

/// Filtering a window uses the same handles as the snapshot beneath it.
///
/// Matching and validation delegate to that snapshot, so an expression can
/// only name fields the catalog knows. The companion is re-parameterized to
/// [`Window`] so the type a listing returns is the type an expression
/// filters.
#[cfg(feature = "query")]
impl Filterable for Window {
    type Fields = WindowFields<Self>;

    const FILTER_TARGET: &'static str = <WindowInfo as Filterable>::FILTER_TARGET;

    fn filter_fields() -> Self::Fields {
        Self::Fields::for_target(Self::FILTER_TARGET)
    }

    fn __filter_matches(&self, predicate: &crate::query::__private::Predicate) -> bool {
        self.projection.window().__filter_matches(predicate)
    }

    fn __filter_validate(
        predicate: &crate::query::__private::Predicate,
    ) -> Result<(), crate::query::FilterExpressionError> {
        <WindowInfo as Filterable>::__filter_validate(predicate)
    }
}

#[cfg(feature = "query")]
impl FilterSchema for Window {
    fn __filter_schema() -> crate::query::__private::FilterSchemaDescriptor {
        <WindowInfo as FilterSchema>::__filter_schema()
    }
}

/// Where a split puts the new pane, relative to the one being divided.
///
/// tmux spells these `-v`, `-v -b`, `-h`, and `-h -b`, where "horizontal"
/// means side by side. Naming the resulting position instead removes a
/// question every tmux user has asked at least once.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::{Rotation, SplitDirection};
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("work").await?;
/// let window = session.active_window().await?.expect("a session has a window");
/// window.split(SplitDirection::Below).await?;
///
/// // Rotating moves panes between positions; it does not create or destroy any.
/// let before = window.panes().await?.len();
/// window.rotate(Rotation::Down).await?;
/// assert_eq!(window.panes().await?.len(), before);
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Rotation {
    /// Move each pane to the position before it, so the first becomes last.
    Up,
    /// Move each pane to the position after it, which is tmux's default.
    Down,
}

impl Rotation {
    /// The tmux flag that asks for this direction.
    const fn flag(self) -> &'static str {
        match self {
            Self::Up => "-U",
            Self::Down => "-D",
        }
    }
}

/// One of the pane arrangements tmux knows by name.
///
/// tmux has exactly these, so a layout that does not exist is a compile error
/// rather than a refusal at the far end of a round trip. A saved layout
/// string is a different thing and goes through [`LayoutSpec::Saved`].
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::Layout;
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("arranged").await?;
/// let mut window = session.active_window().await?.expect("a window");
///
/// window.select_layout(Layout::EvenHorizontal).await?;
/// assert_eq!(Layout::EvenHorizontal.as_str(), "even-horizontal");
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Layout {
    /// Panes side by side, each the full height.
    EvenHorizontal,
    /// Panes stacked, each the full width.
    EvenVertical,
    /// One large pane above a row of the rest.
    MainHorizontal,
    /// The arrangement of [`Layout::MainHorizontal`] with the large pane
    /// below the row rather than above it.
    MainHorizontalMirrored,
    /// One large pane beside a column of the rest.
    MainVertical,
    /// The arrangement of [`Layout::MainVertical`] with the large pane on
    /// the right rather than the left.
    MainVerticalMirrored,
    /// Panes in as even a grid as their count allows.
    Tiled,
}

impl Layout {
    /// The name tmux knows this layout by.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::EvenHorizontal => "even-horizontal",
            Self::EvenVertical => "even-vertical",
            Self::MainHorizontal => "main-horizontal",
            Self::MainHorizontalMirrored => "main-horizontal-mirrored",
            Self::MainVertical => "main-vertical",
            Self::MainVerticalMirrored => "main-vertical-mirrored",
            Self::Tiled => "tiled",
        }
    }

    /// Whether this release's tree carries the preset.
    ///
    /// `has_behavior`, not `meets`: a development identifier carries what its
    /// release number implies, and clamping one to the crate's floor hides a
    /// preset the running tmux has.
    pub(crate) fn is_in(self, version: &crate::TmuxVersion) -> bool {
        version.has_behavior(&self.minimum_release())
    }

    /// The first tmux release that arranges panes this way.
    ///
    /// The mirrored pair arrived in 3.5; the rest predate everything this
    /// crate supports.
    #[must_use]
    pub const fn minimum_release(self) -> crate::ReleaseVersion {
        match self {
            Self::MainHorizontalMirrored | Self::MainVerticalMirrored => {
                crate::version::since::MIRRORED_LAYOUTS
            }
            _ => crate::TmuxVersion::MIN_SUPPORTED,
        }
    }
}

impl fmt::Display for Layout {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// What to arrange a window's panes as.
///
/// A [`Layout`] names an arrangement tmux computes. A saved string is one
/// tmux already computed: [`Window::layout`] reports one, and handing it back
/// restores the pane sizes, which a named layout cannot express.
///
/// From [`crate::since::JSON_LAYOUTS`] (3.8) a saved string carries each
/// pane's id, so the restored arrangement is byte-exact, process for process.
/// Below that release the classic checksum-prefixed string carries only
/// sizes and positions: the shape returns, but which pane lands in which cell
/// depends on whether the live pane list happens to already be in the saved
/// cell order, which a mirrored or otherwise asymmetric layout can defeat. A
/// saved string from 3.8+ is refused below it with
/// [`crate::ErrorKind::UnsupportedVersion`], and a value that is none of a
/// preset name, a classic string, or JSON with
/// [`crate::ErrorKind::InvalidInput`], both before dispatch.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::Layout;
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("restored").await?;
/// let mut window = session.active_window().await?.expect("a window");
///
/// // Keep what tmux reports, rearrange, then put it back exactly.
/// let before = window.layout().to_owned();
/// window.select_layout(Layout::EvenVertical).await?;
/// window.select_layout(&before).await?;
/// assert_eq!(window.layout().as_bytes(), before.as_bytes());
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LayoutSpec {
    /// An arrangement tmux knows by name.
    Named(
        /// The preset used to compute a new pane arrangement.
        Layout,
    ),
    /// A layout string tmux produced, restoring pane sizes exactly.
    Saved(
        /// Layout text to restore, subject to format and tmux version checks.
        OsString,
    ),
}

impl From<Layout> for LayoutSpec {
    fn from(layout: Layout) -> Self {
        Self::Named(layout)
    }
}

impl From<OsString> for LayoutSpec {
    fn from(saved: OsString) -> Self {
        Self::Saved(saved)
    }
}

impl From<String> for LayoutSpec {
    fn from(saved: String) -> Self {
        Self::Saved(saved.into())
    }
}

impl From<&str> for LayoutSpec {
    fn from(saved: &str) -> Self {
        Self::Saved(saved.into())
    }
}

impl From<&OsStr> for LayoutSpec {
    fn from(saved: &OsStr) -> Self {
        Self::Saved(saved.to_owned())
    }
}

impl From<&TmuxText> for LayoutSpec {
    /// Take a layout straight from [`Window::layout`].
    ///
    /// tmux writes a layout as printable ASCII, and this keeps the bytes
    /// rather than the lossy text either way, so a layout that round-trips
    /// through a handle is the one tmux produced.
    fn from(saved: &TmuxText) -> Self {
        #[cfg(unix)]
        {
            use std::os::unix::ffi::OsStringExt as _;

            Self::Saved(OsString::from_vec(saved.as_bytes().to_vec()))
        }
        #[cfg(not(unix))]
        {
            Self::Saved(OsString::from(saved.to_string_lossy().into_owned()))
        }
    }
}

/// What to do about a process still running where one is being respawned.
///
/// tmux refuses `respawn-pane` and `respawn-window` outright while the old
/// command is alive unless `-k` says otherwise, so the choice is not a detail
/// -- it decides whether a live process is killed.
///
/// # Examples
///
/// ```no_run
/// # async fn restart(pane: &mut libtmux::Pane) -> Result<(), libtmux::Error> {
/// use libtmux::Respawn;
///
/// // Rerun the pane's own command, killing it first if it is still running.
/// pane.respawn(None::<&str>, Respawn::Replacing).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Respawn {
    /// Kill whatever is running first. tmux's `-k`.
    Replacing,
    /// Refuse unless the pane or window is already dead.
    OnlyIfDead,
}

impl Respawn {
    pub(crate) const fn kills(self) -> bool {
        matches!(self, Self::Replacing)
    }
}

/// The shape of a saved layout value, read before it reaches tmux.
#[cfg_attr(test, derive(Debug, PartialEq))]
enum SavedLayout {
    /// A preset name, or a prefix of exactly one, passed as text rather than
    /// as a [`Layout`].
    Preset(Layout),
    /// tmux's checksum-prefixed string: four hex digits and a comma.
    Classic,
    /// The JSON form tmux reports from [`crate::since::JSON_LAYOUTS`].
    Json,
    /// A prefix that names more than one preset available on the running
    /// release.
    Ambiguous(Vec<&'static str>),
    /// Nothing tmux ever reported, and nothing `select-layout` can parse.
    Unrecognized,
}

impl SavedLayout {
    /// Every preset [`select_layout`](Window::select_layout) knows, in the
    /// order tmux itself declares them.
    const PRESETS: [Layout; 7] = [
        Layout::EvenHorizontal,
        Layout::EvenVertical,
        Layout::MainHorizontal,
        Layout::MainHorizontalMirrored,
        Layout::MainVertical,
        Layout::MainVerticalMirrored,
        Layout::Tiled,
    ];

    fn classify(saved: &OsStr, version: &crate::TmuxVersion) -> Self {
        let Some(text) = saved.to_str() else {
            return Self::Unrecognized;
        };
        if let Some(named) = Self::PRESETS
            .into_iter()
            .filter(|named| named.is_in(version))
            .find(|named| named.as_str() == text)
        {
            return Self::Preset(named);
        }
        // tmux's own `layout_set_lookup` is a prefix match, so `tile` and
        // `even-h` both apply on every release; matched only against the
        // presets the running release actually has, so `main-h` is unique
        // on 3.2a (five presets) even though it is ambiguous from 3.5,
        // where the mirrored pair exists. An empty string matches every
        // preset's prefix and must stay unrecognized rather than reading as
        // ambiguous among all seven.
        if !text.is_empty() {
            let candidates: Vec<Layout> = Self::PRESETS
                .into_iter()
                .filter(|named| named.is_in(version))
                .filter(|named| named.as_str().starts_with(text))
                .collect();
            match candidates.as_slice() {
                [only] => return Self::Preset(*only),
                [_, ..] => {
                    return Self::Ambiguous(
                        candidates.iter().map(|named| named.as_str()).collect(),
                    );
                }
                [] => {}
            }
        }
        // `layout_parse` reads `%hx,` and insists it consumed exactly five
        // bytes; tmux itself always writes the checksum as four lowercase
        // digits.
        let bytes = text.as_bytes();
        if bytes.len() > 5 && bytes[..4].iter().all(u8::is_ascii_hexdigit) && bytes[4] == b',' {
            return Self::Classic;
        }
        if text.starts_with('{') {
            return Self::Json;
        }
        Self::Unrecognized
    }
}

/// Where a split puts the pane it makes.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::SplitDirection;
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("work").await?;
/// let window = session.active_window().await?.expect("a session has a window");
///
/// // The direction names where the *new* pane lands, not which edge moves.
/// window.split(SplitDirection::Right).await?;
/// window.split(SplitDirection::Below).await?;
/// assert_eq!(window.panes().await?.len(), 3);
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SplitDirection {
    /// Above the pane being divided.
    Above,
    /// Below it, which is tmux's default.
    Below,
    /// To its left.
    Left,
    /// To its right.
    Right,
}

impl SplitDirection {
    /// Return the tmux flags that produce this position.
    pub(crate) const fn flags(self) -> (&'static str, bool) {
        (if self.is_vertical() { "-v" } else { "-h" }, self.before())
    }

    /// Whether this divides a pane top from bottom rather than side to side.
    pub(crate) const fn is_vertical(self) -> bool {
        matches!(self, Self::Above | Self::Below)
    }

    /// Whether tmux needs `-b` to put the new pane on this side.
    pub(crate) const fn before(self) -> bool {
        matches!(self, Self::Above | Self::Left)
    }
}

/// Where a pane lands when it is moved into another window.
///
/// [`Pane::break_out`] takes a pane out into a window of its own and this puts
/// one back, so between them a pane can be moved anywhere. tmux spawns nothing
/// here, which is why this carries none of the command, directory, or
/// environment a [`SplitOptions`] does: the pane already exists and keeps
/// everything running in it.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::{JoinOptions, SplitDirection};
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("moving").await?;
/// let window = session.active_window().await?.expect("a window");
/// let second = session.new_window("elsewhere").await?;
/// let stranded = second.panes().await?.remove(0);
///
/// // Put a pane from the other window beside this one.
/// let here = window.panes().await?.remove(0);
/// let moved = stranded
///     .join_into(&here, JoinOptions::new(SplitDirection::Below))
///     .await?;
/// assert_eq!(moved.window_id(), window.id());
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct JoinOptions {
    direction: SplitDirection,
    size: Option<PaneSize>,
    full: bool,
}

impl JoinOptions {
    /// Land the pane in one direction from the pane it joins.
    #[must_use]
    pub const fn new(direction: SplitDirection) -> Self {
        Self {
            direction,
            size: None,
            full: false,
        }
    }

    /// Ask for a size rather than letting tmux halve the space.
    #[must_use]
    pub const fn size(mut self, size: PaneSize) -> Self {
        self.size = Some(size);
        self
    }

    /// Span the window rather than only the pane being joined.
    #[must_use]
    pub const fn full(mut self) -> Self {
        self.full = true;
        self
    }

    /// Add this placement to a `join-pane` command.
    pub(crate) fn apply(self, command: Command) -> Command {
        let (axis, before) = self.direction.flags();
        let mut command = command.arg(axis);
        if before {
            command = command.arg("-b");
        }
        if self.full {
            command = command.arg("-f");
        }
        if let Some(size) = self.size {
            command = command.arg("-l").arg(size.to_string());
        }
        command
    }
}

/// How much space a new pane gets.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::{PaneSize, SplitDirection, SplitOptions};
///
/// // A size renders as tmux's own `-l` argument, cells bare and shares suffixed.
/// assert_eq!(PaneSize::Cells(20).to_string(), "20");
/// assert_eq!(PaneSize::Percent(25).to_string(), "25%");
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("work").await?;
/// let window = session.active_window().await?.expect("a session has a window");
///
/// let pane = window
///     .split(SplitOptions::new(SplitDirection::Below).size(PaneSize::Percent(25)))
///     .await?;
/// assert!(pane.height() > 0);
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PaneSize {
    /// A number of rows or columns, depending on the split direction.
    Cells(
        /// Requested row or column count, according to the split direction.
        u32,
    ),
    /// A share of the space being divided, as a percentage.
    Percent(
        /// Requested share of the space being divided, in whole percent.
        u32,
    ),
}

impl fmt::Display for PaneSize {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Cells(cells) => write!(formatter, "{cells}"),
            Self::Percent(percent) => write!(formatter, "{percent}%"),
        }
    }
}

/// Which edge a resize moves.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::ResizeDirection;
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("work").await?;
/// let mut window = session.active_window().await?.expect("a session has a window");
///
/// // The direction names the edge that moves, so `Down` makes a window taller
/// // rather than shorter.
/// let before = window.height();
/// window.resize_by(ResizeDirection::Down, 3).await?;
/// assert!(window.height() >= before);
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ResizeDirection {
    /// Move the top edge up, making it taller.
    Up,
    /// Move the bottom edge down.
    Down,
    /// Move the left edge left.
    Left,
    /// Move the right edge right.
    Right,
}

impl ResizeDirection {
    /// Return the tmux flag for this direction.
    pub(crate) const fn flag(self) -> &'static str {
        match self {
            Self::Up => "-U",
            Self::Down => "-D",
            Self::Left => "-L",
            Self::Right => "-R",
        }
    }
}

/// Options for splitting a window or pane into a new pane.
///
/// A bare [`SplitDirection`] is accepted wherever this is:
/// `window.split(SplitDirection::Below)`.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// # runtime.block_on(async {
/// use libtmux::{SplitDirection, SplitOptions};
///
/// let guard = libtmux::test::TestServer::new().await?;
/// let session = guard.server().new_session("split").await?;
/// let window = session.active_window().await?.expect("a session has a window");
///
/// // Splitting leaves focus where it was unless asked otherwise, so a caller
/// // that wants the new pane selected says so.
/// let pane = window
///     .split(SplitOptions::new(SplitDirection::Right).select())
///     .await?;
/// assert_eq!(window.active_pane().await?.map(|active| active.id().to_string()),
///            Some(pane.id().to_string()));
///
/// guard.shutdown().await?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # })?;
/// # Ok(())
/// # }
/// ```
#[must_use = "options describe a split but do not perform one"]
#[derive(Clone)]
pub struct SplitOptions {
    direction: SplitDirection,
    start_directory: Option<TmuxArg>,
    command: Option<OsString>,
    size: Option<PaneSize>,
    environment: Vec<(OsString, OsString)>,
    full: bool,
    zoom: bool,
    select: bool,
}

impl fmt::Debug for SplitOptions {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("SplitOptions")
            .field("direction", &self.direction)
            .field("has_start_directory", &self.start_directory.is_some())
            .field("has_command", &self.command.is_some())
            .field("size", &self.size)
            .field("environment_count", &self.environment.len())
            .field("full", &self.full)
            .field("zoom", &self.zoom)
            .field("select", &self.select)
            .finish()
    }
}

impl SplitOptions {
    /// Describe a split placing the new pane in one direction.
    pub const fn new(direction: SplitDirection) -> Self {
        Self {
            direction,
            start_directory: None,
            command: None,
            size: None,
            environment: Vec::new(),
            full: false,
            zoom: false,
            select: false,
        }
    }

    /// Set the new pane's working directory.
    ///
    /// The directory is sent literally. [`TmuxArg::format`] opts into
    /// expansion, which is how `#{pane_current_path}` is asked for.
    pub fn start_directory(mut self, directory: impl Into<TmuxArg>) -> Self {
        self.start_directory = Some(directory.into());
        self
    }

    /// Run a command instead of the default shell.
    pub fn command(mut self, command: impl Into<OsString>) -> Self {
        self.command = Some(command.into());
        self
    }

    /// Give the new pane a size, in cells or as a share of the space.
    pub const fn size(mut self, size: PaneSize) -> Self {
        self.size = Some(size);
        self
    }

    /// Set an environment variable for the process the new pane starts.
    ///
    /// Call this more than once for more than one variable. tmux applies
    /// these to the new process only, not to the session.
    pub fn environment(mut self, name: impl Into<OsString>, value: impl Into<OsString>) -> Self {
        self.environment.push((name.into(), value.into()));
        self
    }

    /// Span the full width or height of the window rather than of the pane.
    pub const fn full(mut self) -> Self {
        self.full = true;
        self
    }

    /// Zoom the new pane, filling the window until it is unzoomed.
    pub const fn zoom(mut self) -> Self {
        self.zoom = true;
        self
    }

    /// Make the new pane active.
    pub const fn select(mut self) -> Self {
        self.select = true;
        self
    }

    /// Lower these options into a `split-window` command for one target.
    ///
    /// `print_format` is placed with the other flags because tmux stops
    /// parsing flags at the first positional, and the shell command is one.
    pub(crate) fn into_command(self, target: &str, print_format: &str) -> Command {
        let (axis, before) = self.direction.flags();
        let mut command = Command::new("split-window")
            .arg("-P")
            .arg("-F")
            .arg(print_format)
            .arg("-t")
            .arg(target)
            .arg(axis);
        if before {
            command = command.arg("-b");
        }
        if !self.select {
            command = command.arg("-d");
        }
        if self.full {
            command = command.arg("-f");
        }
        if self.zoom {
            command = command.arg("-Z");
        }
        if let Some(size) = self.size {
            command = command.arg("-l").arg(size.to_string());
        }
        if let Some(directory) = self.start_directory {
            command = command.arg("-c").arg(directory.into_os_string());
        }
        for (name, value) in self.environment {
            command = command.arg("-e").sensitive_arg(assignment(&name, &value));
        }
        if let Some(shell_command) = self.command {
            command = command.arg("--").sensitive_arg(shell_command);
        }
        command
    }
}

impl From<SplitDirection> for SplitOptions {
    fn from(direction: SplitDirection) -> Self {
        Self::new(direction)
    }
}

/// Render one `NAME=VALUE` pair for tmux's `-e` flag.
///
/// Built from bytes because tmux accepts environment values that are not
/// valid UTF-8, and rejecting them here would be stricter than tmux is.
pub(crate) fn assignment(name: &OsStr, value: &OsStr) -> OsString {
    use std::os::unix::ffi::{OsStrExt as _, OsStringExt as _};

    let mut bytes = name.as_bytes().to_vec();
    bytes.push(b'=');
    bytes.extend_from_slice(value.as_bytes());

    OsString::from_vec(bytes)
}

/// Renders `session:id`, the form tmux targets one of a window's links by.
///
/// The id alone would be ambiguous about which link is meant, and this is the
/// spelling that can be pasted into a tmux command. It names the window by
/// identity rather than by index: an index is a place within a session, and
/// the window sitting at one changes whenever anything renumbers or unlinks,
/// so a rendered coordinate goes stale in a handle that has not refreshed and
/// reaches a different window rather than nothing.
impl fmt::Display for Window {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}:{}", self.session_id(), self.id())
    }
}

/// Tell a missing link from a missing window, on the failure path only.
///
/// A link-scoped command targets `session:@id`: a window linked into three
/// sessions has one id and three links, so the session half says which link to
/// act on, while the window half stays an identity because a cached index
/// stops being one the moment anything renumbers the session.
///
/// tmux echoes such a target back unresolved whether the window died or is
/// merely linked somewhere else -- the two are the same string -- so the
/// classifier cannot separate them and reports the stronger fact. This buys
/// the weaker one for a lookup, and only when the answer changes what a caller
/// should do with the handle.
///
/// Asked of the server rather than of a handle. A window refreshes within its
/// own session, and a window whose link to that session is gone is exactly the
/// case being told apart -- so refreshing would report it missing and answer
/// the question with its own premise.
/// `links` are the `session:id` targets the command sent, so the one tmux
/// named is the one to ask about: a command carrying both a source and a
/// destination fails on whichever it could not resolve, and probing the other
/// would answer about a window that was never in question.
async fn link_or_object_gone(
    core: &Arc<Core>,
    error: Error,
    links: &[(&SessionId, &WindowId)],
) -> Error {
    let Error::ObjectGone {
        kind: ObjectKind::Window,
        ref id,
    } = error
    else {
        return error;
    };

    let Some(&(session, window)) = links.iter().find(|(_, window)| window.to_string() == *id)
    else {
        return error;
    };

    let alive = crate::Server::from_core(Arc::clone(core))
        .window_by_id(window)
        .await
        .is_ok_and(|found| found.is_some());

    if alive {
        return Error::LinkGone {
            kind: ObjectKind::Window,
            target: format!("{session}:{window}"),
        };
    }
    error
}

#[cfg(test)]
mod split_option_tests {
    use super::{SplitDirection, SplitOptions};

    #[test]
    fn split_options_redact_process_inputs() {
        let secret = "sentinel-split-process";
        let options = SplitOptions::new(SplitDirection::Below)
            .environment("TOKEN", secret)
            .command(secret);
        assert!(!format!("{options:?}").contains(secret));

        let summary = options.into_command("@1", "#{pane_id}").summary();
        assert_eq!(summary.sensitive_argument_count(), 2);
        assert!(!summary.to_string().contains(secret));
    }
}

#[cfg(test)]
mod layout_version_tests {
    use std::ffi::OsStr;

    use super::{Layout, SavedLayout};
    use crate::TmuxVersion;

    fn classify(text: &str, raw: &[u8]) -> SavedLayout {
        let version = TmuxVersion::parse_output(raw).expect("a parsable version");
        SavedLayout::classify(OsStr::new(text), &version)
    }

    /// A development tree carries every preset its release number implies, so
    /// resolving one must ask the behaviour question rather than the
    /// conservative one: clamping a development identifier to the crate's
    /// floor hides the mirrored pair on a tmux that has it, and silently
    /// turns an ambiguous prefix back into a unique one.
    #[test]
    fn a_development_release_resolves_the_presets_it_has() {
        for raw in [b"tmux next-3.9\n".as_slice(), b"tmux master\n".as_slice()] {
            assert_eq!(
                classify("main-vertical-mirrored", raw),
                SavedLayout::Preset(Layout::MainVerticalMirrored),
                "{} names a preset this tree has",
                String::from_utf8_lossy(raw).trim(),
            );
            assert!(
                matches!(classify("main-h", raw), SavedLayout::Ambiguous(_)),
                "{}: `main-h` is a prefix of two presets once the mirrored pair exists",
                String::from_utf8_lossy(raw).trim(),
            );
        }
    }

    /// The floor still has five presets, so the same prefix is unique there.
    #[test]
    fn the_minimum_release_resolves_its_own_presets() {
        assert_eq!(
            classify("main-h", b"tmux 3.3a\n"),
            SavedLayout::Preset(Layout::MainHorizontal),
        );
        assert_eq!(
            classify("main-vertical-mirrored", b"tmux 3.3a\n"),
            SavedLayout::Unrecognized,
        );
    }
}
````
