rmux-core 0.10.0

Core session, pane, layout, format, hook, and buffer model for the RMUX terminal multiplexer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use std::time::{SystemTime, UNIX_EPOCH};

use rmux_proto::{LayoutName, RmuxError, RotateWindowDirection, SplitDirection, TerminalSize};

use crate::layout::{LayoutDirection, LayoutTree};
use crate::{Pane, PaneGeometry, PaneId, WindowId};

/// Runtime alert flag bitset shared by window queue state and winlink-visible state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct AlertFlags(u8);

impl AlertFlags {
    /// Returns an empty alert bitset.
    #[must_use]
    pub const fn empty() -> Self {
        Self(0)
    }

    /// Returns whether all bits in `other` are present.
    #[must_use]
    pub const fn contains(self, other: Self) -> bool {
        (self.0 & other.0) == other.0
    }

    /// Returns whether any bit in `other` is present.
    #[must_use]
    pub const fn intersects(self, other: Self) -> bool {
        (self.0 & other.0) != 0
    }

    /// Returns whether no alert bits are set.
    #[must_use]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }

    /// Returns the union of `self` and `other`.
    #[must_use]
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Adds the bits from `other`.
    pub fn insert(&mut self, other: Self) {
        self.0 |= other.0;
    }

    /// Clears the bits from `other`.
    pub fn remove(&mut self, other: Self) {
        self.0 &= !other.0;
    }
}

/// Window alert queue bit for bells.
pub const WINDOW_BELL: AlertFlags = AlertFlags(0x1);
/// Window alert queue bit for activity.
pub const WINDOW_ACTIVITY: AlertFlags = AlertFlags(0x2);
/// Window alert queue bit for silence.
pub const WINDOW_SILENCE: AlertFlags = AlertFlags(0x4);
/// Combined window alert queue bits.
pub const WINDOW_ALERTFLAGS: AlertFlags =
    AlertFlags(WINDOW_BELL.0 | WINDOW_ACTIVITY.0 | WINDOW_SILENCE.0);

/// Persistent winlink alert bit for bells.
pub const WINLINK_BELL: AlertFlags = AlertFlags(0x1);
/// Persistent winlink alert bit for activity.
pub const WINLINK_ACTIVITY: AlertFlags = AlertFlags(0x2);
/// Persistent winlink alert bit for silence.
pub const WINLINK_SILENCE: AlertFlags = AlertFlags(0x4);
/// Combined persistent winlink alert bits.
pub const WINLINK_ALERTFLAGS: AlertFlags =
    AlertFlags(WINLINK_BELL.0 | WINLINK_ACTIVITY.0 | WINLINK_SILENCE.0);

/// A session-owned window whose pane order is independent from pane indices.
///
/// Pane order is preserved separately from pane indices so a split can insert a
/// new pane immediately after its split target while still assigning the next
/// sequential pane index. Stable pane IDs remain independent from those
/// window-local display indices, which may contain gaps after deletion.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Window {
    id: WindowId,
    panes: Vec<Pane>,
    next_pane_index: u32,
    active_pane: u32,
    last_pane: Option<u32>,
    layout: LayoutName,
    last_layout: Option<LayoutName>,
    layout_tree: Option<LayoutTree>,
    custom_layout: bool,
    old_layout: Option<String>,
    size: TerminalSize,
    name: Option<String>,
    automatic_rename: bool,
    zoomed: bool,
    zoom_restore_pending: bool,
    alert_flags: AlertFlags,
    alerts_queued: bool,
    created_at: i64,
    activity_at: i64,
    // `resize-pane -x` sets an explicit main-pane width; otherwise layout derives it.
    requested_main_width: Option<u16>,
    // `resize-pane -y` sets an explicit main-pane height; otherwise layout derives it.
    requested_main_height: Option<u16>,
}

#[path = "window/activity.rs"]
mod activity;
#[path = "window/layout_cycle.rs"]
mod layout_cycle;
#[path = "window/layout_ops.rs"]
mod layout_ops;
#[path = "window/panes.rs"]
mod panes;
#[path = "window/zoom.rs"]
mod zoom;

pub use activity::WindowPaneActivity;
use panes::layout_for_split;

impl Window {
    /// Creates the single V1 window with its initial pane.
    #[must_use]
    pub fn new(size: TerminalSize) -> Self {
        Self::new_with_initial_pane(size, PaneId::new(0), WindowId::new(0))
    }

    pub(crate) fn new_with_initial_pane(size: TerminalSize, pane_id: PaneId, id: WindowId) -> Self {
        let now = current_unix_timestamp();
        let mut window = Self {
            id,
            panes: vec![Pane::new_with_id(
                pane_id,
                0,
                PaneGeometry::new(0, 0, size.cols, size.rows),
            )],
            next_pane_index: 1,
            active_pane: 0,
            last_pane: None,
            layout: LayoutName::MainVertical,
            last_layout: None,
            layout_tree: Some(LayoutTree::single(size)),
            custom_layout: false,
            old_layout: None,
            size,
            name: None,
            automatic_rename: true,
            zoomed: false,
            zoom_restore_pending: false,
            alert_flags: AlertFlags::empty(),
            alerts_queued: false,
            created_at: now,
            activity_at: now,
            requested_main_width: None,
            requested_main_height: None,
        };
        window.recalculate_geometry();
        window
    }

    /// Returns the stable internal window identity.
    #[must_use]
    pub const fn id(&self) -> WindowId {
        self.id
    }

    /// Returns the panes in window order.
    #[must_use]
    pub fn panes(&self) -> &[Pane] {
        &self.panes
    }

    /// Returns the pane with the given stable pane index.
    #[must_use]
    pub fn pane(&self, pane_index: u32) -> Option<&Pane> {
        self.panes.iter().find(|pane| pane.index() == pane_index)
    }

    /// Returns a mutable pane reference for the given stable pane index.
    #[must_use]
    pub fn pane_mut(&mut self, pane_index: u32) -> Option<&mut Pane> {
        self.panes
            .iter_mut()
            .find(|pane| pane.index() == pane_index)
    }

    /// Returns the active pane index owned by the window.
    #[must_use]
    pub const fn active_pane_index(&self) -> u32 {
        self.active_pane
    }

    /// Returns the previously active pane index when one exists.
    #[must_use]
    pub const fn last_pane_index(&self) -> Option<u32> {
        self.last_pane
    }

    /// Returns the active pane when the window invariant is satisfied.
    #[must_use]
    pub fn active_pane(&self) -> Option<&Pane> {
        self.pane(self.active_pane)
    }

    /// Returns the stable internal pane identity for a display index.
    #[must_use]
    pub fn pane_id(&self, pane_index: u32) -> Option<PaneId> {
        self.pane(pane_index).map(Pane::id)
    }

    /// Returns the last selected named layout for the window.
    #[must_use]
    pub const fn layout(&self) -> LayoutName {
        self.layout
    }

    /// Returns the content size used by the layout tree and pane PTYs.
    #[must_use]
    pub const fn size(&self) -> TerminalSize {
        self.size
    }

    /// Returns the tmux-compatible serialized layout tree for the window.
    #[must_use]
    pub fn layout_dump(&self) -> String {
        self.layout_tree
            .as_ref()
            .map_or_else(String::new, |tree| tree.dump(&self.panes))
    }

    /// Saves the current serialized layout as the tmux-compatible old layout.
    pub fn save_old_layout(&mut self) {
        self.old_layout = Some(self.layout_dump());
    }

    /// Returns the previously saved serialized layout when one exists.
    #[must_use]
    pub fn old_layout(&self) -> Option<&str> {
        self.old_layout.as_deref()
    }

    /// Returns the explicit user-supplied window name when one exists.
    #[must_use]
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// Returns whether automatic renaming remains enabled for the window.
    #[must_use]
    pub const fn automatic_rename(&self) -> bool {
        self.automatic_rename
    }

    /// Returns any queued runtime alert flags for the window.
    #[must_use]
    pub const fn alert_flags(&self) -> AlertFlags {
        self.alert_flags
    }

    /// Returns whether alert processing is already queued for this window.
    #[must_use]
    pub const fn alerts_queued(&self) -> bool {
        self.alerts_queued
    }

    /// Returns the number of panes in the window.
    #[must_use]
    pub fn pane_count(&self) -> usize {
        self.panes.len()
    }

    /// Queues alert flags for later server-side processing.
    pub fn queue_alerts(&mut self, flags: AlertFlags) {
        self.alert_flags.insert(flags);
    }

    /// Drains and returns all queued alert flags.
    pub fn take_alert_flags(&mut self) -> AlertFlags {
        let flags = self.alert_flags;
        self.alert_flags = AlertFlags::empty();
        flags
    }

    /// Clears the provided queued alert flags.
    pub fn clear_alert_flags(&mut self, flags: AlertFlags) {
        self.alert_flags.remove(flags);
    }

    /// Sets whether alert processing is already queued.
    pub fn set_alerts_queued(&mut self, queued: bool) {
        self.alerts_queued = queued;
    }

    /// Resets this window to a single pane with a fresh pane ID, preserving the window id,
    /// name, and size. Returns the new pane ID.
    pub(crate) fn respawn(&mut self, pane_id: PaneId) -> PaneId {
        let size = self.size;
        self.panes = vec![Pane::new_with_id(
            pane_id,
            0,
            PaneGeometry::new(0, 0, size.cols, size.rows),
        )];
        self.next_pane_index = 1;
        self.active_pane = 0;
        self.last_pane = None;
        self.layout = LayoutName::MainVertical;
        self.last_layout = None;
        self.layout_tree = Some(LayoutTree::single(size));
        self.custom_layout = false;
        self.old_layout = None;
        self.automatic_rename = true;
        self.zoomed = false;
        self.zoom_restore_pending = false;
        self.alert_flags = AlertFlags::empty();
        self.alerts_queued = false;
        self.requested_main_width = None;
        self.requested_main_height = None;
        pane_id
    }

    pub(crate) fn set_size(&mut self, size: TerminalSize) {
        self.size = size;
        self.recalculate_geometry();
        if self.zoomed {
            self.apply_zoom_geometry();
        }
    }

    pub(crate) fn set_name(&mut self, name: String) {
        self.name = Some(name);
        self.automatic_rename = false;
    }

    /// Re-enables runtime automatic renaming for this window.
    pub fn enable_automatic_rename(&mut self) {
        self.automatic_rename = true;
    }

    /// Updates the runtime window name while preserving automatic renaming.
    pub fn set_automatic_name(&mut self, name: String) {
        self.name = Some(name);
        self.automatic_rename = true;
    }

    pub(crate) fn rotate_panes(&mut self, direction: RotateWindowDirection) {
        self.rotate_panes_with_zoom(direction, false);
    }

    pub(crate) fn rotate_panes_with_zoom(
        &mut self,
        direction: RotateWindowDirection,
        restore_zoom: bool,
    ) {
        if self.panes.len() <= 1 {
            return;
        }

        self.push_zoom(restore_zoom);
        let previous_active_pane_id = self
            .active_pane()
            .expect("active pane must exist before pane rotation")
            .id();
        let active_position = self
            .panes
            .iter()
            .position(|pane| pane.index() == self.active_pane)
            .expect("active pane must exist in window order");

        match direction {
            RotateWindowDirection::Down => self.panes.rotate_right(1),
            RotateWindowDirection::Up => self.panes.rotate_left(1),
        }
        for (index, pane) in self.panes.iter_mut().enumerate() {
            pane.set_index(index as u32);
        }

        self.apply_layout_tree();

        // tmux keeps the selected slot stable while pane contents rotate, then
        // tracks last-pane as the pane identity that was active before rotation.
        self.active_pane = active_position as u32;
        self.last_pane = self
            .pane_index_for_id(previous_active_pane_id)
            .filter(|pane_index| *pane_index != self.active_pane);
        self.mark_pane_active(self.active_pane);

        self.pop_zoom();
    }

    pub(crate) fn insert_pane_at_position(
        &mut self,
        position: usize,
        pane: Pane,
        direction: SplitDirection,
    ) -> Result<(), RmuxError> {
        if position > self.panes.len() {
            return Err(RmuxError::Server(format!(
                "cannot insert pane at position {position} in a {}-pane window",
                self.panes.len()
            )));
        }

        self.ensure_accepts_pane(&pane, None)?;
        self.auto_unzoom();
        self.layout = layout_for_split(direction);
        self.bump_next_pane_index(pane.index());
        let inserted_index = pane.index();
        self.panes.insert(position, pane);
        if self.panes.len() == 1 {
            self.active_pane = inserted_index;
            self.last_pane = None;
        }
        if self.panes.len() == 1 {
            self.layout_tree = Some(LayoutTree::single(self.size));
            self.apply_layout_tree();
            return Ok(());
        }

        let (target_leaf, insert_before_target) = if position == 0 {
            (0, true)
        } else {
            (position - 1, false)
        };
        let inserted = self.layout_tree.as_mut().is_some_and(|tree| {
            tree.split_leaf(
                target_leaf,
                LayoutDirection::from_split_direction(direction),
                insert_before_target,
            )
        });
        if !inserted {
            self.rebuild_named_layout_tree(self.layout);
        } else {
            self.apply_layout_tree();
        }
        Ok(())
    }

    pub(crate) fn move_pane_by_splitting_target(
        &mut self,
        source_position: usize,
        target_position: usize,
        final_insert_position: usize,
        direction: SplitDirection,
        insert_before_target: bool,
    ) -> Result<PaneId, RmuxError> {
        let pane_count = self.panes.len();
        if source_position >= pane_count {
            return Err(RmuxError::Server(format!(
                "cannot move missing pane at position {source_position}"
            )));
        }
        if target_position >= pane_count {
            return Err(RmuxError::Server(format!(
                "cannot split missing target pane at position {target_position}"
            )));
        }
        if final_insert_position > pane_count.saturating_sub(1) {
            return Err(RmuxError::Server(format!(
                "cannot insert moved pane at position {final_insert_position} in a {}-pane window",
                pane_count.saturating_sub(1)
            )));
        }

        self.auto_unzoom();
        self.layout = layout_for_split(direction);

        let split_insert_position = if insert_before_target {
            target_position
        } else {
            target_position + 1
        };
        let source_leaf_after_split = if split_insert_position <= source_position {
            source_position + 1
        } else {
            source_position
        };
        let tree = self.layout_tree.as_mut().ok_or_else(|| {
            RmuxError::Server("cannot move pane without a layout tree".to_owned())
        })?;
        if !tree.split_leaf(
            target_position,
            LayoutDirection::from_split_direction(direction),
            insert_before_target,
        ) {
            return Err(RmuxError::Server(format!(
                "cannot split target pane at position {target_position}"
            )));
        }
        if !tree.remove_leaf(source_leaf_after_split) {
            return Err(RmuxError::Server(format!(
                "cannot remove source pane leaf at position {source_leaf_after_split}"
            )));
        }

        let moved_pane = self.panes.remove(source_position);
        let moved_pane_id = moved_pane.id();
        self.panes.insert(final_insert_position, moved_pane);
        self.apply_layout_tree();
        Ok(moved_pane_id)
    }

    pub(crate) fn insert_pane_full_size(
        &mut self,
        pane: Pane,
        direction: SplitDirection,
        insert_before_target: bool,
    ) -> Result<(), RmuxError> {
        self.ensure_accepts_pane(&pane, None)?;
        self.auto_unzoom();
        self.layout = layout_for_split(direction);
        self.bump_next_pane_index(pane.index());

        if insert_before_target {
            self.panes.insert(0, pane);
        } else {
            self.panes.push(pane);
        }

        let split = self.layout_tree.as_mut().is_some_and(|tree| {
            tree.split_root(
                LayoutDirection::from_split_direction(direction),
                insert_before_target,
            )
        });
        if !split {
            self.rebuild_named_layout_tree(self.layout);
        } else {
            self.apply_layout_tree();
        }
        Ok(())
    }

    pub(crate) fn replace_pane_for_swap(
        &mut self,
        pane_index: u32,
        pane: Pane,
    ) -> Result<(), RmuxError> {
        let position = self.pane_position(pane_index).ok_or_else(|| {
            RmuxError::Server(format!(
                "cannot replace missing pane index {pane_index} in window {}",
                self.id
            ))
        })?;
        for (existing_position, existing) in self.panes.iter().enumerate() {
            if existing_position != position && existing.id() == pane.id() {
                return Err(RmuxError::Server(format!(
                    "pane id {} already exists in window {}",
                    pane.id().as_u32(),
                    self.id
                )));
            }
        }
        self.bump_next_pane_index(pane.index());
        self.panes[position] = pane;
        self.apply_layout_tree();
        Ok(())
    }

    pub(crate) fn swap_panes(&mut self, source_pane_index: u32, target_pane_index: u32) -> bool {
        let Some(source_position) = self.pane_position(source_pane_index) else {
            return false;
        };
        let Some(target_position) = self.pane_position(target_pane_index) else {
            return false;
        };
        if source_position == target_position {
            return true;
        }

        self.auto_unzoom();
        let active_pane_id = self
            .active_pane()
            .expect("active pane must exist before pane swap")
            .id();
        let last_pane_id = self
            .last_pane
            .and_then(|pane_index| self.pane(pane_index).map(Pane::id));
        self.panes.swap(source_position, target_position);
        self.apply_layout_tree();
        self.renumber_panes_by_position(active_pane_id, last_pane_id);
        true
    }
}

fn current_unix_timestamp() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .ok()
        .and_then(|duration| i64::try_from(duration.as_secs()).ok())
        .unwrap_or_default()
}

#[cfg(test)]
#[path = "window/tests.rs"]
mod tests;