seela 1.5.1

A fast and customizable tmux session manager.
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
use crate::config::{Config, Session, SplitDirection, Window};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::thread;
use std::time::Duration;

const SHELL_POLL_MS: u64 = 50;

pub fn open_session(
    path: &Path,
    config: &Config,
    config_dir: &Path,
    debug: bool,
) -> Result<(), Box<dyn Error>> {
    let session_name = path
        .file_name()
        .ok_or("Could not get directory name")?
        .to_string_lossy()
        .replace('.', "_");

    if debug {
        println!("Opening session: {session_name}");
    }

    let status = Command::new("tmux")
        .arg("has-session")
        .arg("-t")
        .arg(&session_name)
        .stderr(std::process::Stdio::null())
        .status();

    let session_exists = match status {
        Ok(s) => s.success(),
        Err(_) => false,
    };

    if !session_exists {
        if let Some(session_config) = config.get_session_for_path(path) {
            if let Err(e) = create_session_from_config(
                &session_name,
                path,
                config,
                session_config,
                config_dir,
                debug,
            ) {
                let _ = Command::new("tmux")
                    .arg("kill-session")
                    .arg("-t")
                    .arg(&session_name)
                    .status();
                return Err(e);
            }
        } else {
            let mut cmd = Command::new("tmux");
            cmd.arg("new-session")
                .arg("-d")
                .arg("-s")
                .arg(&session_name)
                .arg("-c")
                .arg(path.to_string_lossy().as_ref());
            cmd.status()?;
        }
    }

    if std::env::var("TMUX").is_ok() {
        Command::new("tmux")
            .arg("switch-client")
            .arg("-t")
            .arg(&session_name)
            .status()?;
    } else {
        Command::new("tmux")
            .arg("attach-session")
            .arg("-t")
            .arg(&session_name)
            .status()?;
    }

    Ok(())
}

fn get_command_output(mut cmd: Command, debug: bool) -> Result<String, Box<dyn Error>> {
    if debug {
        println!("Executing for output: {cmd:?}");
    }
    let output = cmd.output()?;
    if !output.status.success() {
        return Err(format!(
            "Tmux command failed: {:?}",
            String::from_utf8_lossy(&output.stderr)
        )
        .into());
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

fn pane_current_command(pane_id: &str) -> Option<String> {
    let out = Command::new("tmux")
        .arg("display-message")
        .arg("-t")
        .arg(pane_id)
        .arg("-p")
        .arg("#{pane_current_command}")
        .output()
        .ok()?;
    let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
    if s.is_empty() { None } else { Some(s) }
}

fn is_shell(cmd: &str) -> bool {
    matches!(
        cmd,
        "bash" | "zsh" | "fish" | "sh" | "dash" | "ksh" | "tcsh" | "csh" | "nu"
    )
}

fn wait_for_shell_idle(pane_id: &str, timeout_ms: u64) -> bool {
    let mut elapsed = 0u64;
    loop {
        if pane_current_command(pane_id).is_some_and(|c| is_shell(&c)) {
            return true;
        }
        if elapsed >= timeout_ms {
            return false;
        }
        thread::sleep(Duration::from_millis(SHELL_POLL_MS));
        elapsed += SHELL_POLL_MS;
    }
}

struct ExecTask {
    pane_id: String,
    commands: Vec<String>,
    path: PathBuf,
    session_name: String,
    window_name: String,
    config_dir: PathBuf,
}

fn split_percentage(ratios: &[f32], i: usize, remaining_ratio: f32) -> u32 {
    let next_sum: f32 = ratios[i + 1..].iter().sum();
    let pct = (next_sum / remaining_ratio) * 100.0;
    (pct.round() as u32).clamp(1, 99)
}

/// `-h` (side-by-side) by default; `-v` (stacked) if first pane sets `split = "horizontal"`.
fn window_split_arg(wc: &Window) -> &'static str {
    match wc.panes.first().and_then(|p| p.split) {
        Some(SplitDirection::Horizontal) => "-v",
        _ => "-h",
    }
}

fn create_session_from_config(
    session_name: &str,
    path: &Path,
    config: &Config,
    session_config: &Session,
    config_dir: &Path,
    debug: bool,
) -> Result<(), Box<dyn Error>> {
    let mut exec_tasks = Vec::new();
    let mut hook_handles: Vec<thread::JoinHandle<()>> = Vec::new();

    for (win_idx, window_name) in session_config.windows.iter().enumerate() {
        let window_config = config.windows.iter().find(|w| &w.name == window_name);

        let root_pane_id = if win_idx == 0 {
            let mut cmd = Command::new("tmux");
            cmd.arg("new-session")
                .arg("-d")
                .arg("-s")
                .arg(session_name)
                .arg("-n")
                .arg(window_name)
                .arg("-c")
                .arg(path.to_string_lossy().as_ref())
                .arg("-P")
                .arg("-F")
                .arg("#{pane_id}");
            get_command_output(cmd, debug)?
        } else {
            let mut cmd = Command::new("tmux");
            cmd.arg("new-window")
                .arg("-a")
                .arg("-t")
                .arg(session_name)
                .arg("-n")
                .arg(window_name)
                .arg("-c")
                .arg(path.to_string_lossy().as_ref())
                .arg("-P")
                .arg("-F")
                .arg("#{pane_id}");
            get_command_output(cmd, debug)?
        };

        if let Some(wc) = window_config {
            if wc.panes.is_empty() {
                continue;
            }

            let split_arg = window_split_arg(wc);
            let mut pane_ids = vec![root_pane_id.clone()];
            let ratios: Vec<f32> = wc.panes.iter().map(|p| p.ratio.unwrap_or(1.0)).collect();
            let mut remaining_ratio: f32 = ratios.iter().sum();

            let mut current_pane_id = root_pane_id.clone();
            for i in 0..wc.panes.len().saturating_sub(1) {
                let percentage = split_percentage(&ratios, i, remaining_ratio);
                let mut cmd = Command::new("tmux");
                cmd.arg("split-window")
                    .arg("-d")
                    .arg(split_arg)
                    .arg("-l")
                    .arg(format!("{}%", percentage))
                    .arg("-t")
                    .arg(&current_pane_id)
                    .arg("-c")
                    .arg(path.to_string_lossy().as_ref())
                    .arg("-P")
                    .arg("-F")
                    .arg("#{pane_id}");
                match get_command_output(cmd, debug) {
                    Ok(new_id) => {
                        pane_ids.push(new_id.clone());
                        current_pane_id = new_id;
                    }
                    Err(e) => {
                        eprintln!("seela: split-window failed: {e}");
                        break;
                    }
                }
                remaining_ratio -= ratios[i];
            }

            for (i, pane_config) in wc.panes.iter().enumerate() {
                if i < pane_ids.len() {
                    setup_pane(
                        &pane_ids[i],
                        pane_config,
                        path,
                        config_dir,
                        debug,
                        &mut exec_tasks,
                        session_name,
                        window_name,
                    )?;
                }
            }

            if !wc.hooks.is_empty() {
                let hooks = wc.hooks.clone();
                let parallel = wc.hooks_parallel;
                let session_name_s = session_name.to_string();
                let window_name_s = window_name.to_string();
                let path_s = path.to_path_buf();
                let config_dir_s = config_dir.to_path_buf();
                hook_handles.push(thread::spawn(move || {
                    run_window_hooks(
                        &hooks,
                        parallel,
                        &session_name_s,
                        &window_name_s,
                        &path_s,
                        &config_dir_s,
                    );
                }));
            }
        }
    }

    for handle in hook_handles {
        let _ = handle.join();
    }

    if !exec_tasks.is_empty() {
        if debug {
            println!(
                "Waiting {}ms for tmux to stabilize...",
                config.tmux.startup_delay_ms
            );
        }
        thread::sleep(Duration::from_millis(config.tmux.startup_delay_ms));

        let tmux_cfg = config.tmux.clone();

        thread::scope(|s| {
            for (task_idx, task) in exec_tasks.into_iter().enumerate() {
                let tmux_cfg = tmux_cfg.clone();
                s.spawn(move || {
                    thread::sleep(Duration::from_millis(task_idx as u64 * 25));

                    let idle = wait_for_shell_idle(&task.pane_id, 10_000);
                    if !idle {
                        let mut cmd = Command::new("tmux");
                        cmd.arg("send-keys").arg("-t").arg(&task.pane_id).arg("C-c");
                        let _ = cmd.status();
                        thread::sleep(Duration::from_millis(tmux_cfg.key_delay_ms));
                    }

                    let mut reset_cmd = Command::new("tmux");
                    reset_cmd.arg("send-keys").arg("-t").arg(&task.pane_id).arg("-R");
                    let _ = reset_cmd.status();

                    let mut clear_cmd = Command::new("tmux");
                    clear_cmd.arg("send-keys").arg("-t").arg(&task.pane_id).arg("C-u");
                    let _ = clear_cmd.status();

                    thread::sleep(Duration::from_millis(tmux_cfg.key_delay_ms));

                    for exec_cmd in task.commands.iter() {
                        let mut final_cmd = exec_cmd.clone();
                        let trimmed = exec_cmd.trim();

                        if trimmed.is_empty() {
                            continue;
                        }

                        if let Some((keyword, val)) = trimmed.split_once(' ') {
                            match keyword {
                                "@confirm" => {
                                    if let Ok(current_exe) = std::env::current_exe() {
                                        final_cmd = format!(
                                            "{} --run-command {}",
                                            current_exe.display(),
                                            shell_escape(val),
                                        );
                                    }
                                }
                                "@run" => {
                                    let expanded_val = {
                                        let mut parts = val.splitn(2, ' ');
                                        let script = expand_exec_path(
                                            parts.next().unwrap_or(""),
                                            &task.config_dir,
                                        );
                                        let rest = parts.next().unwrap_or("");
                                        if rest.is_empty() { script } else { format!("{script} {rest}") }
                                    };
                                    let cmd_str = format!(
                                        "SEELA_SESSION_PATH={} SEELA_SESSION_NAME={} SEELA_WINDOW_NAME={} SEELA_PANE_ID={} {}\n",
                                        shell_escape(&task.path.display().to_string()),
                                        shell_escape(&task.session_name),
                                        shell_escape(&task.window_name),
                                        shell_escape(&task.pane_id),
                                        expanded_val,
                                    );
                                    wait_for_shell_idle(&task.pane_id, 10_000);
                                    let mut load = Command::new("tmux")
                                        .arg("load-buffer").arg("-")
                                        .stdin(std::process::Stdio::piped())
                                        .spawn()
                                        .expect("tmux load-buffer failed");
                                    if let Some(mut stdin) = load.stdin.take() {
                                        use std::io::Write;
                                        let _ = stdin.write_all(cmd_str.as_bytes());
                                    }
                                    let _ = load.wait();
                                    let _ = Command::new("tmux")
                                        .arg("paste-buffer")
                                        .arg("-t").arg(&task.pane_id)
                                        .status();
                                    thread::sleep(Duration::from_millis(tmux_cfg.action_delay_ms));
                                    continue;
                                }
                                "@wait" => {
                                    if let Ok(secs) = val.parse::<u64>() {
                                        thread::sleep(Duration::from_secs(secs));
                                    }
                                    continue;
                                }
                                "@wait-milli" | "@wait-ms" => {
                                    if let Ok(ms) = val.parse::<u64>() {
                                        thread::sleep(Duration::from_millis(ms));
                                    }
                                    continue;
                                }
                                "@send-key" | "@sk" => {
                                    thread::sleep(Duration::from_millis(tmux_cfg.key_delay_ms));
                                    let mut key_cmd = Command::new("tmux");
                                    key_cmd
                                        .arg("send-keys")
                                        .arg("-t").arg(&task.pane_id)
                                        .arg(val);
                                    let _ = key_cmd.status();
                                    continue;
                                }
                                _ => {}
                            }
                        }

                        wait_for_shell_idle(&task.pane_id, 10_000);

                        let mut run_cmd = Command::new("tmux");
                        run_cmd
                            .arg("send-keys")
                            .arg("-t").arg(&task.pane_id)
                            .arg("-l").arg(&final_cmd);
                        let _ = run_cmd.status();

                        thread::sleep(Duration::from_millis(tmux_cfg.key_delay_ms));

                        let mut enter_cmd = Command::new("tmux");
                        enter_cmd
                            .arg("send-keys")
                            .arg("-t").arg(&task.pane_id)
                            .arg("C-m");
                        let _ = enter_cmd.status();

                        thread::sleep(Duration::from_millis(tmux_cfg.action_delay_ms));
                    }
                });
            }
        });
    }

    if let Some(focus_name) = &session_config.window_focus {
        Command::new("tmux")
            .arg("select-window")
            .arg("-t")
            .arg(format!("{session_name}:{focus_name}"))
            .status()?;
    }

    Ok(())
}

fn shell_escape(s: &str) -> String {
    format!("'{}'", s.replace('\'', "'\\''"))
}

/// Expands `~` and resolves relative paths against the config file directory.
/// Plain command names (no `/`) are left untouched so PATH lookup still works.
fn expand_exec_path(s: &str, config_dir: &Path) -> String {
    let expanded = shellexpand::tilde(s);
    let p = Path::new(expanded.as_ref());
    if p.is_absolute() {
        p.to_string_lossy().into_owned()
    } else if s.contains('/') {
        config_dir.join(p).to_string_lossy().into_owned()
    } else {
        s.to_string()
    }
}

fn run_hook(script: &str, session_name: &str, window_name: &str, path: &Path, config_dir: &Path) {
    // Expand only the first token so arguments are preserved.
    let expanded = {
        let mut parts = script.splitn(2, ' ');
        let cmd = expand_exec_path(parts.next().unwrap_or(""), config_dir);
        let rest = parts.next().unwrap_or("");
        if rest.is_empty() {
            cmd
        } else {
            format!("{cmd} {rest}")
        }
    };
    let cmd_str = format!(
        "SEELA_SESSION_PATH={} SEELA_SESSION_NAME={} SEELA_WINDOW_NAME={} {}",
        shell_escape(&path.display().to_string()),
        shell_escape(session_name),
        shell_escape(window_name),
        expanded,
    );
    match Command::new("sh").arg("-c").arg(&cmd_str).output() {
        Ok(out) if !out.status.success() => {
            let stderr = String::from_utf8_lossy(&out.stderr);
            let stderr = stderr.trim();
            if !stderr.is_empty() {
                eprintln!(
                    "seela: hook '{}' failed ({}): {}",
                    script, out.status, stderr
                );
            } else {
                eprintln!("seela: hook '{}' failed ({})", script, out.status);
            }
        }
        Err(e) => eprintln!("seela: hook '{}' could not run: {}", script, e),
        _ => {}
    }
}

fn run_window_hooks(
    hooks: &[String],
    parallel: bool,
    session_name: &str,
    window_name: &str,
    path: &Path,
    config_dir: &Path,
) {
    if parallel {
        thread::scope(|s| {
            for hook in hooks {
                let hook = hook.clone();
                let session_name = session_name.to_string();
                let window_name = window_name.to_string();
                let path = path.to_path_buf();
                let config_dir = config_dir.to_path_buf();
                s.spawn(move || run_hook(&hook, &session_name, &window_name, &path, &config_dir));
            }
        });
    } else {
        for hook in hooks {
            run_hook(hook, session_name, window_name, path, config_dir);
        }
    }
}

fn setup_pane(
    pane_id: &str,
    config: &crate::config::Pane,
    path: &Path,
    config_dir: &Path,
    debug: bool,
    exec_tasks: &mut Vec<ExecTask>,
    session_name: &str,
    window_name: &str,
) -> Result<(), Box<dyn Error>> {
    if !config.panes.is_empty() {
        let mut sub_pane_ids = vec![pane_id.to_string()];

        let split_arg = match config.split {
            Some(SplitDirection::Vertical) => "-h",
            _ => "-v",
        };

        let ratios: Vec<f32> = config
            .panes
            .iter()
            .map(|p| p.ratio.unwrap_or(1.0))
            .collect();
        let mut remaining_ratio: f32 = ratios.iter().sum();

        let mut current_pane_id = pane_id.to_string();
        for i in 0..config.panes.len().saturating_sub(1) {
            let percentage = split_percentage(&ratios, i, remaining_ratio);
            let mut cmd = Command::new("tmux");
            cmd.arg("split-window")
                .arg("-d")
                .arg(split_arg)
                .arg("-l")
                .arg(format!("{}%", percentage))
                .arg("-t")
                .arg(&current_pane_id)
                .arg("-c")
                .arg(path.to_string_lossy().as_ref())
                .arg("-P")
                .arg("-F")
                .arg("#{pane_id}");
            match get_command_output(cmd, debug) {
                Ok(new_id) => {
                    sub_pane_ids.push(new_id.clone());
                    current_pane_id = new_id;
                }
                Err(e) => {
                    eprintln!("seela: split-window failed: {e}");
                    break;
                }
            }
            remaining_ratio -= ratios[i];
        }

        for (i, sub_pane_config) in config.panes.iter().enumerate() {
            if i < sub_pane_ids.len() {
                setup_pane(
                    &sub_pane_ids[i],
                    sub_pane_config,
                    path,
                    config_dir,
                    debug,
                    exec_tasks,
                    session_name,
                    window_name,
                )?;
            }
        }
    } else if let Some(execs) = &config.exec
        && !execs.is_empty()
    {
        exec_tasks.push(ExecTask {
            pane_id: pane_id.to_string(),
            commands: execs.clone(),
            path: path.to_path_buf(),
            session_name: session_name.to_string(),
            window_name: window_name.to_string(),
            config_dir: config_dir.to_path_buf(),
        });
    }

    Ok(())
}