ezpn 0.10.0

Dead simple terminal pane splitting — ezpn 2 3 gives you a 2x3 grid of shells
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
//! Pane lifecycle helpers shared by the foreground loop and the daemon.
//!
//! These functions all hover around the `(Layout, HashMap<usize, Pane>)`
//! pair plus a `Settings` reference. Splitting them into smaller modules
//! would force callers to import three files for any non-trivial mutation,
//! so they live together.
//!
//! Bucket overview:
//! - **Per-pane spawn**: [`spawn_pane`], [`spawn_layout_panes`],
//!   [`spawn_snapshot_panes`], [`spawn_project_panes`], [`replace_pane`].
//! - **Mutations**: [`do_split`], [`close_pane`], [`resize_all`],
//!   [`apply_snapshot`], [`kill_all_panes`].
//! - **Selection / clipboard**: [`extract_selected_text`], [`base64_encode`].
//!
//! Initial bring-up (snapshot/project/Procfile selection) lives next door
//! in [`super::bootstrap`].

use std::collections::HashMap;

use crate::app::render_ctl::make_inner;
use crate::layout::{Direction, Layout};
use crate::pane::{Pane, PaneLaunch};
use crate::project;
use crate::settings::Settings;
use crate::workspace::{self, WorkspaceSnapshot};

/// Extract selected text — server-friendly version that takes individual coords.
pub(crate) fn extract_selected_text(
    screen: &vt100::Screen,
    _pane_id: usize,
    start_row: u16,
    start_col: u16,
    end_row: u16,
    end_col: u16,
) -> String {
    // Normalize
    let (sr, sc, er, ec) = if start_row < end_row || (start_row == end_row && start_col <= end_col)
    {
        (start_row, start_col, end_row, end_col)
    } else {
        (end_row, end_col, start_row, start_col)
    };

    let mut text = String::new();
    for r in sr..=er {
        let col_start = if r == sr { sc } else { 0 };
        let col_end = if r == er { ec } else { u16::MAX };
        let mut row_text = String::new();
        let mut c = col_start;
        loop {
            if c > col_end {
                break;
            }
            if let Some(cell) = screen.cell(r, c) {
                let contents = cell.contents();
                if contents.is_empty() {
                    row_text.push(' ');
                } else {
                    row_text.push_str(&contents);
                }
            } else {
                break;
            }
            c += 1;
        }
        let trimmed = row_text.trim_end();
        text.push_str(trimmed);
        if r < er {
            text.push('\n');
        }
    }
    text
}

pub(crate) fn spawn_layout_panes(
    layout: &Layout,
    launches: HashMap<usize, PaneLaunch>,
    shell: &str,
    tw: u16,
    th: u16,
    settings: &Settings,
    scrollback: usize,
) -> anyhow::Result<HashMap<usize, Pane>> {
    let inner = make_inner(tw, th, settings.show_status_bar);
    let rects = layout.pane_rects(&inner);

    // Collect spawn tasks
    let tasks: Vec<(usize, PaneLaunch, u16, u16)> = rects
        .iter()
        .map(|(&pid, rect)| {
            let launch = launches.get(&pid).cloned().unwrap_or(PaneLaunch::Shell);
            (pid, launch, rect.w.max(1), rect.h.max(1))
        })
        .collect();

    // Spawn panes in parallel using scoped threads
    let mut results: Vec<(usize, anyhow::Result<Pane>)> = Vec::new();
    std::thread::scope(|s| {
        let handles: Vec<_> = tasks
            .iter()
            .map(|(pid, launch, cols, rows)| {
                let pid = *pid;
                let cols = *cols;
                let rows = *rows;
                s.spawn(move || (pid, spawn_pane(shell, launch, cols, rows, scrollback)))
            })
            .collect();
        for handle in handles {
            match handle.join() {
                Ok(result) => results.push(result),
                Err(payload) => {
                    let reason = match payload.downcast_ref::<&'static str>() {
                        Some(s) => (*s).to_string(),
                        None => match payload.downcast_ref::<String>() {
                            Some(s) => s.clone(),
                            None => "unknown panic payload".to_string(),
                        },
                    };
                    eprintln!("ezpn: pane spawn thread panicked: {}", reason);
                    // Continue with the panes that did spawn — partial workspace
                    // is preferable to aborting the entire session.
                }
            }
        }
    });

    let mut panes = HashMap::new();
    for (pid, result) in results {
        panes.insert(pid, result?);
    }
    Ok(panes)
}

pub(crate) fn spawn_snapshot_panes(
    layout: &Layout,
    tab: &workspace::TabSnapshot,
    shell: &str,
    tw: u16,
    th: u16,
    settings: &Settings,
    scrollback: usize,
) -> anyhow::Result<HashMap<usize, Pane>> {
    let inner = make_inner(tw, th, settings.show_status_bar);
    let rects = layout.pane_rects(&inner);
    let mut panes = HashMap::new();

    for ps in &tab.panes {
        let rect = rects.get(&ps.id).cloned().unwrap_or(crate::layout::Rect {
            x: 0,
            y: 0,
            w: 80,
            h: 24,
        });
        let cols = rect.w.max(1);
        let rows = rect.h.max(1);
        let pane_shell = ps.shell.as_deref().unwrap_or(shell);
        let cwd = ps.cwd.as_ref().map(std::path::PathBuf::from);
        let cwd_ref = cwd.as_deref();
        let mut pane = Pane::with_full_config(
            pane_shell,
            ps.launch.clone(),
            cols,
            rows,
            scrollback,
            cwd_ref,
            &ps.env,
        )?;
        if let Some(name) = &ps.name {
            pane.set_name(Some(name.clone()));
        }
        if ps.shell.is_some() {
            pane.set_initial_shell(ps.shell.clone());
        }
        // Replay v3 scrollback blob if present. On error, warn and continue
        // with an empty scrollback — never fail the whole restore.
        if let Some(blob) = &ps.scrollback_blob {
            if let Err(e) = crate::snapshot_blob::decode_scrollback(blob, pane.parser_mut()) {
                eprintln!("ezpn: scrollback restore failed for pane {}: {}", ps.id, e);
            }
        }
        panes.insert(ps.id, pane);
    }
    Ok(panes)
}

pub(crate) fn spawn_pane(
    shell: &str,
    launch: &PaneLaunch,
    cols: u16,
    rows: u16,
    scrollback: usize,
) -> anyhow::Result<Pane> {
    Pane::with_scrollback(shell, launch.clone(), cols, rows, scrollback)
}

pub(crate) fn spawn_project_panes(
    proj: &project::ResolvedProject,
    shell: &str,
    tw: u16,
    th: u16,
    settings: &Settings,
    scrollback: usize,
    max_scrollback: usize,
) -> anyhow::Result<HashMap<usize, Pane>> {
    let inner = make_inner(tw, th, settings.show_status_bar);
    let rects = proj.layout.pane_rects(&inner);
    let mut panes = HashMap::new();

    for (&pid, rect) in &rects {
        let launch = proj
            .launches
            .get(&pid)
            .cloned()
            .unwrap_or(PaneLaunch::Shell);
        let cols = rect.w.max(1);
        let rows = rect.h.max(1);
        let pane_shell = proj.shells.get(&pid).map(|s| s.as_str()).unwrap_or(shell);
        let cwd = proj.cwds.get(&pid).map(|p| p.as_path());
        let env = proj.envs.get(&pid).cloned().unwrap_or_default();
        // SPEC 02: per-pane override takes precedence; capped against the
        // workspace-wide max so a stray `scrollback_lines = 1_000_000` in
        // `.ezpn.toml` cannot bypass the global ceiling.
        let pane_scrollback = proj
            .scrollback_overrides
            .get(&pid)
            .copied()
            .unwrap_or(scrollback)
            .min(max_scrollback);
        let mut pane =
            Pane::with_full_config(pane_shell, launch, cols, rows, pane_scrollback, cwd, &env)?;
        if let Some(name) = proj.names.get(&pid) {
            pane.set_name(Some(name.clone()));
        }
        // Track per-pane shell override for snapshot/restart
        if proj.shells.contains_key(&pid) {
            pane.set_initial_shell(Some(pane_shell.to_string()));
        }
        panes.insert(pid, pane);
    }
    Ok(panes)
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn replace_pane(
    panes: &mut HashMap<usize, Pane>,
    layout: &Layout,
    pane_id: usize,
    launch: PaneLaunch,
    shell: &str,
    tw: u16,
    th: u16,
    settings: &Settings,
    scrollback: usize,
) -> anyhow::Result<()> {
    // Extract cwd/env from the old pane before replacing
    let (cwd, env) = panes
        .get(&pane_id)
        .map(|p| {
            (
                p.live_cwd()
                    .or_else(|| p.initial_cwd().map(|c| c.to_path_buf())),
                p.initial_env().clone(),
            )
        })
        .unwrap_or((None, std::collections::HashMap::new()));
    let inner = make_inner(tw, th, settings.show_status_bar);
    let rect = layout
        .pane_rects(&inner)
        .remove(&pane_id)
        .ok_or_else(|| anyhow::anyhow!("pane rect not found"))?;
    let new_pane = Pane::with_full_config(
        shell,
        launch,
        rect.w.max(1),
        rect.h.max(1),
        scrollback,
        cwd.as_deref(),
        &env,
    )?;
    if let Some(mut old_pane) = panes.insert(pane_id, new_pane) {
        old_pane.kill();
    }
    Ok(())
}

pub(crate) fn kill_all_panes(panes: &mut HashMap<usize, Pane>) {
    for (_, mut pane) in panes.drain() {
        pane.kill();
    }
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn apply_snapshot(
    snapshot: WorkspaceSnapshot,
    layout: &mut Layout,
    panes: &mut HashMap<usize, Pane>,
    active: &mut usize,
    shell: &mut String,
    settings: &mut Settings,
    tw: u16,
    th: u16,
    _scrollback: usize,
) -> anyhow::Result<()> {
    let tab = &snapshot.tabs[snapshot.active_tab];
    let mut next_settings = Settings::with_theme(snapshot.border_style, settings.theme.clone());
    next_settings.show_status_bar = snapshot.show_status_bar;
    next_settings.show_tab_bar = snapshot.show_tab_bar;
    let next_layout = tab.layout.clone();
    let next_panes = spawn_snapshot_panes(
        &next_layout,
        tab,
        &snapshot.shell,
        tw,
        th,
        &next_settings,
        snapshot.scrollback,
    )?;

    kill_all_panes(panes);
    *shell = snapshot.shell.clone();
    *layout = next_layout;
    *panes = next_panes;
    *settings = next_settings;
    settings.visible = false;
    *active = tab.active_pane;
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn do_split(
    layout: &mut Layout,
    panes: &mut HashMap<usize, Pane>,
    active: usize,
    dir: Direction,
    shell: &str,
    tw: u16,
    th: u16,
    settings: &Settings,
    scrollback: usize,
) -> anyhow::Result<()> {
    let inner = make_inner(tw, th, settings.show_status_bar);
    if let Some(rect) = layout.pane_rects(&inner).get(&active) {
        let min_w = 6u16;
        let min_h = 3u16;
        let too_small = match dir {
            Direction::Horizontal => rect.w < min_w * 2 + 1,
            Direction::Vertical => rect.h < min_h * 2 + 1,
        };
        if too_small {
            return Ok(());
        }
    }

    let new_id = layout.split(active, dir);
    let rects = layout.pane_rects(&inner);
    if let Some(rect) = rects.get(&new_id) {
        panes.insert(
            new_id,
            spawn_pane(
                shell,
                &PaneLaunch::Shell,
                rect.w.max(1),
                rect.h.max(1),
                scrollback,
            )?,
        );
    }
    resize_all(panes, layout, tw, th, settings);
    Ok(())
}

/// Remove a pane from the layout and free every map keyed off its id.
///
/// Per SPEC 03 §4.2: dropping the pane out of `panes` triggers
/// `Pane::Drop` which kills the child + joins the reader thread, so we no
/// longer need an explicit `pane.kill()` here. The added `restart_*` and
/// `zoomed_pane` arguments close the leak surface where these maps grew
/// unboundedly with each split/close cycle.
#[allow(clippy::too_many_arguments)]
pub(crate) fn close_pane(
    layout: &mut Layout,
    panes: &mut HashMap<usize, Pane>,
    active: &mut usize,
    pane_id: usize,
    restart_policies: &mut HashMap<usize, project::RestartPolicy>,
    restart_state: &mut HashMap<usize, (std::time::Instant, u32)>,
    zoomed_pane: &mut Option<usize>,
) {
    panes.remove(&pane_id); // Drop fires here — see SPEC 03 §4.1.
    layout.remove(pane_id);
    restart_policies.remove(&pane_id);
    restart_state.remove(&pane_id);
    if *zoomed_pane == Some(pane_id) {
        *zoomed_pane = None;
    }
    if *active == pane_id {
        *active = *layout.pane_ids().first().unwrap_or(&0);
    }
}

pub(crate) fn resize_all(
    panes: &mut HashMap<usize, Pane>,
    layout: &Layout,
    tw: u16,
    th: u16,
    settings: &Settings,
) {
    let inner = make_inner(tw, th, settings.show_status_bar);
    let rects = layout.pane_rects(&inner);
    for (&pid, rect) in &rects {
        if let Some(pane) = panes.get_mut(&pid) {
            pane.resize(rect.w.max(1), rect.h.max(1));
        }
    }
}

/// Minimal base64 encoder for OSC 52 clipboard.
pub(crate) fn base64_encode(data: &[u8]) -> String {
    const ALPHA: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
    for chunk in data.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
        let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
        let triple = (b0 << 16) | (b1 << 8) | b2;
        out.push(ALPHA[((triple >> 18) & 0x3F) as usize] as char);
        out.push(ALPHA[((triple >> 12) & 0x3F) as usize] as char);
        out.push(if chunk.len() > 1 {
            ALPHA[((triple >> 6) & 0x3F) as usize] as char
        } else {
            '='
        });
        out.push(if chunk.len() > 2 {
            ALPHA[(triple & 0x3F) as usize] as char
        } else {
            '='
        });
    }
    out
}