rmux-server 0.1.1

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
Documentation
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
use super::*;

#[tokio::test]
async fn if_shell_format_mode_dispatches_selected_rmux_command() {
    let handler = RequestHandler::new();

    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: "1".to_owned(),
            format_mode: true,
            then_command: "set-buffer -b chosen selected".to_owned(),
            else_command: Some("set-buffer -b chosen wrong".to_owned()),
            target: None,
            caller_cwd: None,
            background: false,
        }))
        .await;

    assert_eq!(
        response,
        Response::IfShell(rmux_proto::IfShellResponse::no_output())
    );

    let response = handler
        .handle(Request::ShowBuffer(ShowBufferRequest {
            name: Some("chosen".to_owned()),
        }))
        .await;
    assert_eq!(
        response
            .command_output()
            .expect("show-buffer output")
            .stdout(),
        b"selected"
    );
}

#[tokio::test]
async fn if_shell_false_without_else_is_a_successful_noop() {
    let handler = RequestHandler::new();

    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: "0".to_owned(),
            format_mode: true,
            then_command: "set-buffer impossible".to_owned(),
            else_command: None,
            target: None,
            caller_cwd: None,
            background: false,
        }))
        .await;

    assert_eq!(
        response,
        Response::IfShell(rmux_proto::IfShellResponse::no_output())
    );
}

#[tokio::test]
async fn scripted_pane_commands_accept_session_targets_like_tmux() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");

    assert!(matches!(
        handler
            .handle(Request::NewSession(NewSessionRequest {
                session_name: alpha.clone(),
                detached: true,
                size: Some(TerminalSize { cols: 80, rows: 24 }),
                environment: None,
            }))
            .await,
        Response::NewSession(_)
    ));

    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: "1".to_owned(),
            format_mode: true,
            then_command: "copy-mode -t alpha".to_owned(),
            else_command: None,
            target: None,
            caller_cwd: None,
            background: false,
        }))
        .await;
    assert!(matches!(response, Response::IfShell(_)));

    let mode = handler
        .handle(Request::DisplayMessage(DisplayMessageRequest {
            target: Some(Target::Pane(PaneTarget::new(alpha, 0))),
            print: true,
            message: Some("#{pane_in_mode}".to_owned()),
        }))
        .await;
    let output = mode.command_output().expect("display-message output");
    assert_eq!(output.stdout(), b"1\n");
}

#[cfg(unix)]
#[tokio::test]
async fn if_shell_shell_mode_uses_tmux_shell_environment_and_caller_cwd() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let root = temp_root("if-shell-shell-mode");
    let marker = root.join("shell-used.txt");
    let shell_path = root.join("record-shell.sh");

    write_executable_script(
        &shell_path,
        &format!(
            "#!/bin/sh\nprintf used > {}\nexec /bin/sh \"$@\"\n",
            shell_quote(&marker)
        ),
    );

    assert!(matches!(
        handler
            .handle(Request::NewSession(NewSessionRequest {
                session_name: alpha.clone(),
                detached: true,
                size: Some(TerminalSize { cols: 80, rows: 24 }),
                environment: None,
            }))
            .await,
        Response::NewSession(_)
    ));
    assert!(matches!(
        handler
            .handle(Request::SetOption(SetOptionRequest {
                scope: ScopeSelector::Global,
                option: OptionName::DefaultShell,
                value: shell_path.to_string_lossy().into_owned(),
                mode: SetOptionMode::Replace,
            }))
            .await,
        Response::SetOption(_)
    ));
    assert!(matches!(
        handler
            .handle(Request::SetEnvironment(SetEnvironmentRequest {
                scope: ScopeSelector::Session(alpha.clone()),
                name: "FOO".to_owned(),
                value: "bar".to_owned(),
                mode: None,
                hidden: false,
                format: false,
            }))
            .await,
        Response::SetEnvironment(_)
    ));

    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: format!(
                "test \"$FOO\" = bar && test \"$PWD\" = {}",
                shell_quote(&root)
            ),
            format_mode: false,
            then_command: "set-buffer -b chosen yes".to_owned(),
            else_command: Some("set-buffer -b chosen no".to_owned()),
            target: Some(Target::Session(alpha)),
            caller_cwd: Some(root),
            background: false,
        }))
        .await;

    assert_eq!(
        response,
        Response::IfShell(rmux_proto::IfShellResponse::no_output())
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("chosen".to_owned()),
            }))
            .await
            .command_output()
            .expect("show-buffer output")
            .stdout(),
        b"yes"
    );
    assert_eq!(fs::read_to_string(marker).expect("shell marker"), "used");
}

#[cfg(windows)]
#[tokio::test]
async fn if_shell_shell_mode_uses_windows_shell_environment_and_caller_cwd() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let root = temp_root("if-shell-shell-mode");
    fs::create_dir_all(&root).expect("caller cwd");
    let cmd = std::env::var_os("COMSPEC")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("cmd.exe"));

    assert!(matches!(
        handler
            .handle(Request::NewSession(NewSessionRequest {
                session_name: alpha.clone(),
                detached: true,
                size: Some(TerminalSize { cols: 80, rows: 24 }),
                environment: None,
            }))
            .await,
        Response::NewSession(_)
    ));
    assert!(matches!(
        handler
            .handle(Request::SetOption(SetOptionRequest {
                scope: ScopeSelector::Global,
                option: OptionName::DefaultShell,
                value: cmd.to_string_lossy().into_owned(),
                mode: SetOptionMode::Replace,
            }))
            .await,
        Response::SetOption(_)
    ));
    assert!(matches!(
        handler
            .handle(Request::SetEnvironment(SetEnvironmentRequest {
                scope: ScopeSelector::Session(alpha.clone()),
                name: "FOO".to_owned(),
                value: "bar".to_owned(),
                mode: None,
                hidden: false,
                format: false,
            }))
            .await,
        Response::SetEnvironment(_)
    ));

    let root = root.to_string_lossy().into_owned();
    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: format!(
                "if not \"%FOO%\"==\"bar\" exit /b 1 & if not \"%CD%\"==\"{root}\" exit /b 1 & exit /b 0"
            ),
            format_mode: false,
            then_command: "set-buffer -b chosen yes".to_owned(),
            else_command: Some("set-buffer -b chosen no".to_owned()),
            target: Some(Target::Session(alpha)),
            caller_cwd: Some(PathBuf::from(root)),
            background: false,
        }))
        .await;

    assert_eq!(
        response,
        Response::IfShell(rmux_proto::IfShellResponse::no_output())
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("chosen".to_owned()),
            }))
            .await
            .command_output()
            .expect("show-buffer output")
            .stdout(),
        b"yes"
    );
}

#[tokio::test]
async fn if_shell_nested_set_buffer_accepts_hyphen_prefixed_content() {
    let handler = RequestHandler::new();

    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: "1".to_owned(),
            format_mode: true,
            then_command: "set-buffer -b hyphen -value".to_owned(),
            else_command: None,
            target: None,
            caller_cwd: None,
            background: false,
        }))
        .await;

    assert_eq!(
        response,
        Response::IfShell(rmux_proto::IfShellResponse::no_output())
    );

    let response = handler
        .handle(Request::ShowBuffer(ShowBufferRequest {
            name: Some("hyphen".to_owned()),
        }))
        .await;
    assert_eq!(
        response
            .command_output()
            .expect("show-buffer output")
            .stdout(),
        b"-value"
    );
}

#[tokio::test]
async fn if_shell_nested_wait_for_accepts_hyphen_prefixed_channel_after_mode_flag() {
    let handler = RequestHandler::new();

    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: "1".to_owned(),
            format_mode: true,
            then_command: "wait-for -S -channel".to_owned(),
            else_command: None,
            target: None,
            caller_cwd: None,
            background: false,
        }))
        .await;

    assert_eq!(
        response,
        Response::IfShell(rmux_proto::IfShellResponse::no_output())
    );
}

#[tokio::test]
async fn if_shell_nested_run_shell_accepts_double_dash_before_command() {
    let handler = RequestHandler::new();

    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: "1".to_owned(),
            format_mode: true,
            then_command: format!("run-shell -- {}", command_quote(&shell_success_command())),
            else_command: None,
            target: None,
            caller_cwd: None,
            background: false,
        }))
        .await;

    assert_eq!(
        response,
        Response::IfShell(rmux_proto::IfShellResponse::no_output())
    );
}

#[tokio::test]
async fn if_shell_string_mode_runs_multiple_commands_in_one_group() {
    let handler = RequestHandler::new();

    let response = handler
        .handle(Request::IfShell(IfShellRequest {
            condition: "1".to_owned(),
            format_mode: true,
            then_command: "set-buffer -b one first; set-buffer -b two second".to_owned(),
            else_command: None,
            target: None,
            caller_cwd: None,
            background: false,
        }))
        .await;

    assert_eq!(
        response,
        Response::IfShell(rmux_proto::IfShellResponse::no_output())
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("one".to_owned()),
            }))
            .await
            .command_output()
            .expect("one buffer output")
            .stdout(),
        b"first"
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("two".to_owned()),
            }))
            .await
            .command_output()
            .expect("two buffer output")
            .stdout(),
        b"second"
    );
}

#[tokio::test]
async fn if_shell_inserted_assignments_apply_before_parent_queue_tail() {
    let handler = RequestHandler::new();
    let command = shell_env_or_default_command("FOO", "unset");
    let parsed = CommandParser::new()
        .parse(&format!(
            "if-shell -F 1 {{ FOO=bar }} ; run-shell {}",
            command_quote(&command)
        ))
        .expect("commands parse");

    let output = handler
        .execute_parsed_commands_for_test(std::process::id(), parsed)
        .await
        .expect("queue succeeds");

    assert_eq!(output.stdout(), b"bar");

    let state = handler.state.lock().await;
    assert_eq!(state.environment.global_value("FOO"), Some("bar"));
}