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
|
//! The `[cli_client]` is used to attach to a running server session
//! and dispatch actions, that are specified through the command line.
use std::collections::{BTreeMap, HashSet};
use std::io::{self, BufRead, Write};
use std::process;
use std::str::FromStr;
use std::{fs, path::PathBuf};
use crate::os_input_output::ClientOsApi;
use uuid::Uuid;
use zellij_utils::{
cli::{SubscribeCli, SubscribeFormat},
data::PaneId,
errors::prelude::*,
input::actions::Action,
ipc::{ClientToServerMsg, ExitReason, ServerToClientMsg},
};
pub fn start_cli_client(
mut os_input: Box<dyn ClientOsApi>,
session_name: &str,
actions: Vec<Action>,
) {
let zellij_ipc_pipe: PathBuf = {
let mut sock_dir = zellij_utils::consts::ZELLIJ_SOCK_DIR.clone();
fs::create_dir_all(&sock_dir).unwrap();
zellij_utils::shared::set_permissions(&sock_dir, 0o700).unwrap();
sock_dir.push(session_name);
sock_dir
};
os_input.connect_to_server(&*zellij_ipc_pipe);
let pane_id = os_input
.env_variable("ZELLIJ_PANE_ID")
.and_then(|e| e.trim().parse().ok());
for action in actions {
match action {
Action::CliPipe {
pipe_id,
name,
payload,
plugin,
args,
configuration,
launch_new,
skip_cache,
floating,
in_place,
cwd,
pane_title,
} => {
pipe_client(
&mut os_input,
pipe_id,
name,
payload,
plugin,
args,
configuration,
launch_new,
skip_cache,
floating,
in_place,
pane_id,
cwd,
pane_title,
);
},
action => {
individual_messages_client(&mut os_input, action, pane_id);
},
}
}
os_input.send_to_server(ClientToServerMsg::ClientExited);
}
fn pipe_client(
os_input: &mut Box<dyn ClientOsApi>,
pipe_id: String,
mut name: Option<String>,
mut payload: Option<String>,
plugin: Option<String>,
args: Option<BTreeMap<String, String>>,
mut configuration: Option<BTreeMap<String, String>>,
launch_new: bool,
skip_cache: bool,
floating: Option<bool>,
in_place: Option<bool>,
pane_id: Option<u32>,
cwd: Option<PathBuf>,
pane_title: Option<String>,
) {
let mut stdin = os_input.get_stdin_reader();
let name = name
// first we try to take the explicitly supplied message name
.take()
// then we use the plugin, to facilitate using aliases
.or_else(|| plugin.clone())
// then we use a uuid to at least have some sort of identifier for this message
.or_else(|| Some(Uuid::new_v4().to_string()));
if launch_new {
// we do this to make sure the plugin is unique (has a unique configuration parameter) so
// that a new one would be launched, but we'll still send it to the same instance rather
// than launching a new one in every iteration of the loop
configuration
.get_or_insert_with(BTreeMap::new)
.insert("_zellij_id".to_owned(), Uuid::new_v4().to_string());
}
let create_msg = |payload: Option<String>| -> ClientToServerMsg {
ClientToServerMsg::Action {
action: Action::CliPipe {
pipe_id: pipe_id.clone(),
name: name.clone(),
payload,
args: args.clone(),
plugin: plugin.clone(),
configuration: configuration.clone(),
floating,
in_place,
launch_new,
skip_cache,
cwd: cwd.clone(),
pane_title: pane_title.clone(),
},
terminal_id: pane_id,
client_id: None,
is_cli_client: true,
}
};
let is_piped = !os_input.stdin_is_terminal();
loop {
if let Some(payload) = payload.take() {
let msg = create_msg(Some(payload));
os_input.send_to_server(msg);
} else if !is_piped {
// here we send an empty message to trigger the plugin, because we don't have any more
// data
let msg = create_msg(None);
os_input.send_to_server(msg);
} else {
// we didn't get payload from the command line, meaning we listen on STDIN because this
// signifies the user is about to pipe more (eg. cat my-large-file | zellij pipe ...)
let mut buffer = String::new();
let _ = stdin.read_line(&mut buffer);
if buffer.is_empty() {
let msg = create_msg(None);
os_input.send_to_server(msg);
break;
} else {
// we've got data! send it down the pipe (most common)
let msg = create_msg(Some(buffer));
os_input.send_to_server(msg);
}
}
loop {
// wait for a response and act accordingly
match os_input.recv_from_server() {
Some((ServerToClientMsg::UnblockCliPipeInput { pipe_name }, _)) => {
// unblock this pipe, meaning we need to stop waiting for a response and read
// once more from STDIN
if pipe_name == pipe_id {
if !is_piped {
// if this client is not piped, we need to exit the process completely
// rather than wait for more data
process::exit(0);
} else {
break;
}
}
},
Some((ServerToClientMsg::CliPipeOutput { pipe_name, output }, _)) => {
// send data to STDOUT, this *does not* mean we need to unblock the input
let err_context = "Failed to write to stdout";
if pipe_name == pipe_id {
let mut stdout = os_input.get_stdout_writer();
stdout
.write_all(output.as_bytes())
.context(err_context)
.non_fatal();
stdout.flush().context(err_context).non_fatal();
}
},
Some((ServerToClientMsg::Log { lines: log_lines }, _)) => {
log_lines.iter().for_each(|line| println!("{line}"));
process::exit(0);
},
Some((ServerToClientMsg::LogError { lines: log_lines }, _)) => {
log_lines.iter().for_each(|line| eprintln!("{line}"));
process::exit(2);
},
Some((ServerToClientMsg::Exit { exit_reason }, _)) => match exit_reason {
ExitReason::Error(e) => {
eprintln!("{}", e);
process::exit(2);
},
_ => {
process::exit(0);
},
},
_ => {},
}
}
}
}
fn individual_messages_client(
os_input: &mut Box<dyn ClientOsApi>,
action: Action,
pane_id: Option<u32>,
) {
let msg = ClientToServerMsg::Action {
action,
terminal_id: pane_id,
client_id: None,
is_cli_client: true,
};
os_input.send_to_server(msg);
loop {
match os_input.recv_from_server() {
Some((ServerToClientMsg::UnblockInputThread, _)) => {
break;
},
Some((ServerToClientMsg::Log { lines: log_lines }, _)) => {
log_lines.iter().for_each(|line| println!("{line}"));
break;
},
Some((ServerToClientMsg::LogError { lines: log_lines }, _)) => {
log_lines.iter().for_each(|line| eprintln!("{line}"));
process::exit(2);
},
Some((ServerToClientMsg::Exit { exit_reason }, _)) => match exit_reason {
ExitReason::Error(e) => {
eprintln!("{}", e);
process::exit(2);
},
ExitReason::CustomExitStatus(exit_status) => {
process::exit(exit_status);
},
_ => {
break;
},
},
_ => {},
}
}
}
pub fn start_subscribe_client(
os_input: Box<dyn ClientOsApi>,
session_name: &str,
subscribe_cli: SubscribeCli,
) {
let zellij_ipc_pipe: PathBuf = {
let mut sock_dir = zellij_utils::consts::ZELLIJ_SOCK_DIR.clone();
fs::create_dir_all(&sock_dir).unwrap();
zellij_utils::shared::set_permissions(&sock_dir, 0o700).unwrap();
sock_dir.push(session_name);
sock_dir
};
os_input.connect_to_server(&*zellij_ipc_pipe);
// Parse pane IDs
let pane_ids: Vec<PaneId> = subscribe_cli
.pane_id
.iter()
.map(|s| {
PaneId::from_str(s).unwrap_or_else(|e| {
eprintln!("Invalid pane ID '{}': {}", s, e);
process::exit(2);
})
})
.collect();
// Send subscribe message
os_input.send_to_server(ClientToServerMsg::SubscribeToPaneRenders {
pane_ids: pane_ids.clone(),
scrollback: subscribe_cli.scrollback,
ansi: subscribe_cli.ansi,
});
// Track remaining panes for exit-on-all-closed
let mut remaining_panes: HashSet<PaneId> = pane_ids.into_iter().collect();
// Streaming receive loop
let stdout = io::stdout();
let mut stdout = stdout.lock();
loop {
match os_input.recv_from_server() {
Some((
ServerToClientMsg::PaneRenderUpdate {
pane_id,
viewport,
scrollback,
is_initial,
},
_,
)) => match subscribe_cli.format {
SubscribeFormat::Raw => {
if let Some(ref scrollback_lines) = scrollback {
for line in scrollback_lines {
let _ = writeln!(stdout, "{}", line);
}
}
for line in &viewport {
let _ = writeln!(stdout, "{}", line);
}
let _ = stdout.flush();
},
SubscribeFormat::Json => {
let json = serde_json::json!({
"event": "pane_update",
"pane_id": pane_id.to_string(),
"viewport": viewport,
"scrollback": scrollback,
"is_initial": is_initial,
});
let _ = writeln!(stdout, "{}", json);
let _ = stdout.flush();
},
},
Some((ServerToClientMsg::SubscribedPaneClosed { pane_id }, _)) => {
remaining_panes.remove(&pane_id);
match subscribe_cli.format {
SubscribeFormat::Raw => {},
SubscribeFormat::Json => {
let json = serde_json::json!({
"event": "pane_closed",
"pane_id": pane_id.to_string(),
});
let _ = writeln!(stdout, "{}", json);
let _ = stdout.flush();
},
}
if remaining_panes.is_empty() {
break;
}
},
Some((ServerToClientMsg::Exit { .. }, _)) => break,
Some((ServerToClientMsg::LogError { lines }, _)) => {
for line in lines {
eprintln!("{}", line);
}
process::exit(2);
},
None => break,
_ => {},
}
}
os_input.send_to_server(ClientToServerMsg::ClientExited);
}
|