tmux-client 0.1.0-alpha.1

Typed asynchronous Rust client for tmux
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
//! Window-link snapshots, layouts, and mutations.

use std::collections::BTreeMap;
use std::ffi::OsString;
use std::future::Future;
use std::path::PathBuf;

use crate::{
    Command, Error, NewPane, ObjectKind, OptionMap, OptionScope, OptionValue, Pane,
    ResizeDirection, Result, Server, Session, SessionId, SessionTarget, SparseOptionMap, SplitPane,
    TmuxText, WindowId, WindowLink, WindowSnapshot, WindowTarget,
};

/// A built-in tmux window layout or a caller-provided layout string.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Layout {
    /// `even-horizontal`.
    EvenHorizontal,
    /// `even-vertical`.
    EvenVertical,
    /// `main-horizontal`.
    MainHorizontal,
    /// `main-vertical`.
    MainVertical,
    /// `tiled`.
    Tiled,
    /// A serialized or custom layout name.
    Custom(String),
}

impl std::fmt::Display for Layout {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(match self {
            Self::EvenHorizontal => "even-horizontal",
            Self::EvenVertical => "even-vertical",
            Self::MainHorizontal => "main-horizontal",
            Self::MainVertical => "main-vertical",
            Self::Tiled => "tiled",
            Self::Custom(value) => value,
        })
    }
}

/// Direction used by `rotate-window`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Rotation {
    /// Rotate panes upward.
    Up,
    /// Rotate panes downward.
    Down,
}

/// Placement relative to a target window.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum WindowPosition {
    /// Insert before the target (`-b`).
    Before,
    /// Insert after the target (`-a`).
    After,
}

/// Typed arguments for the three `resize-window` modes.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct WindowResize {
    adjustment: Option<(ResizeDirection, u32)>,
    width: Option<u32>,
    height: Option<u32>,
    expand: bool,
    shrink: bool,
}

impl WindowResize {
    /// Creates empty resize arguments.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adjusts one edge by a non-zero number of cells.
    pub fn adjustment(mut self, direction: ResizeDirection, cells: u32) -> Result<Self> {
        if cells == 0 {
            return Err(Error::InvalidArgument {
                argument: "window resize adjustment",
                message: "must be non-zero".to_owned(),
            });
        }
        self.adjustment = Some((direction, cells));
        Ok(self)
    }

    /// Sets a non-zero absolute width.
    pub fn width(mut self, width: u32) -> Result<Self> {
        if width == 0 {
            return Err(Error::InvalidArgument {
                argument: "window width",
                message: "must be non-zero".to_owned(),
            });
        }
        self.width = Some(width);
        Ok(self)
    }

    /// Sets a non-zero absolute height.
    pub fn height(mut self, height: u32) -> Result<Self> {
        if height == 0 {
            return Err(Error::InvalidArgument {
                argument: "window height",
                message: "must be non-zero".to_owned(),
            });
        }
        self.height = Some(height);
        Ok(self)
    }

    /// Expands the window to the available size.
    #[must_use]
    pub const fn expand(mut self, enabled: bool) -> Self {
        self.expand = enabled;
        self
    }

    /// Shrinks the window to the smallest size.
    #[must_use]
    pub const fn shrink(mut self, enabled: bool) -> Self {
        self.shrink = enabled;
        self
    }
}

/// Typed arguments for `respawn-window`.
#[derive(Clone, Debug, Default)]
pub struct RespawnWindow {
    command: Option<OsString>,
    cwd: Option<PathBuf>,
    environment: BTreeMap<String, OsString>,
    kill: bool,
}

impl RespawnWindow {
    /// Creates default respawn arguments.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the replacement command.
    #[must_use]
    pub fn command(mut self, command: impl Into<OsString>) -> Self {
        self.command = Some(command.into());
        self
    }

    /// Sets the starting directory.
    #[must_use]
    pub fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
        self.cwd = Some(cwd.into());
        self
    }

    /// Adds an environment entry.
    pub fn environment(
        mut self,
        name: impl Into<String>,
        value: impl Into<OsString>,
    ) -> Result<Self> {
        let name = name.into();
        crate::server::validate_name("environment name", &name)?;
        self.environment.insert(name, value.into());
        Ok(self)
    }

    /// Kills active processes before replacement.
    #[must_use]
    pub const fn kill(mut self, enabled: bool) -> Self {
        self.kill = enabled;
        self
    }
}

/// Typed arguments for creating a window.
#[derive(Clone, Debug)]
pub struct NewWindow {
    name: Option<OsString>,
    cwd: Option<PathBuf>,
    index: Option<u32>,
    detached: bool,
    kill_existing: bool,
    shell_command: Option<OsString>,
    environment: BTreeMap<String, OsString>,
    relative_to: Option<WindowTarget>,
    position: Option<WindowPosition>,
    select_existing: bool,
}

impl Default for NewWindow {
    fn default() -> Self {
        Self {
            name: None,
            cwd: None,
            index: None,
            detached: true,
            kill_existing: false,
            shell_command: None,
            environment: BTreeMap::new(),
            relative_to: None,
            position: None,
            select_existing: false,
        }
    }
}

impl NewWindow {
    /// Creates default arguments.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the window name.
    #[must_use]
    pub fn name(mut self, name: impl Into<OsString>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Sets the starting directory.
    #[must_use]
    pub fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
        self.cwd = Some(cwd.into());
        self
    }

    /// Places the new link at a specific index.
    #[must_use]
    pub const fn index(mut self, index: u32) -> Self {
        self.index = Some(index);
        self
    }

    /// Chooses whether the new window remains detached.
    #[must_use]
    pub const fn detached(mut self, detached: bool) -> Self {
        self.detached = detached;
        self
    }

    /// Replaces an existing link at the requested index.
    #[must_use]
    pub const fn kill_existing(mut self, enabled: bool) -> Self {
        self.kill_existing = enabled;
        self
    }

    /// Sets the command for the first pane.
    #[must_use]
    pub fn shell_command(mut self, command: impl Into<OsString>) -> Self {
        self.shell_command = Some(command.into());
        self
    }

    /// Adds a window environment entry. The value is redacted in diagnostics.
    pub fn environment(
        mut self,
        name: impl Into<String>,
        value: impl Into<OsString>,
    ) -> Result<Self> {
        let name = name.into();
        crate::server::validate_name("environment name", &name)?;
        self.environment.insert(name, value.into());
        Ok(self)
    }

    /// Places the new window relative to an existing target.
    #[must_use]
    pub fn relative_to(
        mut self,
        target: impl Into<WindowTarget>,
        position: WindowPosition,
    ) -> Self {
        self.relative_to = Some(target.into());
        self.position = Some(position);
        self
    }

    /// Selects an existing window with the requested name instead of creating it.
    #[must_use]
    pub const fn select_existing(mut self, enabled: bool) -> Self {
        self.select_existing = enabled;
        self
    }
}

/// A tmux window identity plus one session-specific link and owned snapshot.
#[derive(Clone, Debug)]
pub struct Window {
    server: Server,
    id: WindowId,
    session_id: SessionId,
    link: WindowLink,
    snapshot: WindowSnapshot,
}

impl Window {
    pub(crate) fn from_row(server: Server, row: BTreeMap<&'static str, TmuxText>) -> Result<Self> {
        let snapshot = WindowSnapshot::new(row);
        let id = snapshot.id()?.ok_or_else(|| Error::Decode {
            context: "window row",
            message: "window_id was empty".to_owned(),
        })?;
        let session_id = snapshot
            .get("session_id")
            .ok_or_else(|| Error::Decode {
                context: "window row",
                message: "session_id was absent".to_owned(),
            })?
            .to_str()
            .map_err(|source| Error::Decode {
                context: "window session ID",
                message: source.to_string(),
            })?
            .parse()?;
        let index = snapshot.index()?.ok_or_else(|| Error::Decode {
            context: "window row",
            message: "window_index was empty".to_owned(),
        })?;
        Ok(Self {
            server,
            id,
            session_id,
            link: WindowLink::new(session_id, index),
            snapshot,
        })
    }

    /// Resolves the window containing the pane named by `TMUX_PANE`.
    pub async fn from_environment() -> Result<Self> {
        Server::from_environment()?
            .pane_from_environment()
            .await?
            .window()
            .await
    }

    pub(crate) async fn create(session: &Session, options: NewWindow) -> Result<Self> {
        let target = options.relative_to.as_ref().map_or_else(
            || {
                options.index.map_or_else(
                    || session.id().to_string(),
                    |index| format!("{}:{index}", session.id()),
                )
            },
            ToString::to_string,
        );
        let mut command = Command::new("new-window")
            .arg("-P")
            .arg("-F")
            .arg("#{window_id}")
            .arg("-t")
            .arg(target);
        if options.detached {
            command = command.arg("-d");
        }
        if options.kill_existing {
            command = command.arg("-k");
        }
        if options.select_existing {
            command = command.arg("-S");
        }
        if let Some(position) = options.position {
            command = command.arg(match position {
                WindowPosition::Before => "-b",
                WindowPosition::After => "-a",
            });
        }
        if let Some(name) = options.name {
            command = command.arg("-n").arg(name);
        }
        if let Some(cwd) = options.cwd {
            command = command.arg("-c").arg(cwd);
        }
        for (name, value) in options.environment {
            command =
                command
                    .arg("-e")
                    .sensitive_arg(format!("{}={}", name, value.to_string_lossy()));
        }
        if let Some(shell_command) = options.shell_command {
            command = command.sensitive_arg(shell_command);
        }
        let result = session.server().checked("create window", command).await?;
        let id: WindowId =
            crate::server::single_line(result.stdout(), "new-window window ID")?.parse()?;
        session.window(id).await
    }

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

    /// Returns this session-specific window link.
    #[must_use]
    pub const fn link(&self) -> &WindowLink {
        &self.link
    }

    /// Returns the owning session ID.
    #[must_use]
    pub const fn session_id(&self) -> SessionId {
        self.session_id
    }

    /// Returns the owned snapshot.
    #[must_use]
    pub const fn snapshot(&self) -> &WindowSnapshot {
        &self.snapshot
    }

    /// Returns the server handle.
    #[must_use]
    pub const fn server(&self) -> &Server {
        &self.server
    }

    /// Refreshes this exact window link. A moved or unlinked row is stale.
    pub async fn refresh(&mut self) -> Result<()> {
        let link = self.link.clone();
        *self = self
            .server
            .windows()
            .await?
            .into_iter()
            .find(|window| window.link == link)
            .ok_or_else(|| Error::ObjectNotFound {
                kind: ObjectKind::Window,
                target: link.to_string(),
            })?;
        Ok(())
    }

    /// Resolves the parent session.
    pub async fn session(&self) -> Result<Session> {
        self.server.session(self.session_id()).await
    }

    /// Resolves every session containing a link to this window.
    pub async fn linked_sessions(&self) -> Result<Vec<Session>> {
        let mut ids = self
            .server
            .windows()
            .await?
            .into_iter()
            .filter(|window| window.id == self.id)
            .map(|window| window.session_id)
            .collect::<std::collections::BTreeSet<_>>();
        let mut sessions = Vec::with_capacity(ids.len());
        while let Some(id) = ids.pop_first() {
            sessions.push(self.server.session(id).await?);
        }
        Ok(sessions)
    }

    /// Lists panes in this window.
    pub async fn panes(&self) -> Result<Vec<Pane>> {
        Ok(self
            .server
            .panes()
            .await?
            .into_iter()
            .filter(|pane| pane.window_id() == self.id && pane.session_id() == self.session_id())
            .collect())
    }

    /// Lists panes, returning an empty collection on failure.
    pub async fn panes_or_empty(&self) -> Vec<Pane> {
        self.panes().await.unwrap_or_default()
    }

    /// Returns the active pane.
    pub async fn active_pane(&self) -> Result<Pane> {
        for pane in self.panes().await? {
            if pane.snapshot().active()? == Some(true) {
                return Ok(pane);
            }
        }
        Err(Error::ObjectNotFound {
            kind: ObjectKind::Pane,
            target: format!("active pane in {}", self.link),
        })
    }

    /// Runs an arbitrary tmux command targeted at this exact link.
    pub async fn cmd(&self, command: Command) -> Result<crate::CommandResult> {
        self.server.cmd(command.target(&self.link)).await
    }

    /// Splits this window's active pane.
    pub async fn split(&self, options: SplitPane) -> Result<Pane> {
        let pane = self.active_pane().await?;
        pane.split(options).await
    }

    /// Creates a floating pane in this window. Requires tmux 3.7+.
    pub async fn new_pane(&self, options: NewPane) -> Result<Pane> {
        self.active_pane().await?.new_pane(options).await
    }

    /// Creates a window before or after this link.
    pub async fn new_window(&self, options: NewWindow, position: WindowPosition) -> Result<Window> {
        self.session()
            .await?
            .new_window(options.relative_to(self.link.clone(), position))
            .await
    }

    /// Selects a pane in this window.
    pub async fn select_pane(&self, pane: crate::PaneId) -> Result<Pane> {
        self.server
            .checked(
                "select window pane",
                Command::new("select-pane").target(pane),
            )
            .await?;
        self.server.pane(pane).await
    }

    /// Selects the last active pane.
    pub async fn last_pane(&self) -> Result<Pane> {
        self.last_pane_with(None, false).await
    }

    /// Selects the last active pane with explicit input and zoom behavior.
    pub async fn last_pane_with(
        &self,
        input_enabled: Option<bool>,
        keep_zoom: bool,
    ) -> Result<Pane> {
        let mut command = Command::new("last-pane").target(&self.link);
        if let Some(enabled) = input_enabled {
            command = command.arg(if enabled { "-e" } else { "-d" });
        }
        if keep_zoom {
            command = command.arg("-Z");
        }
        self.server.checked("last pane", command).await?;
        self.active_pane().await
    }

    /// Creates a temporary pane, runs an async operation, and attempts cleanup.
    pub async fn with_pane<F, Fut, T>(&self, options: SplitPane, operation: F) -> Result<T>
    where
        F: FnOnce(Pane) -> Fut,
        Fut: Future<Output = Result<T>>,
    {
        let pane = self.split(options).await?;
        let cleanup = pane.clone();
        let outcome = operation(pane).await;
        let cleanup_result = cleanup.kill().await;
        match (outcome, cleanup_result) {
            (Ok(value), Ok(())) => Ok(value),
            (Ok(_), Err(source)) => Err(Error::Cleanup {
                operation: "with_pane",
                source: Box::new(source),
            }),
            (Err(source), _) => Err(source),
        }
    }

    /// Selects this window link.
    pub async fn select(&self) -> Result<()> {
        self.server
            .checked(
                "select window",
                Command::new("select-window").target(&self.link),
            )
            .await
            .map(|_| ())
    }

    /// Renames the window and refreshes the snapshot.
    pub async fn rename(&mut self, name: impl Into<OsString>) -> Result<()> {
        self.server
            .checked(
                "rename window",
                Command::new("rename-window").target(&self.link).arg(name),
            )
            .await?;
        self.refresh().await
    }

    /// Applies a built-in or serialized layout.
    pub async fn select_layout(&mut self, layout: &Layout) -> Result<()> {
        self.server
            .checked(
                "select layout",
                Command::new("select-layout")
                    .target(&self.link)
                    .arg(layout.to_string()),
            )
            .await?;
        self.refresh().await
    }

    /// Advances to the next built-in layout and refreshes the snapshot.
    pub async fn next_layout(&mut self) -> Result<()> {
        self.server
            .checked(
                "next layout",
                Command::new("next-layout").target(&self.link),
            )
            .await?;
        self.refresh().await
    }

    /// Returns to the previous layout and refreshes the snapshot.
    pub async fn previous_layout(&mut self) -> Result<()> {
        self.server
            .checked(
                "previous layout",
                Command::new("previous-layout").target(&self.link),
            )
            .await?;
        self.refresh().await
    }

    /// Spreads panes evenly with `select-layout -E`.
    pub async fn spread_layout(&mut self) -> Result<()> {
        self.server
            .checked(
                "spread layout",
                Command::new("select-layout").arg("-E").target(&self.link),
            )
            .await?;
        self.refresh().await
    }

    /// Resizes the window to an absolute width and/or height.
    pub async fn resize(&mut self, width: Option<u32>, height: Option<u32>) -> Result<()> {
        if width.is_none() && height.is_none() {
            return Err(Error::InvalidArgument {
                argument: "window size",
                message: "width or height is required".to_owned(),
            });
        }
        if width == Some(0) || height == Some(0) {
            return Err(Error::InvalidArgument {
                argument: "window size",
                message: "dimensions must be non-zero".to_owned(),
            });
        }
        let mut command = Command::new("resize-window").target(&self.link);
        if let Some(width) = width {
            command = command.arg("-x").arg(width.to_string());
        }
        if let Some(height) = height {
            command = command.arg("-y").arg(height.to_string());
        }
        self.server.checked("resize window", command).await?;
        self.refresh().await
    }

    /// Applies typed adjustment, absolute, expand, or shrink resizing.
    pub async fn resize_with(&mut self, options: WindowResize) -> Result<()> {
        let modes = usize::from(options.adjustment.is_some())
            + usize::from(options.width.is_some() || options.height.is_some())
            + usize::from(options.expand)
            + usize::from(options.shrink);
        if modes != 1 {
            return Err(Error::InvalidArgument {
                argument: "window resize mode",
                message: "choose exactly one of adjustment, absolute size, expand, or shrink"
                    .to_owned(),
            });
        }
        let mut command = Command::new("resize-window").target(&self.link);
        if let Some((direction, cells)) = options.adjustment {
            command = command.arg(direction.flag()).arg(cells.to_string());
        }
        if let Some(width) = options.width {
            command = command.arg("-x").arg(width.to_string());
        }
        if let Some(height) = options.height {
            command = command.arg("-y").arg(height.to_string());
        }
        if options.expand {
            command = command.arg("-A");
        }
        if options.shrink {
            command = command.arg("-a");
        }
        self.server.checked("resize window", command).await?;
        self.refresh().await
    }

    /// Rotates panes in the window.
    pub async fn rotate(&self, direction: Rotation) -> Result<()> {
        let mut command = Command::new("rotate-window");
        if direction == Rotation::Down {
            command = command.arg("-D");
        }
        self.server
            .checked("rotate window", command.target(&self.link))
            .await
            .map(|_| ())
    }

    /// Rotates panes without unzooming the window.
    pub async fn rotate_keep_zoom(&self, direction: Rotation) -> Result<()> {
        let direction = match direction {
            Rotation::Up => "-U",
            Rotation::Down => "-D",
        };
        self.server
            .checked(
                "rotate window",
                Command::new("rotate-window")
                    .arg(direction)
                    .arg("-Z")
                    .target(&self.link),
            )
            .await
            .map(|_| ())
    }

    /// Swaps this window with another target.
    pub async fn swap_with(
        &self,
        destination: impl Into<WindowTarget>,
        detached: bool,
    ) -> Result<()> {
        let mut command = Command::new("swap-window")
            .arg("-s")
            .arg(self.link.to_string());
        if detached {
            command = command.arg("-d");
        }
        command = command.arg("-t").arg(destination.into().to_string());
        self.server
            .checked("swap window", command)
            .await
            .map(|_| ())
    }

    /// Links this window into another session and returns the new link snapshot.
    pub async fn link_to(
        &self,
        session: impl Into<SessionTarget>,
        index: Option<u32>,
        kill_existing: bool,
    ) -> Result<Window> {
        self.link_to_with(session, index, kill_existing, None, true)
            .await
    }

    /// Links this window with placement and selection flags.
    pub async fn link_to_with(
        &self,
        session: impl Into<SessionTarget>,
        index: Option<u32>,
        kill_existing: bool,
        position: Option<WindowPosition>,
        detached: bool,
    ) -> Result<Window> {
        let session = session.into();
        let destination_session_id = match &session {
            SessionTarget::Id(id) => *id,
            SessionTarget::Name(name) => self.server.session_by_name(name).await?.id(),
        };
        let destination =
            index.map_or_else(|| session.to_string(), |index| format!("{session}:{index}"));
        let mut command = Command::new("link-window")
            .arg("-s")
            .arg(self.link.to_string())
            .arg("-t")
            .arg(&destination);
        if kill_existing {
            command = command.arg("-k");
        }
        if let Some(position) = position {
            command = command.arg(match position {
                WindowPosition::Before => "-b",
                WindowPosition::After => "-a",
            });
        }
        if detached {
            command = command.arg("-d");
        }
        self.server.checked("link window", command).await?;
        self.server
            .windows()
            .await?
            .into_iter()
            .find(|window| {
                window.id == self.id
                    && window.session_id == destination_session_id
                    && index.is_none_or(|index| window.link.index() == index)
            })
            .ok_or(Error::ObjectNotFound {
                kind: ObjectKind::Window,
                target: destination,
            })
    }

    /// Moves this link to another destination.
    pub async fn move_to(
        &self,
        session: impl Into<SessionTarget>,
        index: Option<u32>,
        kill_existing: bool,
    ) -> Result<()> {
        self.move_to_with(session, index, kill_existing, None, true)
            .await
    }

    /// Moves this window with placement and selection flags.
    pub async fn move_to_with(
        &self,
        session: impl Into<SessionTarget>,
        index: Option<u32>,
        kill_existing: bool,
        position: Option<WindowPosition>,
        detached: bool,
    ) -> Result<()> {
        let session = session.into();
        let destination =
            index.map_or_else(|| session.to_string(), |index| format!("{session}:{index}"));
        let mut command = Command::new("move-window")
            .arg("-s")
            .arg(self.link.to_string())
            .arg("-t")
            .arg(destination);
        if kill_existing {
            command = command.arg("-k");
        }
        if let Some(position) = position {
            command = command.arg(match position {
                WindowPosition::Before => "-b",
                WindowPosition::After => "-a",
            });
        }
        if detached {
            command = command.arg("-d");
        }
        self.server
            .checked("move window", command)
            .await
            .map(|_| ())
    }

    /// Unlinks this window from its session.
    pub async fn unlink(&self, kill_if_last: bool) -> Result<()> {
        let mut command = Command::new("unlink-window");
        if kill_if_last {
            command = command.arg("-k");
        }
        self.server
            .checked("unlink window", command.target(&self.link))
            .await
            .map(|_| ())
    }

    /// Respawns the active pane for this window.
    pub async fn respawn(
        &self,
        command: Option<OsString>,
        cwd: Option<PathBuf>,
        kill: bool,
    ) -> Result<()> {
        let mut tmux = Command::new("respawn-window").target(&self.link);
        if kill {
            tmux = tmux.arg("-k");
        }
        if let Some(cwd) = cwd {
            tmux = tmux.arg("-c").arg(cwd);
        }
        if let Some(command) = command {
            tmux = tmux.sensitive_arg(command);
        }
        self.server
            .checked("respawn window", tmux)
            .await
            .map(|_| ())
    }

    /// Respawns a window with typed environment and process arguments.
    pub async fn respawn_with(&self, options: RespawnWindow) -> Result<()> {
        let mut command = Command::new("respawn-window").target(&self.link);
        if options.kill {
            command = command.arg("-k");
        }
        if let Some(cwd) = options.cwd {
            command = command.arg("-c").arg(cwd);
        }
        for (name, value) in options.environment {
            command =
                command
                    .arg("-e")
                    .sensitive_arg(format!("{}={}", name, value.to_string_lossy()));
        }
        if let Some(shell_command) = options.command {
            command = command.sensitive_arg(shell_command);
        }
        self.server
            .checked("respawn window", command)
            .await
            .map(|_| ())
    }

    /// Destroys the underlying window and all of its links.
    pub async fn kill(&self) -> Result<()> {
        self.server
            .checked("kill window", Command::new("kill-window").target(self.id))
            .await
            .map(|_| ())
    }

    /// Kills every other window in this session.
    pub async fn kill_others(&self) -> Result<()> {
        self.server
            .checked(
                "kill other windows",
                Command::new("kill-window").arg("-a").target(&self.link),
            )
            .await
            .map(|_| ())
    }

    /// Shows window-scoped options.
    pub async fn options(&self) -> Result<OptionMap> {
        self.server
            .show_options(OptionScope::Window, Some(&self.link.to_string()))
            .await
    }

    /// Shows sparse window options and hooks.
    pub async fn sparse_options(&self) -> Result<SparseOptionMap> {
        self.server
            .show_sparse_options(OptionScope::Window, Some(&self.link.to_string()))
            .await
    }

    /// Sets a window option.
    pub async fn set_option(&self, name: &str, value: &OptionValue, append: bool) -> Result<()> {
        self.server
            .set_option(
                OptionScope::Window,
                Some(&self.link.to_string()),
                name,
                value,
                append,
            )
            .await
    }

    /// Unsets a window option.
    pub async fn unset_option(&self, name: &str) -> Result<()> {
        self.server
            .unset_option(OptionScope::Window, Some(&self.link.to_string()), name)
            .await
    }
}

impl PartialEq for Window {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.link == other.link
            && self.server.config() == other.server.config()
    }
}

impl Eq for Window {}