rmux-server 0.5.0

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
410
411
412
413
414
415
416
417
418
use super::*;

#[tokio::test]
async fn source_file_continues_after_parse_errors_and_loads_later_files() {
    let handler = RequestHandler::new();
    let root = temp_root("multi-path-parse-error");
    let bad = root.join("bad.conf");
    let good = root.join("good.conf");
    write_config(&bad, "not-a-command\n");
    write_config(&good, "set-buffer -b parsed-after ok\n");

    let response = handler
        .handle(source_file_request(
            vec!["bad.conf".to_owned(), "good.conf".to_owned()],
            Some(root),
        ))
        .await;

    match response {
        Response::Error(rmux_proto::ErrorResponse { error }) => {
            assert_eq!(
                error.to_string(),
                format!(
                    "server error: {}: unknown command: not-a-command",
                    bad.display()
                )
            );
        }
        other => panic!("expected source-file error, got {other:?}"),
    }
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("parsed-after".to_owned()),
            }))
            .await
            .command_output()
            .expect("parsed-after buffer output")
            .stdout(),
        b"ok"
    );
}

#[tokio::test]
async fn source_file_continuation_inside_single_quoted_string() {
    let handler = RequestHandler::new();
    let root = temp_root("sq-cont");
    write_config(&root.join("sq.conf"), "set-buffer -b sq 'hello\\\nworld'\n");

    let response = handler
        .handle(source_file_request(vec!["sq.conf".to_owned()], Some(root)))
        .await;

    assert_eq!(
        response,
        Response::SourceFile(rmux_proto::SourceFileResponse { output: None })
    );
    // In single quotes, backslash-newline is literal (no joining).
    // tmux's lexer treats continuation (backslash-newline) at the get_char level,
    // before quote processing. So single-quoted strings DO get continuation joining.
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("sq".to_owned()),
            }))
            .await
            .command_output()
            .expect("sq buffer output")
            .stdout(),
        b"helloworld"
    );
}

#[tokio::test]
async fn source_file_nested_if_elif_else_endif_branches() {
    let handler = RequestHandler::new();
    let root = temp_root("nested-if");
    write_config(
        &root.join("branches.conf"),
        "%if 0\nset-buffer -b branch wrong1\n%elif 0\nset-buffer -b branch wrong2\n%elif 1\nset-buffer -b branch correct\n%else\nset-buffer -b branch wrong3\n%endif\n",
    );

    let response = handler
        .handle(source_file_request(
            vec!["branches.conf".to_owned()],
            Some(root),
        ))
        .await;

    assert_eq!(
        response,
        Response::SourceFile(rmux_proto::SourceFileResponse { output: None })
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("branch".to_owned()),
            }))
            .await
            .command_output()
            .expect("branch buffer output")
            .stdout(),
        b"correct"
    );
}

#[tokio::test]
async fn source_file_if_with_format_expression_condition() {
    let handler = RequestHandler::new();
    let root = temp_root("if-format");
    // current_file is set during source-file loading, so #{current_file} should be truthy.
    write_config(
        &root.join("fmt.conf"),
        "%if #{current_file}\nset-buffer -b fmt-cond yes\n%else\nset-buffer -b fmt-cond no\n%endif\n",
    );

    let response = handler
        .handle(source_file_request(vec!["fmt.conf".to_owned()], Some(root)))
        .await;

    assert_eq!(
        response,
        Response::SourceFile(rmux_proto::SourceFileResponse { output: None })
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("fmt-cond".to_owned()),
            }))
            .await
            .command_output()
            .expect("fmt-cond buffer output")
            .stdout(),
        b"yes"
    );
}

#[tokio::test]
async fn source_file_stdin_dash_without_stdin_returns_error() {
    let handler = RequestHandler::new();
    let root = temp_root("stdin-missing");
    fs::create_dir_all(&root).expect("create temp root");

    let response = handler
        .handle(Request::SourceFile(SourceFileRequest {
            paths: vec!["-".to_owned()],
            quiet: false,
            parse_only: false,
            verbose: false,
            expand_paths: false,
            target: None,
            caller_cwd: Some(root),
            stdin: None,
        }))
        .await;

    assert!(
        matches!(response, Response::Error(ref e) if e.error.to_string().contains("stdin")),
        "expected stdin error, got {response:?}"
    );
}

#[tokio::test]
async fn source_file_ignores_server_scope_for_non_server_options_like_tmux() {
    let handler = RequestHandler::new();
    let root = temp_root("set-option-server-scope");
    fs::create_dir_all(&root).expect("create temp root");
    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::SourceFile(SourceFileRequest {
            paths: vec!["-".to_owned()],
            quiet: false,
            parse_only: false,
            verbose: false,
            expand_paths: false,
            target: Some(PaneTarget::with_window(alpha, 0, 0)),
            caller_cwd: Some(root),
            stdin: Some("set-option -s status off\n".to_owned()),
        }))
        .await;

    assert_eq!(
        response,
        Response::SourceFile(rmux_proto::SourceFileResponse { output: None })
    );
    let response = handler
        .handle(Request::ShowOptions(ShowOptionsRequest {
            scope: OptionScopeSelector::SessionGlobal,
            name: Some("status".to_owned()),
            value_only: true,
            include_inherited: false,
        }))
        .await;
    assert_eq!(
        response.command_output().expect("status output").stdout(),
        b"on\n"
    );
}

#[tokio::test]
async fn source_file_routes_window_show_commands_and_global_show_scope_compatibility() {
    let handler = RequestHandler::new();
    let root = temp_root("show-options-compat");
    fs::create_dir_all(&root).expect("create temp root");
    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::SourceFile(SourceFileRequest {
            paths: vec!["-".to_owned()],
            quiet: false,
            parse_only: false,
            verbose: false,
            expand_paths: false,
            target: Some(PaneTarget::with_window(alpha, 0, 0)),
            caller_cwd: Some(root),
            stdin: Some(
                "set-option -s message-limit 77\n\
set -gq status off\n\
set -gw pane-border-style fg=colour3\n\
set-window-option -gw pane-active-border-style fg=colour5\n\
set -gw copy-mode-selection-style bg=cyan,fg=black\n\
set-option -ag status-left append\n\
	show-options -gqsv -t alpha message-limit\n\
show-options -gqv status\n\
show-window-options -g -t alpha -v pane-border-style\n\
show-window-options -g -v pane-active-border-style\n\
show-window-options -g -v copy-mode-selection-style\n"
                    .to_owned(),
            ),
        }))
        .await;

    assert_eq!(
        response
            .command_output()
            .unwrap_or_else(|| panic!("queued show-options output, got {response:?}"))
            .stdout(),
        b"77\noff\nfg=colour3\nfg=colour5\nbg=cyan,fg=black\n"
    );
}

#[tokio::test]
async fn source_file_without_target_routes_append_to_default_global_scope() {
    let handler = RequestHandler::new();
    let root = temp_root("set-option-append-no-target");
    fs::create_dir_all(&root).expect("create temp root");

    let response = handler
        .handle(Request::SourceFile(SourceFileRequest {
            paths: vec!["-".to_owned()],
            quiet: false,
            parse_only: false,
            verbose: false,
            expand_paths: false,
            target: None,
            caller_cwd: Some(root),
            stdin: Some("set-option -ag status-left append\n".to_owned()),
        }))
        .await;

    assert!(
        matches!(response, Response::SourceFile(_)),
        "set-option append without a current target should load, got {response:?}"
    );
    let state = handler.state.lock().await;
    assert_eq!(
        state.options.global_value(OptionName::StatusLeft),
        Some("[#{session_name}] append")
    );
}

#[tokio::test]
async fn source_file_without_target_uses_preferred_session_for_parse_time_formats() {
    let handler = RequestHandler::new();
    let root = temp_root("source-file-implicit-target");
    fs::create_dir_all(&root).expect("create temp root");
    let alpha = session_name("alpha");
    assert!(matches!(
        handler
            .handle(Request::NewSession(NewSessionRequest {
                session_name: alpha,
                detached: true,
                size: Some(TerminalSize { cols: 80, rows: 24 }),
                environment: None,
            }))
            .await,
        Response::NewSession(_)
    ));

    let response = handler
        .handle(Request::SourceFile(SourceFileRequest {
            paths: vec!["-".to_owned()],
            quiet: false,
            parse_only: false,
            verbose: false,
            expand_paths: false,
            target: None,
            caller_cwd: Some(root),
            stdin: Some(
                "%if #{==:#{session_name},alpha}\n\
set-buffer -b implicit yes\n\
%else\n\
set-buffer -b implicit no\n\
%endif\n\
if-shell -F '#{==:#{window_index},0}' 'set-buffer -b implicit-if yes' 'set-buffer -b implicit-if no'\n"
                    .to_owned(),
            ),
        }))
        .await;

    assert!(matches!(response, Response::SourceFile(_)));
    let state = handler.state.lock().await;
    let (_, content) = state
        .buffers
        .show(Some("implicit"))
        .expect("implicit buffer exists");
    assert_eq!(String::from_utf8_lossy(content), "yes");
    let (_, content) = state
        .buffers
        .show(Some("implicit-if"))
        .expect("implicit-if buffer exists");
    assert_eq!(String::from_utf8_lossy(content), "yes");
}

#[tokio::test]
async fn source_file_comment_after_command_is_ignored() {
    let handler = RequestHandler::new();
    let root = temp_root("comment-after");
    write_config(
        &root.join("commented.conf"),
        "set-buffer -b commented value # this is a comment\n",
    );

    let response = handler
        .handle(source_file_request(
            vec!["commented.conf".to_owned()],
            Some(root),
        ))
        .await;

    assert_eq!(
        response,
        Response::SourceFile(rmux_proto::SourceFileResponse { output: None })
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("commented".to_owned()),
            }))
            .await
            .command_output()
            .expect("commented buffer output")
            .stdout(),
        b"value"
    );
}

#[tokio::test]
async fn source_file_glob_expands_matching_files() {
    let handler = RequestHandler::new();
    let root = temp_root("glob-expand");
    write_config(&root.join("a.conf"), "set-buffer -b glob-a yes\n");
    write_config(&root.join("b.conf"), "set-buffer -b glob-b yes\n");

    let response = handler
        .handle(source_file_request(vec!["*.conf".to_owned()], Some(root)))
        .await;

    assert_eq!(
        response,
        Response::SourceFile(rmux_proto::SourceFileResponse { output: None })
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("glob-a".to_owned()),
            }))
            .await
            .command_output()
            .expect("glob-a buffer output")
            .stdout(),
        b"yes"
    );
    assert_eq!(
        handler
            .handle(Request::ShowBuffer(ShowBufferRequest {
                name: Some("glob-b".to_owned()),
            }))
            .await
            .command_output()
            .expect("glob-b buffer output")
            .stdout(),
        b"yes"
    );
}