workmux 0.1.209

An opinionated workflow tool that orchestrates git worktrees and tmux
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
//! Application state for the sidebar TUI.

use anyhow::Result;
use ratatui::layout::Rect;
use ratatui::widgets::ListState;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::agent_display::{extract_project_name, extract_worktree_name, resolve_labels};
use crate::cmd::Cmd;
use crate::config::{AgentIcons, Config, SidebarPosition, SidebarWidth, StatusIcons};
use crate::git::GitStatus;
use crate::github::PrSummary;
use ratatui::style::Color;
use std::collections::BTreeMap;
use std::str::FromStr;
use tracing::warn;

use crate::multiplexer::{AgentPane, Multiplexer};

use crate::ui::theme::ThemePalette;

use super::snapshot::SidebarSnapshot;
use super::template::parser::{ParseError, Token, parse_line};

/// Sidebar layout mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SidebarLayoutMode {
    Compact,
    #[default]
    Tiles,
}

impl SidebarLayoutMode {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Compact => "compact",
            Self::Tiles => "tiles",
        }
    }
}

/// Whether the sidebar auto-follows its host window or the user is navigating manually.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SelectionMode {
    FollowHost,
    Manual,
}

/// Runtime form of `sidebar.agent_icons`: icon strings and parsed colors.
///
/// Built once when config loads or reloads. Color strings are parsed eagerly
/// so the render path does no string parsing per row per frame, and invalid
/// colors warn once at load time instead of being silently ignored every
/// render.
///
/// The `colors` map distinguishes:
///   - `Some(Some(c))`: user override color.
///   - `Some(None)`: explicit opt-out (`color: ''`); skip the
///     `AgentKind::default_color` fallback.
///   - kind missing from map: no override, fall through to default.
#[derive(Debug, Default, Clone)]
pub struct ResolvedAgentIcons {
    pub icons: BTreeMap<String, String>,
    pub colors: BTreeMap<String, Option<Color>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HitBox {
    pub idx: usize,
    pub x_start: u16,
    pub x_end: u16,
}

impl ResolvedAgentIcons {
    pub fn from_config(map: Option<&AgentIcons>) -> Self {
        let mut icons = BTreeMap::new();
        let mut colors = BTreeMap::new();
        let Some(map) = map else {
            return Self { icons, colors };
        };
        for (kind, spec) in map {
            if let Some(i) = spec.icon() {
                icons.insert(kind.clone(), i.to_string());
            }
            if let Some(raw) = spec.color() {
                let trimmed = raw.trim();
                if trimmed.is_empty() {
                    colors.insert(kind.clone(), None);
                } else {
                    match Color::from_str(trimmed) {
                        Ok(c) => {
                            colors.insert(kind.clone(), Some(c));
                        }
                        Err(_) => warn!(
                            "sidebar.agent_icons.{kind}.color = {raw:?}: invalid color, ignoring"
                        ),
                    }
                }
            }
        }
        Self { icons, colors }
    }
}

const DEFAULT_COMPACT_TEMPLATE: &str = "{status_icon} {primary} {pane_suffix} {fill} {elapsed}";
const DEFAULT_TILE_TEMPLATES: &[&str] = &[
    "{primary} {pane_suffix} {fill} {elapsed}",
    "{secondary} {fill} {git_stats}",
    "{pane_title}",
];
const DEFAULT_HORIZONTAL_TEMPLATES: &[&str] = &[
    "{status_icon} {primary} {pane_suffix} {fill} {elapsed}",
    "{secondary} {fill} {git_stats}",
    "{pane_title}",
];

/// Parsed templates for one sidebar instance.
#[derive(Debug, Clone)]
pub struct ParsedTemplates {
    pub compact: Vec<Token>,
    pub tiles: Vec<Vec<Token>>,
    pub horizontal: Vec<Vec<Token>>,
}

/// Latest sidebar template parsing failure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemplateError {
    pub location: String,
    pub message: String,
}

impl TemplateError {
    fn new(location: impl Into<String>, error: &ParseError) -> Self {
        Self {
            location: location.into(),
            message: error.to_string(),
        }
    }

    pub fn display_message(&self) -> String {
        format!("template error: {} in {}", self.message, self.location)
    }
}

/// Lightweight sidebar app state. No preview, git, PR, diff, or input mode.
pub struct SidebarApp {
    pub mux: Arc<dyn Multiplexer>,
    pub agents: Vec<AgentPane>,
    pub has_loaded_snapshot: bool,
    pub list_state: ListState,
    pub should_quit: bool,
    pub quit_reason: Option<String>,
    pub palette: ThemePalette,
    pub status_icons: StatusIcons,
    pub spinner_frame: u8,
    pub stale_threshold_secs: u64,
    pub position: SidebarPosition,
    pub layout_mode: SidebarLayoutMode,
    /// Area where the list was last rendered (for mouse hit testing)
    pub list_area: Rect,
    /// Window prefix from config
    window_prefix: String,
    /// The sidebar's own host session (immutable, detected once at startup via TMUX_PANE)
    host_session: Option<String>,
    /// Stable tmux window ID (e.g., @42) for active-window detection
    host_window_id: Option<String>,
    /// Index of the agent in the sidebar's host window (updated each snapshot)
    pub host_agent_idx: Option<usize>,
    /// Whether this sidebar's host window is the active window in the session
    host_window_active: bool,
    selection_mode: SelectionMode,
    /// Git status per worktree path (received from daemon snapshots).
    pub git_statuses: HashMap<PathBuf, GitStatus>,
    /// PR summary per worktree path (received from daemon snapshots).
    pub pr_statuses: HashMap<PathBuf, PrSummary>,
    /// Pane IDs of agents detected as interrupted by the daemon.
    pub interrupted_pane_ids: std::collections::HashSet<String>,
    /// Pane IDs of agents manually marked as sleeping by the user.
    pub sleeping_pane_ids: std::collections::HashSet<String>,
    /// Parsed sidebar templates.
    pub templates: ParsedTemplates,
    /// Most recent template parse failure, shown in the sidebar until fixed.
    pub template_error: Option<TemplateError>,
    /// Per-agent icon and color overrides, parsed once at config load.
    pub agent_icons: ResolvedAgentIcons,
    /// Cached tile heights for hit testing (updated each render).
    pub tile_heights: Vec<usize>,
    /// Cached horizontal chip hitboxes for top bar mouse hit testing.
    pub horizontal_hitboxes: Vec<HitBox>,
    /// First agent index rendered in the horizontal top bar.
    pub first_visible_agent_idx: usize,
    /// Maximum width of each horizontal item in columns.
    pub horizontal_item_width: usize,
    /// Last `config_version` from the daemon snapshot. Increments trigger a
    /// client-side config reload.
    pub last_config_version: u64,
    /// String form of the compact template currently parsed into `templates`.
    /// Tracked so we don't re-parse on every snapshot, and so we don't retry
    /// an unchanged broken value after logging once.
    pub current_compact_str: String,
    /// String forms of tile templates currently parsed into `templates`.
    pub current_tile_strs: Vec<String>,
    /// String forms of horizontal bar templates currently parsed into `templates`.
    pub current_horizontal_strs: Vec<String>,
    /// Live sidebar width as last loaded from config. Stored for parity with
    /// other live keys; tmux pane resize is not driven from here.
    pub current_width: Option<SidebarWidth>,
    /// Last known window width (for detecting manual pane resizes).
    last_window_width: Option<u16>,
    /// Last known window height (for detecting manual top bar resizes).
    last_window_height: Option<u16>,
    /// Pending resize columns to process after debounce.
    pending_resize_cols: Option<u16>,
    /// Pending resize rows to process after debounce.
    pending_resize_rows: Option<u16>,
    /// Deadline after which pending resize should be processed.
    pub(super) resize_deadline: Option<Instant>,
}

impl SidebarApp {
    #[cfg(test)]
    pub(crate) fn test_with_template_error(template_error: TemplateError) -> Self {
        Self {
            mux: Arc::new(crate::multiplexer::TmuxBackend::new()),
            agents: Vec::new(),
            has_loaded_snapshot: true,
            list_state: ListState::default(),
            should_quit: false,
            quit_reason: None,
            palette: ThemePalette::from_config(
                &Config::default().theme,
                crate::config::ThemeMode::Dark,
            ),
            status_icons: StatusIcons::default(),
            spinner_frame: 0,
            stale_threshold_secs: 3600,
            position: SidebarPosition::Left,
            layout_mode: SidebarLayoutMode::Compact,
            list_area: Rect::default(),
            window_prefix: "wm-".to_string(),
            host_session: None,
            host_window_id: None,
            host_agent_idx: None,
            host_window_active: true,
            selection_mode: SelectionMode::FollowHost,
            git_statuses: HashMap::new(),
            pr_statuses: HashMap::new(),
            interrupted_pane_ids: std::collections::HashSet::new(),
            sleeping_pane_ids: std::collections::HashSet::new(),
            templates: ParsedTemplates {
                compact: parse_line("{primary}").unwrap(),
                tiles: vec![parse_line("{primary}").unwrap()],
                horizontal: vec![parse_line("{primary}").unwrap()],
            },
            template_error: Some(template_error),
            agent_icons: ResolvedAgentIcons::default(),
            tile_heights: Vec::new(),
            horizontal_hitboxes: Vec::new(),
            first_visible_agent_idx: 0,
            horizontal_item_width: 24,
            last_config_version: 0,
            current_compact_str: "{primary}".to_string(),
            current_tile_strs: vec!["{primary}".to_string()],
            current_horizontal_strs: vec!["{primary}".to_string()],
            current_width: None,
            last_window_width: None,
            last_window_height: None,
            pending_resize_cols: None,
            pending_resize_rows: None,
            resize_deadline: None,
        }
    }

    /// Create a new sidebar client. Does config + host detection only, no tmux polling.
    pub fn new_client(mux: Arc<dyn Multiplexer>) -> Result<Self> {
        let config = Config::load(None)?;

        let theme_mode = config
            .theme
            .mode
            .unwrap_or_else(|| match terminal_light::luma() {
                Ok(luma) if luma > 0.6 => crate::config::ThemeMode::Light,
                _ => crate::config::ThemeMode::Dark,
            });
        let palette = ThemePalette::from_config(&config.theme, theme_mode);
        let window_prefix = config.window_prefix().to_string();
        let status_icons = config.status_icons.clone();

        let (host_session, host_window_id) = detect_host_window();

        let (templates, template_error) = parse_templates(&config);
        let (current_compact_str, current_tile_strs, current_horizontal_strs) =
            resolved_template_strings(&config);
        let agent_icons = ResolvedAgentIcons::from_config(config.sidebar.agent_icons.as_ref());
        let current_width = config.sidebar.width.clone();
        let horizontal_item_width = config.sidebar.horizontal.item_width();
        let position = super::read_sidebar_position(&config);

        // Seed last_window_width so the first resize event after startup grace
        // can be compared against a baseline (fixes first-resize-dropped bug).
        let initial_window_width = query_window_width_for_pane();
        let initial_window_height = query_window_height_for_pane();

        Ok(Self {
            mux,
            agents: Vec::new(),
            has_loaded_snapshot: false,
            list_state: ListState::default(),
            should_quit: false,
            quit_reason: None,
            palette,
            status_icons,
            spinner_frame: 0,
            stale_threshold_secs: 60 * 60, // 60 minutes
            position,
            layout_mode: SidebarLayoutMode::default(),
            list_area: Rect::default(),
            window_prefix,
            host_session,
            host_window_id,
            host_agent_idx: None,
            host_window_active: true,
            selection_mode: SelectionMode::FollowHost,
            git_statuses: HashMap::new(),
            pr_statuses: HashMap::new(),
            interrupted_pane_ids: std::collections::HashSet::new(),
            sleeping_pane_ids: std::collections::HashSet::new(),
            templates,
            template_error,
            agent_icons,
            tile_heights: Vec::new(),
            horizontal_hitboxes: Vec::new(),
            first_visible_agent_idx: 0,
            horizontal_item_width,
            last_config_version: 0,
            current_compact_str,
            current_tile_strs,
            current_horizontal_strs,
            current_width,
            last_window_width: initial_window_width,
            last_window_height: initial_window_height,
            pending_resize_cols: None,
            pending_resize_rows: None,
            resize_deadline: None,
        })
    }

    /// Apply a snapshot received from the daemon.
    pub fn apply_snapshot(&mut self, snapshot: SidebarSnapshot) {
        self.has_loaded_snapshot = true;

        // Compute host agent index from the new snapshot first so that a
        // config_version bump anchors the reload to the *current* host path,
        // not whatever was selected from the previous snapshot.
        self.host_agent_idx = self.host_window_id.as_ref().and_then(|wid| {
            let mut first_match = None;
            for (i, agent) in snapshot.agents.iter().enumerate() {
                if agent.window_id != *wid {
                    continue;
                }
                if snapshot.active_pane_ids.contains(&agent.pane_id) {
                    return Some(i);
                }
                first_match.get_or_insert(i);
            }
            first_match
        });

        if snapshot.config_version != self.last_config_version {
            self.last_config_version = snapshot.config_version;
            self.reload_config_from_disk(&snapshot);
        }

        self.position = snapshot.position;
        self.layout_mode = snapshot.layout_mode;
        self.git_statuses = snapshot.git_statuses;
        self.pr_statuses = snapshot.pr_statuses;
        self.interrupted_pane_ids = snapshot.interrupted_pane_ids;
        self.sleeping_pane_ids = snapshot.sleeping_pane_ids;

        // Check if host window is active
        let was_active = self.host_window_active;
        self.host_window_active =
            if let (Some(session), Some(window_id)) = (&self.host_session, &self.host_window_id) {
                snapshot
                    .active_windows
                    .contains(&(session.clone(), window_id.clone()))
            } else {
                true
            };

        // Re-arm FollowHost when window becomes active
        if !was_active && self.host_window_active {
            self.selection_mode = SelectionMode::FollowHost;
        }

        // Preserve selection by pane_id
        let selected_pane = self
            .list_state
            .selected()
            .and_then(|i| self.agents.get(i))
            .map(|a| a.pane_id.clone());

        self.agents = snapshot.agents;

        // Restore selection
        if let Some(ref pane_id) = selected_pane {
            if let Some(idx) = self.agents.iter().position(|a| &a.pane_id == pane_id) {
                self.list_state.select(Some(idx));
            } else if !self.agents.is_empty() {
                let clamped = self
                    .list_state
                    .selected()
                    .unwrap_or(0)
                    .min(self.agents.len() - 1);
                self.list_state.select(Some(clamped));
            } else {
                self.list_state.select(None);
            }
        } else if !self.agents.is_empty() && self.list_state.selected().is_none() {
            self.list_state.select(Some(0));
        }

        self.sync_selection();
    }

    /// Select the agent belonging to this sidebar's host window (only in FollowHost mode).
    pub fn sync_selection(&mut self) {
        if self.selection_mode != SelectionMode::FollowHost {
            return;
        }
        if let Some(idx) = self.host_agent_idx {
            self.list_state.select(Some(idx));
        }
    }

    /// Re-read the merged config from disk and apply live-reloadable fields:
    /// templates, agent icons, and width. Templates are anchored at the host
    /// agent's worktree path so per-project `.workmux.yaml` overrides are
    /// honored. On any parse error, keep the previously valid templates.
    fn reload_config_from_disk(&mut self, snapshot: &SidebarSnapshot) {
        let host_path = self
            .host_agent_idx
            .and_then(|i| snapshot.agents.get(i))
            .map(|a| a.path.clone());

        let cfg_result = match host_path.as_ref() {
            Some(p) => Config::load_with_location_from(p, None).map(|(c, _)| c),
            None => Config::load(None),
        };
        let cfg = match cfg_result {
            Ok(c) => c,
            Err(e) => {
                tracing::warn!("client config reload failed: {}", e);
                return;
            }
        };

        let (new_compact, new_tiles, new_horizontal) = resolved_template_strings(&cfg);
        if new_compact != self.current_compact_str
            || new_tiles != self.current_tile_strs
            || new_horizontal != self.current_horizontal_strs
        {
            self.template_error = try_reparse_templates(
                &mut self.templates,
                &mut self.current_compact_str,
                &mut self.current_tile_strs,
                &mut self.current_horizontal_strs,
                &new_compact,
                &new_tiles,
                &new_horizontal,
            );
        }

        self.agent_icons = ResolvedAgentIcons::from_config(cfg.sidebar.agent_icons.as_ref());
        self.horizontal_item_width = cfg.sidebar.horizontal.item_width();
        self.current_width = cfg.sidebar.width.clone();
    }

    pub fn host_window_id(&self) -> Option<&str> {
        self.host_window_id.as_deref()
    }

    pub fn host_window_active(&self) -> bool {
        self.host_window_active
    }

    pub fn tick(&mut self) {
        self.spinner_frame = self.spinner_frame.wrapping_add(1) % 10;
    }

    pub fn next(&mut self) {
        self.selection_mode = SelectionMode::Manual;
        if self.agents.is_empty() {
            return;
        }
        let i = self.list_state.selected().unwrap_or(0);
        let next = if i >= self.agents.len() - 1 { 0 } else { i + 1 };
        self.list_state.select(Some(next));
    }

    pub fn previous(&mut self) {
        self.selection_mode = SelectionMode::Manual;
        if self.agents.is_empty() {
            return;
        }
        let i = self.list_state.selected().unwrap_or(0);
        let prev = if i == 0 { self.agents.len() - 1 } else { i - 1 };
        self.list_state.select(Some(prev));
    }

    pub fn select_first(&mut self) {
        self.selection_mode = SelectionMode::Manual;
        if !self.agents.is_empty() {
            self.list_state.select(Some(0));
        }
    }

    pub fn select_last(&mut self) {
        self.selection_mode = SelectionMode::Manual;
        if !self.agents.is_empty() {
            self.list_state.select(Some(self.agents.len() - 1));
        }
    }

    pub fn select_index(&mut self, idx: usize) {
        self.selection_mode = SelectionMode::Manual;
        if !self.agents.is_empty() {
            self.list_state.select(Some(idx.min(self.agents.len() - 1)));
        }
    }

    pub fn scroll_up(&mut self) {
        self.selection_mode = SelectionMode::Manual;
        if let Some(i) = self.list_state.selected() {
            self.list_state.select(Some(i.saturating_sub(1)));
        }
    }

    pub fn scroll_down(&mut self) {
        self.selection_mode = SelectionMode::Manual;
        if let Some(i) = self.list_state.selected() {
            let last = self.agents.len().saturating_sub(1);
            self.list_state.select(Some((i + 1).min(last)));
        }
    }

    pub fn hit_test(&self, column: u16, row: u16) -> Option<usize> {
        if self.agents.is_empty() {
            return None;
        }
        let area = self.list_area;
        if row < area.y || row >= area.y + area.height {
            return None;
        }

        if self.position == SidebarPosition::Top {
            return self
                .horizontal_hitboxes
                .iter()
                .find(|hit| column >= hit.x_start && column < hit.x_end)
                .map(|hit| hit.idx);
        }

        let relative_row = (row - area.y) as usize;
        let offset = self.list_state.offset();

        match self.layout_mode {
            SidebarLayoutMode::Compact => {
                let idx = offset + relative_row;
                (idx < self.agents.len()).then_some(idx)
            }
            SidebarLayoutMode::Tiles => {
                let mut y = 0;
                for idx in offset..self.agents.len() {
                    let h = self.tile_item_height(idx);
                    if relative_row < y + h {
                        return Some(idx);
                    }
                    y += h;
                }
                None
            }
        }
    }

    pub fn ensure_selected_visible(&mut self, visible_count: usize) {
        let Some(selected) = self.list_state.selected() else {
            return;
        };
        if selected < self.first_visible_agent_idx {
            self.first_visible_agent_idx = selected;
        } else if visible_count > 0 && selected >= self.first_visible_agent_idx + visible_count {
            self.first_visible_agent_idx = selected + 1 - visible_count;
        }
    }

    /// Height in rows of a tile-mode item at the given index.
    /// Uses cached heights from the last render pass.
    fn tile_item_height(&self, idx: usize) -> usize {
        let base = self.tile_heights.get(idx).copied().unwrap_or(3);
        let mut h = base;
        if idx > 0 {
            h += 1; // top separator
        }
        if idx == self.agents.len() - 1 {
            h += 1; // bottom separator
        }
        h
    }

    pub fn jump_to_selected(&mut self) {
        if let Some(idx) = self.list_state.selected()
            && let Some(agent) = self.agents.get(idx)
        {
            let pane_id = agent.pane_id.clone();
            let _ = self.mux.switch_to_pane(&pane_id, None);
            // Signal daemon directly to bypass tmux hook round-trip latency
            super::daemon_ctrl::signal_daemon();
        }
    }

    pub fn toggle_layout_mode(&mut self) {
        if self.position == SidebarPosition::Top {
            return;
        }
        self.layout_mode = match self.layout_mode {
            SidebarLayoutMode::Compact => SidebarLayoutMode::Tiles,
            SidebarLayoutMode::Tiles => SidebarLayoutMode::Compact,
        };
        // Persist to tmux so all sidebar instances pick it up immediately
        let _ = Cmd::new("tmux")
            .args(&[
                "set-option",
                "-g",
                "@workmux_sidebar_layout",
                self.layout_mode.as_str(),
            ])
            .run();
        // Persist to settings.json so it survives tmux restarts
        if let Ok(store) = crate::state::StateStore::new()
            && let Ok(mut settings) = store.load_settings()
        {
            settings.sidebar_layout = Some(self.layout_mode.as_str().to_string());
            let _ = store.save_settings(&settings);
        }
    }

    /// Toggle the sleeping state of the selected agent.
    /// Does a read-modify-write on the tmux global option so concurrent
    /// toggles from different sidebar clients don't clobber each other.
    pub fn toggle_sleeping(&mut self) {
        let Some(pane_id) = self
            .list_state
            .selected()
            .and_then(|i| self.agents.get(i))
            .map(|a| a.pane_id.clone())
        else {
            return;
        };

        // Read current set from tmux (source of truth) to avoid losing
        // toggles made by other sidebar clients since our last snapshot.
        let mut current: std::collections::HashSet<String> = Cmd::new("tmux")
            .args(&["show-option", "-gqv", "@workmux_sleeping_panes"])
            .run_and_capture_stdout()
            .ok()
            .map(|s| s.split_whitespace().map(String::from).collect())
            .unwrap_or_default();

        if !current.insert(pane_id.clone()) {
            current.remove(&pane_id);
        }

        // Update local state for immediate rendering
        self.sleeping_pane_ids = current.clone();

        // Write back to tmux
        let panes: String = current.into_iter().collect::<Vec<_>>().join(" ");
        if panes.is_empty() {
            let _ = Cmd::new("tmux")
                .args(&["set-option", "-gu", "@workmux_sleeping_panes"])
                .run();
        } else {
            let _ = Cmd::new("tmux")
                .args(&["set-option", "-g", "@workmux_sleeping_panes", &panes])
                .run();
        }

        // Signal daemon for immediate refresh (re-sort + broadcast)
        super::daemon_ctrl::signal_daemon();
    }

    pub fn window_prefix(&self) -> &str {
        &self.window_prefix
    }

    /// Record a resize event for debounced manual pane resize processing.
    pub fn on_resize_event(&mut self, cols: u16, rows: u16) {
        match self.position {
            SidebarPosition::Left => {
                let window_w = self.query_host_window_width();
                if self.last_window_width.is_some_and(|prev| prev != window_w) {
                    self.last_window_width = Some(window_w);
                    self.pending_resize_cols = None;
                    self.pending_resize_rows = None;
                    self.resize_deadline = None;
                    let _ = super::reflow_all_to_window_extent(Some(window_w));
                    return;
                }
                self.pending_resize_cols = Some(cols);
            }
            SidebarPosition::Top => {
                let window_h = self.query_host_window_height();
                if self.last_window_height.is_some_and(|prev| prev != window_h) {
                    self.last_window_height = Some(window_h);
                    self.pending_resize_cols = None;
                    self.pending_resize_rows = None;
                    self.resize_deadline = None;
                    let _ = super::reflow_all_to_window_extent(Some(window_h));
                    return;
                }
                self.pending_resize_rows = Some(rows);
            }
        }

        self.resize_deadline = Some(Instant::now() + Duration::from_millis(500));
    }

    /// Process any pending resize after the debounce period has elapsed.
    /// Skips detection during startup grace period.
    pub fn process_pending_resize(&mut self, startup: &Instant, startup_grace: Duration) {
        if startup.elapsed() < startup_grace {
            // Suppress detection during startup to avoid false positives from
            // initial pane creation layout divergence.
            self.pending_resize_cols = None;
            self.pending_resize_rows = None;
            self.resize_deadline = None;
            return;
        }

        let Some(deadline) = self.resize_deadline else {
            return;
        };
        if Instant::now() < deadline {
            return;
        }

        let config = Config::load(None).unwrap_or_default();
        match self.position {
            SidebarPosition::Left => {
                let Some(pane_width) = self.pending_resize_cols else {
                    self.resize_deadline = None;
                    return;
                };
                let window_w = self.query_host_window_width();
                let prev_window_w = self.last_window_width;
                self.last_window_width = Some(window_w);
                self.pending_resize_cols = None;
                self.pending_resize_rows = None;
                self.resize_deadline = None;
                let Some(prev_ww) = prev_window_w else { return };
                if prev_ww != window_w {
                    return;
                }
                let actual_width = query_pane_width_for_pane().unwrap_or(pane_width);
                let expected = super::effective_width_for(&config, window_w);
                let delta = (actual_width as i16 - expected as i16).abs();
                if delta > 0 {
                    super::set_sidebar_width(actual_width);
                    if let Some(wid) = self.host_window_id() {
                        super::reflow_all_sidebars_except(wid);
                    }
                }
            }
            SidebarPosition::Top => {
                let Some(pane_height) = self.pending_resize_rows else {
                    self.resize_deadline = None;
                    return;
                };
                let window_h = self.query_host_window_height();
                let prev_window_h = self.last_window_height;
                self.last_window_height = Some(window_h);
                self.pending_resize_cols = None;
                self.pending_resize_rows = None;
                self.resize_deadline = None;
                let Some(prev_wh) = prev_window_h else { return };
                if prev_wh != window_h {
                    return;
                }
                let actual_height = query_pane_height_for_pane().unwrap_or(pane_height);
                let expected = super::effective_height_for(&config, window_h);
                let delta = (actual_height as i16 - expected as i16).abs();
                if delta > 0 {
                    super::set_sidebar_height(actual_height);
                    if let Some(wid) = self.host_window_id() {
                        super::reflow_all_sidebars_except(wid);
                    }
                }
            }
        }
    }

    fn query_host_window_width(&self) -> u16 {
        query_window_width_for_pane().unwrap_or(0)
    }

    fn query_host_window_height(&self) -> u16 {
        query_window_height_for_pane().unwrap_or(0)
    }

    /// Resolve the (primary, secondary) label pair for an agent row.
    ///
    /// Strips the workmux prefix from session/window names so the resolver only
    /// considers user-authored values. The window name is never promoted for
    /// non-tmux backends (signaled by `window_cmd: None`).
    pub fn resolve_agent_labels(&self, agent: &AgentPane) -> (String, String) {
        let project = extract_project_name(&agent.path);
        let (worktree, _is_main) = extract_worktree_name(
            &agent.session,
            &agent.window_name,
            &self.window_prefix,
            &agent.path,
        );

        // Workmux-managed names start with the configured prefix; treat them as
        // not user-authored by clearing them before the resolver sees them.
        let session = if agent.session.starts_with(&self.window_prefix) {
            ""
        } else {
            agent.session.as_str()
        };
        let window = if agent.window_name.starts_with(&self.window_prefix) {
            ""
        } else {
            agent.window_name.as_str()
        };

        resolve_labels(
            &project,
            session,
            &worktree,
            window,
            agent.window_cmd.as_deref(),
        )
    }
}

/// Resolve template strings from config, falling back to defaults.
fn resolved_template_strings(config: &Config) -> (String, Vec<String>, Vec<String>) {
    let compact = config
        .sidebar
        .templates
        .as_ref()
        .and_then(|t| t.compact.clone())
        .unwrap_or_else(|| DEFAULT_COMPACT_TEMPLATE.to_string());
    let tiles = config
        .sidebar
        .templates
        .as_ref()
        .and_then(|t| t.tiles.clone())
        .unwrap_or_else(|| {
            DEFAULT_TILE_TEMPLATES
                .iter()
                .map(|s| s.to_string())
                .collect()
        });
    let horizontal = config
        .sidebar
        .templates
        .as_ref()
        .and_then(|t| t.horizontal.clone())
        .unwrap_or_else(|| {
            DEFAULT_HORIZONTAL_TEMPLATES
                .iter()
                .map(|s| s.to_string())
                .collect()
        });
    (compact, tiles, horizontal)
}

fn default_template_lines(default_lines: &[&str]) -> Vec<Vec<Token>> {
    default_lines
        .iter()
        .map(|s| parse_line(s).expect("default template is valid"))
        .collect()
}

fn parse_template_lines(lines: &[String], kind: &str) -> Result<Vec<Vec<Token>>, TemplateError> {
    lines
        .iter()
        .enumerate()
        .map(|(i, line)| {
            parse_line(line).map_err(|e| {
                let location = format!("{kind}[{i}]");
                tracing::warn!("failed to parse {location} template '{}': {}", line, e);
                TemplateError::new(location, &e)
            })
        })
        .collect()
}

fn parse_templates(config: &Config) -> (ParsedTemplates, Option<TemplateError>) {
    let (compact_str, tile_strs, horizontal_strs) = resolved_template_strings(config);
    let mut first_error = None;

    let compact = match parse_line(&compact_str) {
        Ok(tokens) => tokens,
        Err(e) => {
            tracing::warn!("failed to parse compact template: {}, using default", e);
            first_error.get_or_insert_with(|| TemplateError::new("compact", &e));
            parse_line(DEFAULT_COMPACT_TEMPLATE).expect("default template is valid")
        }
    };
    let tiles = match parse_template_lines(&tile_strs, "tiles") {
        Ok(tokens) => tokens,
        Err(e) => {
            first_error.get_or_insert(e);
            default_template_lines(DEFAULT_TILE_TEMPLATES)
        }
    };
    let horizontal = match parse_template_lines(&horizontal_strs, "horizontal") {
        Ok(tokens) => tokens,
        Err(e) => {
            first_error.get_or_insert(e);
            default_template_lines(DEFAULT_HORIZONTAL_TEMPLATES)
        }
    };

    (
        ParsedTemplates {
            compact,
            tiles,
            horizontal,
        },
        first_error,
    )
}

/// Query the window width for the current tmux pane (standalone for use before
/// `Self` exists).
fn query_window_width_for_pane() -> Option<u16> {
    let pane_id = std::env::var("TMUX_PANE").unwrap_or_default();
    let mut args = vec!["display-message", "-p"];
    if !pane_id.is_empty() {
        args.extend_from_slice(&["-t", &pane_id]);
    }
    args.push("#{window_width}");
    Cmd::new("tmux")
        .args(&args)
        .run_and_capture_stdout()
        .ok()
        .and_then(|s| s.trim().parse().ok())
}

fn query_window_height_for_pane() -> Option<u16> {
    let pane_id = std::env::var("TMUX_PANE").unwrap_or_default();
    let mut args = vec!["display-message", "-p"];
    if !pane_id.is_empty() {
        args.extend_from_slice(&["-t", &pane_id]);
    }
    args.push("#{window_height}");
    Cmd::new("tmux")
        .args(&args)
        .run_and_capture_stdout()
        .ok()
        .and_then(|s| s.trim().parse().ok())
}

/// Query the actual pane width from tmux. Used to verify the sidebar pane
/// size after a manual resize, since crossterm's SIGWINCH-derived cols may
/// differ from what tmux reports via #{pane_width}.
fn query_pane_width_for_pane() -> Option<u16> {
    query_pane_extent_for_pane("#{pane_width}")
}

fn query_pane_height_for_pane() -> Option<u16> {
    query_pane_extent_for_pane("#{pane_height}")
}

fn query_pane_extent_for_pane(format: &str) -> Option<u16> {
    let pane_id = std::env::var("TMUX_PANE").unwrap_or_default();
    let mut args = vec!["display-message", "-p"];
    if !pane_id.is_empty() {
        args.extend_from_slice(&["-t", &pane_id]);
    }
    args.push(format);
    Cmd::new("tmux")
        .args(&args)
        .run_and_capture_stdout()
        .ok()
        .and_then(|s| s.trim().parse().ok())
        .filter(|&extent| extent > 0)
}

/// Parse new template strings, mutating `templates` and the cached strings.
/// On any parse error, keep `templates` as-is and log a warning. The cached
/// strings are still updated so we don't retry the same broken value on every
/// snapshot.
fn try_reparse_templates(
    templates: &mut ParsedTemplates,
    current_compact_str: &mut String,
    current_tile_strs: &mut Vec<String>,
    current_horizontal_strs: &mut Vec<String>,
    new_compact: &str,
    new_tiles: &[String],
    new_horizontal: &[String],
) -> Option<TemplateError> {
    let mut first_error = None;

    match parse_line(new_compact) {
        Ok(tokens) => templates.compact = tokens,
        Err(e) => {
            tracing::warn!("compact template parse error, keeping previous: {}", e);
            first_error.get_or_insert_with(|| TemplateError::new("compact", &e));
        }
    }

    match parse_template_lines(new_tiles, "tiles") {
        Ok(tokens) => templates.tiles = tokens,
        Err(e) => {
            tracing::warn!(
                "{} template parse error, keeping previous: {}",
                e.location,
                e.message
            );
            first_error.get_or_insert(e);
        }
    }

    match parse_template_lines(new_horizontal, "horizontal") {
        Ok(tokens) => templates.horizontal = tokens,
        Err(e) => {
            tracing::warn!(
                "{} template parse error, keeping previous: {}",
                e.location,
                e.message
            );
            first_error.get_or_insert(e);
        }
    }

    *current_compact_str = new_compact.to_string();
    *current_tile_strs = new_tiles.to_vec();
    *current_horizontal_strs = new_horizontal.to_vec();
    first_error
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{AgentIconConfig, AgentIconDetails};

    #[test]
    fn resolved_icons_legacy_string() {
        let mut map = AgentIcons::new();
        map.insert(
            "claude".to_string(),
            AgentIconConfig::Plain("C".to_string()),
        );
        let r = ResolvedAgentIcons::from_config(Some(&map));
        assert_eq!(r.icons.get("claude").map(String::as_str), Some("C"));
        assert!(r.colors.is_empty());
    }

    #[test]
    fn resolved_icons_detailed_with_valid_color() {
        let mut map = AgentIcons::new();
        map.insert(
            "claude".to_string(),
            AgentIconConfig::Detailed(AgentIconDetails {
                icon: Some("X".to_string()),
                color: Some("#00ff00".to_string()),
            }),
        );
        let r = ResolvedAgentIcons::from_config(Some(&map));
        assert_eq!(r.icons.get("claude").map(String::as_str), Some("X"));
        assert_eq!(r.colors.get("claude"), Some(&Some(Color::Rgb(0, 255, 0))));
    }

    #[test]
    fn resolved_icons_blank_color_disables_default() {
        let mut map = AgentIcons::new();
        map.insert(
            "claude".to_string(),
            AgentIconConfig::Detailed(AgentIconDetails {
                icon: None,
                color: Some("   ".to_string()),
            }),
        );
        let r = ResolvedAgentIcons::from_config(Some(&map));
        assert_eq!(r.colors.get("claude"), Some(&None));
    }

    #[test]
    fn resolved_icons_invalid_color_is_dropped() {
        let mut map = AgentIcons::new();
        map.insert(
            "claude".to_string(),
            AgentIconConfig::Detailed(AgentIconDetails {
                icon: None,
                color: Some("not-a-color".to_string()),
            }),
        );
        let r = ResolvedAgentIcons::from_config(Some(&map));
        // No entry: lookup falls through to AgentKind::default_color at use site.
        assert!(!r.colors.contains_key("claude"));
    }

    #[test]
    fn resolved_icons_null_variant_is_no_op() {
        let mut map = AgentIcons::new();
        map.insert("claude".to_string(), AgentIconConfig::Null);
        let r = ResolvedAgentIcons::from_config(Some(&map));
        assert!(r.icons.is_empty());
        assert!(r.colors.is_empty());
    }

    fn parsed_for(s: &str) -> ParsedTemplates {
        ParsedTemplates {
            compact: parse_line(s).unwrap(),
            tiles: vec![parse_line(s).unwrap()],
            horizontal: vec![parse_line(s).unwrap()],
        }
    }

    #[test]
    fn reparse_swaps_templates_on_change() {
        let mut templates = parsed_for("{primary}");
        let mut compact = "{primary}".to_string();
        let mut tiles = vec!["{primary}".to_string()];
        let mut top = vec!["{primary}".to_string()];

        let new_compact = "{secondary} {fill}";
        let new_tiles = vec!["{primary} {fill} {elapsed}".to_string()];
        let new_top = vec!["{secondary} {fill} {git_stats}".to_string()];
        let error = try_reparse_templates(
            &mut templates,
            &mut compact,
            &mut tiles,
            &mut top,
            new_compact,
            &new_tiles,
            &new_top,
        );

        assert_eq!(error, None);
        assert_eq!(compact, new_compact);
        assert_eq!(tiles, new_tiles);
        assert_eq!(top, new_top);
        // 3 tokens: secondary field, literal " ", fill
        assert_eq!(templates.compact.len(), 3);
    }

    #[test]
    fn reparse_keeps_previous_on_compact_parse_error() {
        let original_str = "{primary}".to_string();
        let mut templates = parsed_for(&original_str);
        let original_tokens = templates.compact.clone();
        let mut compact = original_str.clone();
        let mut tiles = vec![original_str.clone()];
        let mut top = vec![original_str.clone()];

        let bad_compact = "{unclosed";
        let error = try_reparse_templates(
            &mut templates,
            &mut compact,
            &mut tiles,
            &mut top,
            bad_compact,
            &[original_str.clone()],
            &[original_str.clone()],
        );

        assert_eq!(
            error,
            Some(TemplateError {
                location: "compact".to_string(),
                message: "unclosed brace at column 1: '{unclosed'".to_string(),
            })
        );
        // Templates unchanged
        assert_eq!(templates.compact, original_tokens);
        // But cached strings updated so we don't retry the broken value
        assert_eq!(compact, bad_compact);
    }

    #[test]
    fn reparse_keeps_previous_on_tile_parse_error() {
        let mut templates = parsed_for("{primary}");
        let original_tiles = templates.tiles.clone();
        let mut compact = "{primary}".to_string();
        let mut tiles = vec!["{primary}".to_string()];
        let mut top = vec!["{primary}".to_string()];

        let error = try_reparse_templates(
            &mut templates,
            &mut compact,
            &mut tiles,
            &mut top,
            "{primary}",
            &["{pr_status}".to_string()],
            &["{primary}".to_string()],
        );

        assert_eq!(templates.tiles, original_tiles);
        assert_eq!(tiles, vec!["{pr_status}".to_string()]);
        assert_eq!(
            error,
            Some(TemplateError {
                location: "tiles[0]".to_string(),
                message: "unknown token 'pr_status' at column 1".to_string(),
            })
        );
    }

    #[test]
    fn parse_templates_reports_invalid_horizontal_template() {
        let mut config = Config::default();
        config.sidebar.templates = Some(crate::config::TemplatesConfig {
            horizontal: Some(vec!["{primary}".to_string(), "{pr_status}".to_string()]),
            ..Default::default()
        });

        let (templates, error) = parse_templates(&config);

        assert_eq!(
            templates.horizontal,
            default_template_lines(DEFAULT_HORIZONTAL_TEMPLATES)
        );
        assert_eq!(
            error,
            Some(TemplateError {
                location: "horizontal[1]".to_string(),
                message: "unknown token 'pr_status' at column 1".to_string(),
            })
        );
    }

    #[test]
    fn parse_templates_reports_first_error() {
        let mut config = Config::default();
        config.sidebar.templates = Some(crate::config::TemplatesConfig {
            compact: Some("{bad_compact}".to_string()),
            tiles: Some(vec!["{pr_status}".to_string()]),
            ..Default::default()
        });

        let (_, error) = parse_templates(&config);

        assert_eq!(
            error,
            Some(TemplateError {
                location: "compact".to_string(),
                message: "unknown token 'bad_compact' at column 1".to_string(),
            })
        );
    }

    #[test]
    fn reparse_updates_valid_sections_when_tile_parse_fails() {
        let mut templates = parsed_for("{primary}");
        let mut compact = "{primary}".to_string();
        let mut tiles = vec!["{primary}".to_string()];
        let mut top = vec!["{primary}".to_string()];

        let error = try_reparse_templates(
            &mut templates,
            &mut compact,
            &mut tiles,
            &mut top,
            "{secondary}",
            &["{pr_status}".to_string()],
            &["{elapsed}".to_string()],
        );

        assert_eq!(templates.compact, parse_line("{secondary}").unwrap());
        assert_eq!(templates.tiles, vec![parse_line("{primary}").unwrap()]);
        assert_eq!(templates.horizontal, vec![parse_line("{elapsed}").unwrap()]);
        assert_eq!(
            error,
            Some(TemplateError {
                location: "tiles[0]".to_string(),
                message: "unknown token 'pr_status' at column 1".to_string(),
            })
        );
    }
}

/// Detect this sidebar's host window using TMUX_PANE (stable, one-time).
/// Returns (session, window_id).
fn detect_host_window() -> (Option<String>, Option<String>) {
    let pane_id = std::env::var("TMUX_PANE").ok().unwrap_or_default();
    let mut args = vec!["display-message", "-p"];
    if !pane_id.is_empty() {
        args.extend_from_slice(&["-t", &pane_id]);
    }
    args.push("#{session_name}\t#{window_id}");
    let output = Cmd::new("tmux")
        .args(&args)
        .run_and_capture_stdout()
        .ok()
        .unwrap_or_default();
    let trimmed = output.trim();
    let mut parts = trimmed
        .split('\t')
        .map(|s| (!s.is_empty()).then(|| s.to_string()));
    let session = parts.next().flatten();
    let window_id = parts.next().flatten();
    (session, window_id)
}