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
|
//! Runtime command launching and overlay pane lifecycle helpers for [`App`].
use std::process::{Command, Stdio};
use ratatui::layout::Direction;
use super::{App, Mode};
#[derive(Debug, Clone, Copy)]
pub(super) struct OverlayPaneState {
ws_idx: usize,
tab_idx: usize,
previous_focus: crate::layout::PaneId,
previous_zoomed: bool,
}
impl App {
pub(super) fn launch_custom_command(&mut self, binding: crate::config::CustomCommandKeybind) {
let previous_toast = self.state.toast.clone();
let result = match binding.action {
crate::config::CustomCommandAction::Shell => self.spawn_custom_command(&binding),
crate::config::CustomCommandAction::Pane => self.spawn_pane_command(&binding.command),
};
match result {
Ok(()) => {
if self.state.active.is_some() {
self.state.mode = Mode::Terminal;
}
}
Err(err) => {
self.state.toast = Some(crate::app::state::ToastNotification {
kind: crate::app::state::ToastKind::NeedsAttention,
title: "custom command failed".to_string(),
context: err.to_string(),
});
self.sync_toast_deadline(previous_toast);
}
}
}
fn custom_command_env(&self) -> (Vec<(String, String)>, Option<std::path::PathBuf>) {
let mut env = vec![(
crate::api::SOCKET_PATH_ENV_VAR.to_string(),
crate::api::socket_path().display().to_string(),
)];
if let Ok(current_exe) = std::env::current_exe() {
env.push((
"HERDR_BIN_PATH".to_string(),
current_exe.display().to_string(),
));
}
let mut cwd = None;
if let Some(ws_idx) = self.state.active {
env.push((
"HERDR_ACTIVE_WORKSPACE_ID".to_string(),
self.public_workspace_id(ws_idx),
));
if let Some(workspace) = self.state.workspaces.get(ws_idx) {
let tab_idx = workspace.active_tab_index();
if let Some(tab_id) = self.public_tab_id(ws_idx, tab_idx) {
env.push(("HERDR_ACTIVE_TAB_ID".to_string(), tab_id));
}
if let Some(pane_id) = workspace.focused_pane_id() {
if let Some(public_pane_id) = self.public_pane_id(ws_idx, pane_id) {
env.push(("HERDR_ACTIVE_PANE_ID".to_string(), public_pane_id));
}
if let Some(pane_cwd) = workspace
.active_tab()
.and_then(|tab| tab.cwd_for_pane(pane_id))
{
env.push((
"HERDR_ACTIVE_PANE_CWD".to_string(),
pane_cwd.display().to_string(),
));
if pane_cwd.is_dir() {
cwd = Some(pane_cwd);
}
}
}
}
}
(env, cwd)
}
fn spawn_custom_command(
&self,
binding: &crate::config::CustomCommandKeybind,
) -> std::io::Result<()> {
let mut command = Command::new("/bin/sh");
command
.arg("-lc")
.arg(&binding.command)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
let (env, cwd) = self.custom_command_env();
command.envs(env);
if let Some(cwd) = cwd {
command.current_dir(cwd);
}
command.spawn()?;
Ok(())
}
fn spawn_pane_command(&mut self, command: &str) -> std::io::Result<()> {
let Some(ws_idx) = self.state.active else {
return Err(std::io::Error::other("no active workspace"));
};
let (rows, cols) = self.state.estimate_pane_size();
let new_rows = rows.max(4);
let new_cols = cols.max(10);
let (env, _) = self.custom_command_env();
let ws = self
.state
.workspaces
.get_mut(ws_idx)
.ok_or_else(|| std::io::Error::other("active workspace disappeared"))?;
let tab_idx = ws.active_tab_index();
let previous_focus = ws
.focused_pane_id()
.ok_or_else(|| std::io::Error::other("no focused pane"))?;
let previous_zoomed = ws.active_tab().map(|tab| tab.zoomed).unwrap_or(false);
let cwd = ws
.active_tab()
.and_then(|tab| tab.cwd_for_pane(previous_focus));
let new_pane_id = ws.split_focused_command(
Direction::Horizontal,
new_rows,
new_cols,
cwd,
command,
&env,
self.state.pane_scrollback_limit_bytes,
self.state.host_terminal_theme,
)?;
ws.active_tab_mut()
.expect("workspace must have an active tab")
.layout
.focus_pane(new_pane_id);
ws.active_tab_mut()
.expect("workspace must have an active tab")
.zoomed = true;
self.overlay_panes.insert(
new_pane_id,
OverlayPaneState {
ws_idx,
tab_idx,
previous_focus,
previous_zoomed,
},
);
self.state.mode = Mode::Terminal;
Ok(())
}
pub(super) fn restore_overlay_after_exit(&mut self, overlay: OverlayPaneState) {
let Some(ws) = self.state.workspaces.get_mut(overlay.ws_idx) else {
return;
};
if overlay.tab_idx >= ws.tabs.len() {
return;
}
ws.active_tab = overlay.tab_idx;
let tab = &mut ws.tabs[overlay.tab_idx];
if tab.panes.contains_key(&overlay.previous_focus) {
tab.layout.focus_pane(overlay.previous_focus);
}
tab.zoomed = overlay.previous_zoomed;
if self.state.active == Some(overlay.ws_idx) {
self.state.mode = Mode::Terminal;
}
}
}
|