-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtmux_provider.rs
More file actions
879 lines (784 loc) · 27 KB
/
Copy pathtmux_provider.rs
File metadata and controls
879 lines (784 loc) · 27 KB
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
use std::collections::{HashMap, HashSet};
use std::process::Command;
use std::sync::Arc;
use crate::mux::{
ActiveWindow, AgentPane, MuxProvider, MuxSessionInfo, SidebarPane, SidebarPosition,
};
const SEP: &str = "\t";
const STASH_SESSION: &str = "_os_stash";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandOutput {
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
}
impl CommandOutput {
pub fn ok(&self) -> bool {
self.exit_code == 0
}
}
pub trait CommandRunner: Send + Sync {
fn run(&self, args: &[String]) -> CommandOutput;
}
#[derive(Debug, Clone)]
pub struct StdCommandRunner {
binary: String,
}
impl StdCommandRunner {
pub fn new(binary: impl Into<String>) -> Self {
Self {
binary: binary.into(),
}
}
}
impl Default for StdCommandRunner {
fn default() -> Self {
Self::new("tmux")
}
}
impl CommandRunner for StdCommandRunner {
fn run(&self, args: &[String]) -> CommandOutput {
match Command::new(&self.binary).args(args).output() {
Ok(output) => CommandOutput {
exit_code: output.status.code().unwrap_or(1),
stdout: String::from_utf8_lossy(&output.stdout).trim().to_string(),
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
},
Err(err) => CommandOutput {
exit_code: 1,
stdout: String::new(),
stderr: err.to_string(),
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionInfo {
pub id: String,
pub name: String,
pub created_at: u64,
pub attached_clients: u32,
pub window_count: u32,
pub dir: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WindowInfo {
pub id: String,
pub session_id: String,
pub session_name: String,
pub index: u32,
pub name: String,
pub active: bool,
pub pane_count: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PaneInfo {
pub id: String,
pub session_name: String,
pub window_id: String,
pub window_index: u32,
pub index: u32,
pub active: bool,
pub tty: String,
pub pid: u32,
pub cwd: String,
pub command: String,
pub title: String,
pub width: u16,
pub height: u16,
pub left: u16,
pub right: u16,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientInfo {
pub name: String,
pub tty: String,
pub pid: u32,
pub session_name: String,
pub width: u16,
pub height: u16,
}
#[derive(Clone)]
pub struct TmuxClient {
runner: Arc<dyn CommandRunner>,
}
impl TmuxClient {
pub fn new(runner: Arc<dyn CommandRunner>) -> Self {
Self { runner }
}
pub fn run(&self, args: &[&str]) -> CommandOutput {
let args = args
.iter()
.map(|arg| (*arg).to_string())
.collect::<Vec<_>>();
self.runner.run(&args)
}
pub fn list_sessions(&self) -> Vec<SessionInfo> {
parse_sessions(&self.run(&["list-sessions", "-F", session_format()]).stdout)
}
pub fn list_windows(&self) -> Vec<WindowInfo> {
parse_windows(
&self
.run(&["list-windows", "-a", "-F", window_format()])
.stdout,
)
}
pub fn list_clients(&self) -> Vec<ClientInfo> {
parse_clients(&self.run(&["list-clients", "-F", client_format()]).stdout)
}
pub fn list_panes(&self, scope: PaneScope<'_>) -> Vec<PaneInfo> {
let mut args = vec!["list-panes"];
match scope {
PaneScope::All => args.push("-a"),
PaneScope::Session(target) => {
args.push("-s");
args.push("-t");
args.push(target);
}
PaneScope::Window(target) => {
args.push("-t");
args.push(target);
}
}
args.push("-F");
args.push(pane_format());
parse_panes(&self.run(&args).stdout)
}
pub fn switch_client(&self, target: &str, client_tty: Option<&str>) {
let mut args = vec!["switch-client"];
if let Some(client_tty) = client_tty {
args.push("-c");
args.push(client_tty);
}
args.push("-t");
args.push(target);
self.run(&args);
}
pub fn new_session(&self, name: Option<&str>, cwd: Option<&str>) -> String {
let mut args = vec!["new-session", "-d"];
if let Some(name) = name {
args.push("-s");
args.push(name);
}
if let Some(cwd) = cwd {
args.push("-c");
args.push(cwd);
}
args.extend(["-P", "-F", "#{session_name}"]);
self.run(&args).stdout
}
pub fn kill_session(&self, target: &str) {
self.run(&["kill-session", "-t", target]);
}
pub fn kill_pane(&self, target: &str) {
self.run(&["kill-pane", "-t", target]);
}
pub fn select_window(&self, target: &str) {
self.run(&["select-window", "-t", target]);
}
pub fn select_pane(&self, target: &str) {
self.run(&["select-pane", "-t", target]);
}
pub fn set_pane_title(&self, target: &str, title: &str) {
self.run(&["select-pane", "-t", target, "-T", title]);
}
pub fn resize_pane_width(&self, target: &str, width: u16) {
self.run(&["resize-pane", "-t", target, "-x", &width.to_string()]);
}
pub fn split_sidebar_pane(
&self,
target: &str,
before: bool,
width: u16,
command: &str,
) -> Option<PaneInfo> {
let size = width.to_string();
let side = if before { "-hb" } else { "-h" };
let output = self.run(&[
"split-window",
side,
"-f",
"-l",
&size,
"-t",
target,
"-P",
"-F",
pane_format(),
command,
]);
if !output.ok() || output.stdout.is_empty() {
return None;
}
parse_panes(&output.stdout).into_iter().next()
}
pub fn display(&self, format: &str, target: Option<&str>) -> String {
let mut args = vec!["display-message"];
if let Some(target) = target {
args.push("-t");
args.push(target);
}
args.push("-p");
args.push(format);
self.run(&args).stdout
}
pub fn get_current_session(&self) -> Option<String> {
self.list_clients()
.into_iter()
.find(|client| !client.tty.is_empty())
.and_then(|client| (!client.session_name.is_empty()).then_some(client.session_name))
}
pub fn get_client_tty(&self) -> String {
self.display("#{client_tty}", None)
}
pub fn get_current_window_id(&self) -> Option<String> {
let window_id = self.display("#{window_id}", None);
(!window_id.is_empty()).then_some(window_id)
}
pub fn get_session_dir(&self, target: &str) -> String {
self.display("#{pane_current_path}", Some(target))
}
pub fn get_pane_count(&self, target: &str) -> u32 {
self.list_panes(PaneScope::Session(target)).len() as u32
}
pub fn get_all_pane_counts(&self) -> HashMap<String, u32> {
let mut counts = HashMap::new();
for pane in self.list_panes(PaneScope::All) {
*counts.entry(pane.session_name).or_insert(0) += 1;
}
counts
}
pub fn get_active_session_dirs(&self) -> HashMap<String, String> {
let output = self.run(&[
"list-panes",
"-a",
"-f",
"#{&&:#{window_active},#{!=:#{pane_title},opensessions-sidebar}}",
"-F",
"#{session_name}\t#{pane_current_path}",
]);
let mut dirs = HashMap::new();
for line in output.stdout.lines() {
let Some((session, cwd)) = line.split_once(SEP) else {
continue;
};
dirs.entry(session.to_string())
.or_insert_with(|| cwd.to_string());
}
dirs
}
pub fn set_global_hook(&self, name: &str, command: &str) {
self.run(&["set-hook", "-g", name, command]);
}
pub fn unset_global_hook(&self, name: &str) {
self.run(&["set-hook", "-gu", name]);
}
}
pub enum PaneScope<'a> {
All,
Session(&'a str),
Window(&'a str),
}
#[derive(Clone)]
pub struct TmuxProvider {
name: String,
client: TmuxClient,
}
impl TmuxProvider {
pub fn new(runner: Arc<dyn CommandRunner>) -> Self {
Self {
name: "tmux".to_string(),
client: TmuxClient::new(runner),
}
}
}
impl MuxProvider for TmuxProvider {
fn name(&self) -> &str {
&self.name
}
fn list_sessions(&self) -> Vec<MuxSessionInfo> {
let active_dirs = self.client.get_active_session_dirs();
self.client
.list_sessions()
.into_iter()
.filter(|session| session.name != STASH_SESSION)
.map(|session| MuxSessionInfo {
name: session.name.clone(),
created_at: session.created_at,
dir: active_dirs
.get(&session.name)
.cloned()
.unwrap_or(session.dir),
windows: session.window_count,
})
.collect()
}
fn switch_session(&self, name: &str, client_tty: Option<&str>) {
self.client.switch_client(name, client_tty);
}
fn get_current_session(&self) -> Option<String> {
self.client.get_current_session()
}
fn get_session_dir(&self, name: &str) -> String {
self.client.get_session_dir(name)
}
fn get_session_pane_pids(&self, name: &str) -> Vec<u32> {
self.client
.list_panes(PaneScope::Session(name))
.into_iter()
.map(|pane| pane.pid)
.filter(|pid| *pid > 0)
.collect()
}
fn get_pane_count(&self, name: &str) -> u32 {
self.client.get_pane_count(name)
}
fn get_client_tty(&self) -> String {
self.client.get_client_tty()
}
fn create_session(&self, name: Option<&str>, dir: Option<&str>) {
self.client.new_session(name, dir);
}
fn kill_session(&self, name: &str) {
self.client.kill_session(name);
}
fn cleanup_sidebar(&self) {
self.client.kill_session(STASH_SESSION);
}
fn setup_hooks(&self, server_host: &str, server_port: u16) {
let base = format!("http://{server_host}:{server_port}");
let hook = |path: &str, data: Option<&str>| {
let body = data.map(|data| format!(" -d '{data}'")).unwrap_or_default();
format!(
"run-shell -b \"curl -s -o /dev/null -m 0.2 --connect-timeout 0.1 -X POST {base}{path}{body} >/dev/null 2>&1 || true\""
)
};
let focus_cmd = hook("/focus", Some("#{client_tty}|#{session_name}|#{window_id}"));
let refresh_cmd = hook("/refresh", None);
let ensure_cmd = hook(
"/ensure-sidebar",
Some("#{client_tty}|#{session_name}|#{window_id}"),
);
let client_resized_cmd = hook("/client-resized", None);
let pane_exited_cmd = hook("/pane-exited", None);
self.client.set_global_hook(
"client-session-changed",
&format!("{focus_cmd} ; {ensure_cmd}"),
);
self.client.set_global_hook("session-created", &refresh_cmd);
self.client.set_global_hook("session-closed", &refresh_cmd);
self.client
.set_global_hook("after-select-window", &ensure_cmd);
self.client.set_global_hook("after-new-window", &ensure_cmd);
self.client
.set_global_hook("client-resized", &client_resized_cmd);
self.client.set_global_hook("pane-exited", &pane_exited_cmd);
}
fn cleanup_hooks(&self) {
for hook in [
"client-session-changed",
"session-created",
"session-closed",
"after-select-window",
"after-new-window",
"client-resized",
"pane-exited",
] {
self.client.unset_global_hook(hook);
}
}
fn is_window_capable(&self) -> bool {
true
}
fn is_sidebar_capable(&self) -> bool {
true
}
fn is_batch_capable(&self) -> bool {
true
}
fn list_active_windows(&self) -> Vec<ActiveWindow> {
let mut windows = Vec::<ActiveWindow>::new();
for window in self
.client
.list_windows()
.into_iter()
.filter(|window| window.session_name != STASH_SESSION)
{
let next = ActiveWindow {
id: window.id,
session_name: window.session_name,
active: window.active,
};
if let Some(current) = windows.iter_mut().find(|current| current.id == next.id) {
if !current.active && next.active {
*current = next;
}
} else {
windows.push(next);
}
}
windows
}
fn get_current_window_id(&self) -> Option<String> {
self.client.get_current_window_id()
}
fn list_sidebar_panes(&self, session_name: Option<&str>) -> Vec<SidebarPane> {
let panes = match session_name {
Some(session_name) => self.client.list_panes(PaneScope::Session(session_name)),
None => self.client.list_panes(PaneScope::All),
};
let mut window_widths = HashMap::new();
for pane in &panes {
let width = pane.right.saturating_add(1);
window_widths
.entry(pane.window_id.clone())
.and_modify(|current: &mut u16| *current = (*current).max(width))
.or_insert(width);
}
let mut seen_pane_ids = HashSet::new();
panes
.into_iter()
.filter(|pane| {
pane.title == "opensessions-sidebar" && pane.session_name != STASH_SESSION
})
.filter(|pane| seen_pane_ids.insert(pane.id.clone()))
.map(|pane| SidebarPane {
pane_id: pane.id,
session_name: pane.session_name,
window_id: pane.window_id.clone(),
width: Some(pane.width),
window_width: window_widths.get(&pane.window_id).copied(),
})
.collect()
}
fn list_agent_panes(&self, session_name: &str) -> Vec<AgentPane> {
self.client
.list_panes(PaneScope::Session(session_name))
.into_iter()
.filter(|pane| pane.title != "opensessions-sidebar")
.filter_map(|pane| agent_from_pane(&pane).map(|agent| (pane, agent)))
.map(|(pane, agent)| AgentPane {
thread_name: thread_name_from_pane(&pane, &agent),
agent,
pane_id: pane.id,
thread_id: None,
})
.collect()
}
fn hide_sidebar(&self, pane_id: &str) {
self.client.kill_pane(pane_id);
}
fn kill_sidebar_pane(&self, pane_id: &str) {
self.client.kill_pane(pane_id);
}
fn focus_pane(&self, pane_id: &str) {
let window_id = self.client.display("#{window_id}", Some(pane_id));
if !window_id.is_empty() {
self.client.select_window(&window_id);
}
self.client.select_pane(pane_id);
}
fn kill_pane(&self, pane_id: &str) {
self.client.kill_pane(pane_id);
}
fn resolve_agent_pane_id(
&self,
session: &str,
agent: &str,
_thread_id: Option<&str>,
thread_name: Option<&str>,
) -> Option<String> {
let panes = self
.client
.list_panes(PaneScope::Session(session))
.into_iter()
.filter(|pane| pane.title != "opensessions-sidebar")
.collect::<Vec<_>>();
if agent == "amp" {
if let Some(thread_name) = thread_name {
let matches = panes
.iter()
.filter(|pane| {
pane.title.to_lowercase().starts_with("amp - ")
&& pane.title.contains(thread_name)
})
.collect::<Vec<_>>();
if matches.len() == 1 {
return Some(matches[0].id.clone());
}
}
}
let patterns = match agent {
"amp" => &["amp"][..],
"claude-code" => &["claude"][..],
"codex" => &["codex"][..],
"opencode" => &["opencode"][..],
_ => return None,
};
panes
.into_iter()
.find(|pane| {
let title = pane.title.to_lowercase();
patterns.iter().any(|pattern| title.contains(pattern))
})
.map(|pane| pane.id)
}
fn resize_sidebar_pane(&self, pane_id: &str, width: u16) {
self.client.resize_pane_width(pane_id, width);
}
fn kill_orphaned_sidebar_panes(&self) {
let panes = self.client.list_panes(PaneScope::All);
let mut window_pane_counts: HashMap<String, u32> = HashMap::new();
let mut sidebars_by_window: HashMap<String, Vec<String>> = HashMap::new();
let mut window_session: HashMap<String, String> = HashMap::new();
let mut windows_by_session: HashMap<String, HashSet<String>> = HashMap::new();
let mut seen_pane_ids = HashSet::new();
for pane in panes {
if pane.session_name == STASH_SESSION || !seen_pane_ids.insert(pane.id.clone()) {
continue;
}
*window_pane_counts
.entry(pane.window_id.clone())
.or_insert(0) += 1;
window_session
.entry(pane.window_id.clone())
.or_insert_with(|| pane.session_name.clone());
windows_by_session
.entry(pane.session_name.clone())
.or_default()
.insert(pane.window_id.clone());
if pane.title == "opensessions-sidebar" {
sidebars_by_window
.entry(pane.window_id)
.or_default()
.push(pane.id);
}
}
for (window_id, sidebars) in sidebars_by_window {
if window_pane_counts.get(&window_id) == Some(&1) {
// The window holds only the sidebar. Killing it leaves the
// window with zero panes, destroying the window — and the
// session too if this is its last window. Only reclaim it when
// the session has other windows; otherwise keep the sidebar so
// closing the last work pane doesn't close the whole session.
let session_has_other_windows = window_session
.get(&window_id)
.and_then(|session| windows_by_session.get(session))
.is_some_and(|windows| windows.len() > 1);
if session_has_other_windows {
for pane_id in sidebars {
self.client.kill_pane(&pane_id);
}
}
continue;
}
for pane_id in sidebars.into_iter().skip(1) {
self.client.kill_pane(&pane_id);
}
}
}
fn spawn_sidebar(
&self,
_session_name: &str,
window_id: &str,
width: u16,
position: SidebarPosition,
scripts_dir: &str,
) -> Option<String> {
let panes = self.client.list_panes(PaneScope::Window(window_id));
let target = match position {
SidebarPosition::Left => panes.iter().min_by_key(|pane| pane.left),
SidebarPosition::Right => panes.iter().max_by_key(|pane| pane.right),
}?;
// Resolve the script path against `$OPENSESSIONS_DIR` so the spawned
// pane works even when the parent pane's cwd is unrelated to the
// workspace (e.g. tmux sessions whose default cwd is `$HOME`). Falls
// back to the literal path if the env is unset.
//
// Wrap in `sh -c '...'`: tmux runs pane commands via the user's
// `default-command`/`default-shell`, which may be a non-POSIX shell
// (e.g. fish) that cannot parse `FOO=bar exec` or `${VAR:-default}`.
// Forcing `sh` keeps the launcher portable regardless of the user's
// interactive shell.
//
// Quoting is two-layered: the session name / window id sit inside
// double quotes (so `$`, backtick, `"` and `\` are still live to the
// inner `sh`), so escape those metacharacters first; then the whole
// `inner` is wrapped in single quotes for `sh -c`, with embedded single
// quotes escaped via the standard `'\''` dance. Without the inner
// escape a session name like `x"; rm -rf ~ #` would break out of the
// double quotes and inject commands.
let inner = format!(
"OPENSESSIONS_SESSION_NAME=\"{}\" OPENSESSIONS_WINDOW_ID=\"{}\" REFOCUS_WINDOW=\"{}\" exec \"${{OPENSESSIONS_DIR:-.}}\"/{scripts_dir}/start.sh",
sh_double_quote_escape(&target.session_name),
sh_double_quote_escape(window_id),
sh_double_quote_escape(window_id),
);
let command = format!("sh -c '{}'", inner.replace('\'', r"'\''"));
let new_pane = self.client.split_sidebar_pane(
&target.id,
position == SidebarPosition::Left,
width,
&command,
)?;
self.client
.set_pane_title(&new_pane.id, "opensessions-sidebar");
Some(new_pane.id)
}
fn get_all_pane_counts(&self) -> HashMap<String, u32> {
self.client.get_all_pane_counts()
}
}
/// Escape the characters that stay special inside a POSIX double-quoted
/// string (`\`, `"`, `$`, backtick) so an untrusted value (e.g. a tmux session
/// name) can be interpolated into `FOO="..."` without breaking out of the
/// quotes or triggering command/parameter substitution. Backslash is escaped
/// first so the backslashes added for the others are not doubled.
fn sh_double_quote_escape(s: &str) -> String {
s.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('$', "\\$")
.replace('`', "\\`")
}
fn session_format() -> &'static str {
"#{session_id}\t#{session_name}\t#{session_created}\t#{session_attached}\t#{session_windows}\t#{session_path}"
}
fn window_format() -> &'static str {
"#{window_id}\t#{session_id}\t#{session_name}\t#{window_index}\t#{window_name}\t#{window_active}\t#{window_panes}"
}
fn client_format() -> &'static str {
"#{client_name}\t#{client_tty}\t#{client_pid}\t#{session_name}\t#{client_width}\t#{client_height}"
}
fn pane_format() -> &'static str {
"#{pane_id}\t#{session_name}\t#{window_id}\t#{window_index}\t#{pane_index}\t#{pane_active}\t#{pane_tty}\t#{pane_pid}\t#{pane_current_path}\t#{pane_current_command}\t#{pane_title}\t#{pane_width}\t#{pane_height}\t#{pane_left}\t#{pane_right}"
}
fn agent_from_pane(pane: &PaneInfo) -> Option<String> {
let title = pane.title.to_lowercase();
let command = pane.command.to_lowercase();
if title.starts_with("amp") || command.contains("amp") {
return Some("amp".to_string());
}
if title.contains("claude") || command.contains("claude") {
return Some("claude-code".to_string());
}
if title.contains("codex") || command.contains("codex") {
return Some("codex".to_string());
}
if title.contains("opencode") || command.contains("opencode") {
return Some("opencode".to_string());
}
None
}
fn thread_name_from_pane(pane: &PaneInfo, agent: &str) -> Option<String> {
let title = pane.title.trim();
if agent == "amp"
&& let Some((_, thread_name)) = title.split_once(" - ")
{
let thread_name = thread_name.trim();
if !thread_name.is_empty() {
return Some(thread_name.to_string());
}
}
None
}
fn parse_sessions(raw: &str) -> Vec<SessionInfo> {
raw.lines()
.filter(|line| !line.is_empty())
.map(|line| {
let parts = split(line);
SessionInfo {
id: part(&parts, 0),
name: part(&parts, 1),
created_at: parse_u64(&parts, 2),
attached_clients: parse_u32(&parts, 3),
window_count: parse_u32(&parts, 4),
dir: part(&parts, 5),
}
})
.collect()
}
fn parse_windows(raw: &str) -> Vec<WindowInfo> {
raw.lines()
.filter(|line| !line.is_empty())
.map(|line| {
let parts = split(line);
WindowInfo {
id: part(&parts, 0),
session_id: part(&parts, 1),
session_name: part(&parts, 2),
index: parse_u32(&parts, 3),
name: part(&parts, 4),
active: part(&parts, 5) == "1",
pane_count: parse_u32(&parts, 6),
}
})
.collect()
}
fn parse_clients(raw: &str) -> Vec<ClientInfo> {
raw.lines()
.filter(|line| !line.is_empty())
.map(|line| {
let parts = split(line);
ClientInfo {
name: part(&parts, 0),
tty: part(&parts, 1),
pid: parse_u32(&parts, 2),
session_name: part(&parts, 3),
width: parse_u16(&parts, 4),
height: parse_u16(&parts, 5),
}
})
.collect()
}
fn parse_panes(raw: &str) -> Vec<PaneInfo> {
raw.lines()
.filter(|line| !line.is_empty())
.filter_map(|line| {
let parts = split(line);
if parts.len() < 15 {
return None;
}
Some(PaneInfo {
id: part(&parts, 0),
session_name: part(&parts, 1),
window_id: part(&parts, 2),
window_index: parse_u32(&parts, 3),
index: parse_u32(&parts, 4),
active: part(&parts, 5) == "1",
tty: part(&parts, 6),
pid: parse_u32(&parts, 7),
cwd: part(&parts, 8),
command: part(&parts, 9),
title: part(&parts, 10),
width: parse_u16(&parts, 11),
height: parse_u16(&parts, 12),
left: parse_u16(&parts, 13),
right: parse_u16(&parts, 14),
})
})
.collect()
}
fn split(line: &str) -> Vec<&str> {
line.split(SEP).collect()
}
fn part(parts: &[&str], index: usize) -> String {
parts.get(index).copied().unwrap_or_default().to_string()
}
fn parse_u16(parts: &[&str], index: usize) -> u16 {
parts
.get(index)
.and_then(|value| value.parse::<u16>().ok())
.unwrap_or_default()
}
fn parse_u32(parts: &[&str], index: usize) -> u32 {
parts
.get(index)
.and_then(|value| value.parse::<u32>().ok())
.unwrap_or_default()
}
fn parse_u64(parts: &[&str], index: usize) -> u64 {
parts
.get(index)
.and_then(|value| value.parse::<u64>().ok())
.unwrap_or_default()
}