Skip to main content

rmux_sdk/handles/
split.rs

1//! Pane-split direction for the ergonomic high-level SDK.
2//!
3//! Names describe **where the new pane appears** relative to the active one.
4//! This deliberately avoids the historical `Horizontal`/`Vertical` labels,
5//! which depend on whether you mean the divider-line orientation or the
6//! pane arrangement (tmux uses the latter; readers of source code often
7//! assume the former). `Right`/`Left`/`Up`/`Down` admits exactly one
8//! reading.
9//!
10//! The four directions map to the rmux daemon's two-axis split plus the
11//! tmux `-b` flag — see [`SplitDirection::axis`] and
12//! [`SplitDirection::before`].
13
14/// Direction of a `Pane::split` call.
15///
16/// Named by **where the new pane lands** relative to the pane being split.
17/// Internally each variant decomposes into an axis and an insertion side
18/// (after / before the target), matching the tmux `split-window` model.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20#[non_exhaustive]
21pub enum SplitDirection {
22    /// New pane to the right of the active pane (vertical divider line).
23    Right,
24    /// New pane to the left of the active pane (vertical divider line).
25    ///
26    /// Equivalent to tmux `split-window -h -b`.
27    Left,
28    /// New pane below the active pane (horizontal divider line).
29    Down,
30    /// New pane above the active pane (horizontal divider line).
31    ///
32    /// Equivalent to tmux `split-window -v -b`.
33    Up,
34}
35
36impl SplitDirection {
37    /// Returns the daemon-side axis of this split direction.
38    ///
39    /// `Right`/`Left` split the horizontal axis (panes end up side by side);
40    /// `Up`/`Down` split the vertical axis (panes end up stacked).
41    pub(crate) fn axis(self) -> rmux_proto::SplitDirection {
42        match self {
43            Self::Right | Self::Left => rmux_proto::SplitDirection::Horizontal,
44            Self::Up | Self::Down => rmux_proto::SplitDirection::Vertical,
45        }
46    }
47
48    /// Returns `true` when the new pane is inserted *before* the active
49    /// pane on the chosen axis (matches the tmux `-b` flag).
50    pub(crate) fn before(self) -> bool {
51        matches!(self, Self::Left | Self::Up)
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::SplitDirection;
58
59    #[test]
60    fn right_and_down_insert_after() {
61        assert!(!SplitDirection::Right.before());
62        assert!(!SplitDirection::Down.before());
63    }
64
65    #[test]
66    fn left_and_up_insert_before() {
67        assert!(SplitDirection::Left.before());
68        assert!(SplitDirection::Up.before());
69    }
70
71    #[test]
72    fn horizontal_directions_share_an_axis() {
73        assert_eq!(SplitDirection::Right.axis(), SplitDirection::Left.axis());
74    }
75
76    #[test]
77    fn vertical_directions_share_an_axis() {
78        assert_eq!(SplitDirection::Up.axis(), SplitDirection::Down.axis());
79    }
80
81    #[test]
82    fn horizontal_and_vertical_axes_differ() {
83        assert_ne!(SplitDirection::Right.axis(), SplitDirection::Down.axis());
84    }
85}