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
|
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use ratatui::layout::Direction;
use tokio::sync::{mpsc, Notify};
use crate::events::AppEvent;
use crate::layout::{Node, PaneId, TileLayout};
use crate::pane::{PaneLaunchEnv, PaneState};
use crate::render_signal::RenderSignal;
use crate::terminal::{TerminalId, TerminalRuntime, TerminalRuntimeRegistry, TerminalState};
pub(crate) type DetachedPane = (PaneId, TerminalId);
pub(crate) struct MovedPane {
pub pane_id: PaneId,
pub pane_state: PaneState,
}
pub struct NewPane {
pub pane_id: PaneId,
pub terminal: TerminalState,
pub runtime: TerminalRuntime,
}
enum SplitCommand<'a> {
Shell {
command: &'a str,
launch_env: &'a PaneLaunchEnv,
},
Argv {
argv: &'a [String],
launch_env: &'a PaneLaunchEnv,
},
}
pub struct Tab {
pub custom_name: Option<String>,
pub number: usize,
/// Identity source for this tab's pane tree.
pub root_pane: PaneId,
pub layout: TileLayout,
/// Pane viewport state — always present, testable without PTYs.
pub panes: HashMap<PaneId, PaneState>,
#[cfg(test)]
pub runtimes: HashMap<PaneId, TerminalRuntime>,
pub zoomed: bool,
pub events: mpsc::Sender<AppEvent>,
pub(crate) render_notify: Arc<Notify>,
pub(crate) render_dirty: Arc<RenderSignal>,
}
impl Tab {
// Tab construction threads pane runtime geometry, host context, and render hooks.
#[allow(clippy::too_many_arguments)]
pub fn new(
number: usize,
initial_cwd: PathBuf,
rows: u16,
cols: u16,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
host_terminal_appearance: Option<crate::terminal_theme::HostAppearance>,
shell_config: crate::pane::PaneShellConfig<'_>,
launch_env: &PaneLaunchEnv,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<RenderSignal>,
) -> std::io::Result<(Self, TerminalState, TerminalRuntime)> {
Self::new_with_runtime(
number,
initial_cwd,
rows,
cols,
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
shell_config,
launch_env,
events,
render_notify,
render_dirty,
None,
)
}
// Command tab construction mirrors the shell tab runtime arguments.
#[allow(clippy::too_many_arguments)]
pub fn new_argv_command(
number: usize,
initial_cwd: PathBuf,
rows: u16,
cols: u16,
argv: &[String],
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
host_terminal_appearance: Option<crate::terminal_theme::HostAppearance>,
launch_env: &PaneLaunchEnv,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<RenderSignal>,
) -> std::io::Result<(Self, TerminalState, TerminalRuntime)> {
Self::new_with_runtime(
number,
initial_cwd,
rows,
cols,
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
crate::pane::PaneShellConfig::new("", crate::config::ShellModeConfig::NonLogin),
launch_env,
events,
render_notify,
render_dirty,
Some(argv),
)
}
#[allow(clippy::too_many_arguments)]
fn new_with_runtime(
number: usize,
initial_cwd: PathBuf,
rows: u16,
cols: u16,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
host_terminal_appearance: Option<crate::terminal_theme::HostAppearance>,
shell_config: crate::pane::PaneShellConfig<'_>,
launch_env: &PaneLaunchEnv,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<RenderSignal>,
argv: Option<&[String]>,
) -> std::io::Result<(Self, TerminalState, TerminalRuntime)> {
let (layout, root_id) = TileLayout::new();
let runtime = if let Some(argv) = argv {
TerminalRuntime::spawn_argv_command(
root_id,
rows,
cols,
initial_cwd.clone(),
argv,
launch_env,
crate::pane::AgentDetection::Enabled,
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
events.clone(),
render_notify.clone(),
render_dirty.clone(),
)?
} else {
TerminalRuntime::spawn(
root_id,
rows,
cols,
initial_cwd.clone(),
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
shell_config,
launch_env,
events.clone(),
render_notify.clone(),
render_dirty.clone(),
)?
};
let terminal_id = TerminalId::alloc();
let terminal = match argv {
Some(argv) => {
TerminalState::new(terminal_id.clone(), initial_cwd).with_launch_argv(argv.to_vec())
}
None => TerminalState::new(terminal_id.clone(), initial_cwd),
};
let mut panes = HashMap::new();
panes.insert(root_id, PaneState::new(terminal_id));
Ok((
Self {
custom_name: None,
number,
root_pane: root_id,
layout,
panes,
#[cfg(test)]
runtimes: HashMap::new(),
zoomed: false,
events,
render_notify,
render_dirty,
},
terminal,
runtime,
))
}
pub fn is_auto_named(&self) -> bool {
self.custom_name.is_none()
}
pub fn set_custom_name(&mut self, name: String) {
self.custom_name = Some(name);
}
pub fn split_focused_command(
&mut self,
direction: Direction,
rows: u16,
cols: u16,
cwd: Option<PathBuf>,
command: &str,
launch_env: &PaneLaunchEnv,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
host_terminal_appearance: Option<crate::terminal_theme::HostAppearance>,
) -> std::io::Result<NewPane> {
self.split_pane_with_runtime(
self.layout.focused(),
true,
direction,
None,
rows,
cols,
cwd,
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
crate::pane::PaneShellConfig::new("", crate::config::ShellModeConfig::NonLogin),
launch_env,
Some(SplitCommand::Shell {
command,
launch_env,
}),
)
}
/// Split `target` with a shell pane. Focus moves to the new pane only when
/// `focus_new_pane` is set; a spawn failure rolls the layout back without
/// touching focus or its history.
#[allow(clippy::too_many_arguments)]
pub(crate) fn split_pane_shell(
&mut self,
target: PaneId,
focus_new_pane: bool,
direction: Direction,
ratio: Option<f32>,
rows: u16,
cols: u16,
cwd: Option<PathBuf>,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
host_terminal_appearance: Option<crate::terminal_theme::HostAppearance>,
shell_config: crate::pane::PaneShellConfig<'_>,
launch_env: &PaneLaunchEnv,
) -> std::io::Result<NewPane> {
self.split_pane_with_runtime(
target,
focus_new_pane,
direction,
ratio,
rows,
cols,
cwd,
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
shell_config,
launch_env,
None,
)
}
/// Split `target` with an argv-command pane. Same focus contract as
/// `split_pane_shell`.
#[allow(clippy::too_many_arguments)]
pub(crate) fn split_pane_argv(
&mut self,
target: PaneId,
focus_new_pane: bool,
direction: Direction,
ratio: Option<f32>,
rows: u16,
cols: u16,
cwd: Option<PathBuf>,
argv: &[String],
launch_env: &PaneLaunchEnv,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
host_terminal_appearance: Option<crate::terminal_theme::HostAppearance>,
) -> std::io::Result<NewPane> {
self.split_pane_with_runtime(
target,
focus_new_pane,
direction,
ratio,
rows,
cols,
cwd,
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
crate::pane::PaneShellConfig::new("", crate::config::ShellModeConfig::NonLogin),
launch_env,
Some(SplitCommand::Argv { argv, launch_env }),
)
}
// Split construction threads geometry, host context, launch policy, and command state.
#[allow(clippy::too_many_arguments)]
fn split_pane_with_runtime(
&mut self,
target: PaneId,
focus_new_pane: bool,
direction: Direction,
ratio: Option<f32>,
rows: u16,
cols: u16,
cwd: Option<PathBuf>,
scrollback_limit_bytes: usize,
host_terminal_theme: crate::terminal_theme::TerminalTheme,
host_terminal_appearance: Option<crate::terminal_theme::HostAppearance>,
shell_config: crate::pane::PaneShellConfig<'_>,
launch_env: &PaneLaunchEnv,
command: Option<SplitCommand<'_>>,
) -> std::io::Result<NewPane> {
let Some(new_id) = self
.layout
.split_pane(target, direction, ratio.unwrap_or(0.5))
else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"split target pane is not in the layout",
));
};
let actual_cwd =
cwd.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| "/".into()));
let launch_argv = if let Some(SplitCommand::Argv { argv, .. }) = &command {
Some((*argv).to_vec())
} else {
None
};
let runtime = match command {
Some(SplitCommand::Shell {
command,
launch_env,
}) => TerminalRuntime::spawn_shell_command(
new_id,
rows,
cols,
actual_cwd.clone(),
command,
launch_env,
crate::pane::AgentDetection::Enabled,
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
self.events.clone(),
self.render_notify.clone(),
self.render_dirty.clone(),
),
Some(SplitCommand::Argv { argv, launch_env }) => TerminalRuntime::spawn_argv_command(
new_id,
rows,
cols,
actual_cwd.clone(),
argv,
launch_env,
crate::pane::AgentDetection::Enabled,
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
self.events.clone(),
self.render_notify.clone(),
self.render_dirty.clone(),
),
None => TerminalRuntime::spawn(
new_id,
rows,
cols,
actual_cwd.clone(),
scrollback_limit_bytes,
host_terminal_theme,
host_terminal_appearance,
shell_config,
launch_env,
self.events.clone(),
self.render_notify.clone(),
self.render_dirty.clone(),
),
};
let runtime = match runtime {
Ok(runtime) => runtime,
Err(err) => {
self.layout.close_pane(new_id);
return Err(err);
}
};
let terminal_id = TerminalId::alloc();
let terminal = match launch_argv {
Some(argv) => {
TerminalState::new(terminal_id.clone(), actual_cwd).with_launch_argv(argv)
}
None => TerminalState::new(terminal_id.clone(), actual_cwd),
};
if focus_new_pane {
self.layout.focus_pane(new_id);
}
self.panes.insert(new_id, PaneState::new(terminal_id));
self.zoomed = false;
Ok(NewPane {
pane_id: new_id,
terminal,
runtime,
})
}
#[cfg(test)]
pub fn close_focused(&mut self) -> Option<DetachedPane> {
let pane_id = self.layout.focused();
self.detach_pane(pane_id)
}
pub fn close_pane(&mut self, pane_id: PaneId) -> Option<DetachedPane> {
self.detach_pane(pane_id)
}
pub fn remove_pane(&mut self, pane_id: PaneId) -> Option<DetachedPane> {
self.detach_pane(pane_id)
}
pub(crate) fn from_existing_pane(
number: usize,
custom_name: Option<String>,
moved: MovedPane,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<RenderSignal>,
) -> Self {
let mut panes = HashMap::new();
let pane_id = moved.pane_id;
panes.insert(pane_id, moved.pane_state);
Self {
custom_name,
number,
root_pane: pane_id,
layout: TileLayout::from_saved(Node::Pane(pane_id), pane_id),
panes,
#[cfg(test)]
runtimes: HashMap::new(),
zoomed: false,
events,
render_notify,
render_dirty,
}
}
pub(crate) fn take_pane_for_move(&mut self, pane_id: PaneId) -> Option<MovedPane> {
if !self.panes.contains_key(&pane_id) {
return None;
}
if self.layout.pane_count() > 1 {
let next_root = self.promoted_root_if_needed(pane_id);
self.layout.close_pane(pane_id);
if let Some(next_root) = next_root {
self.root_pane = next_root;
}
}
let pane_state = self.panes.remove(&pane_id)?;
self.zoomed = false;
Some(MovedPane {
pane_id,
pane_state,
})
}
pub(crate) fn insert_existing_pane(
&mut self,
target_pane_id: PaneId,
moved: MovedPane,
direction: Direction,
ratio: f32,
focus: bool,
) -> Result<PaneId, MovedPane> {
if !self
.layout
.insert_pane_near(target_pane_id, moved.pane_id, direction, ratio, focus)
{
return Err(moved);
}
let pane_id = moved.pane_id;
self.panes.insert(pane_id, moved.pane_state);
self.zoomed = false;
Ok(pane_id)
}
fn detach_pane(&mut self, pane_id: PaneId) -> Option<DetachedPane> {
if self.layout.pane_count() <= 1 {
return None;
}
let next_root = self.promoted_root_if_needed(pane_id);
self.layout.close_pane(pane_id);
let pane = self.panes.remove(&pane_id)?;
let terminal_id = pane.attached_terminal_id;
self.zoomed = false;
if let Some(next_root) = next_root {
self.root_pane = next_root;
}
Some((pane_id, terminal_id))
}
fn promoted_root_if_needed(&self, closing: PaneId) -> Option<PaneId> {
if self.root_pane != closing {
return None;
}
self.layout.pane_ids().into_iter().find(|id| *id != closing)
}
pub fn terminal_id(&self, pane_id: PaneId) -> Option<&TerminalId> {
self.panes
.get(&pane_id)
.map(|pane| &pane.attached_terminal_id)
}
pub fn cwd_for_pane(
&self,
pane_id: PaneId,
terminals: &HashMap<TerminalId, TerminalState>,
terminal_runtimes: &TerminalRuntimeRegistry,
) -> Option<PathBuf> {
let terminal_id = self.terminal_id(pane_id)?;
terminal_runtimes
.get(terminal_id)
.and_then(|rt| rt.cwd())
.or_else(|| {
terminals
.get(terminal_id)
.map(|terminal| terminal.cwd.clone())
})
}
pub fn foreground_cwd_for_pane(
&self,
pane_id: PaneId,
terminal_runtimes: &TerminalRuntimeRegistry,
) -> Option<PathBuf> {
let terminal_id = self.terminal_id(pane_id)?;
terminal_runtimes
.get(terminal_id)
.and_then(|rt| rt.foreground_cwd())
}
}
|