rmux-sdk 0.3.0

Public, daemon-backed Rust SDK for the RMUX terminal multiplexer (facade, ensure-session, snapshots, events, detach helpers).
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
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
//! Inert SDK command specification DTOs.
//!
//! The types in this module hold caller intent and map to `rmux-proto`
//! request payloads. They do not open IPC streams, start daemons, inspect
//! processes, probe endpoint paths, parse tmux command text, or resolve
//! targets.

use std::fmt;

use serde::{Deserialize, Serialize};

use crate::types::{PaneRef, TerminalSizeSpec};
use crate::SessionName;

/// Explicit process command mode used by SDK builders and specs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ProcessCommandSpec {
    /// Execute a program directly with structured argv.
    Argv(Vec<String>),
    /// Execute command text through the configured shell.
    Shell(String),
}

impl From<ProcessCommandSpec> for rmux_proto::ProcessCommand {
    fn from(value: ProcessCommandSpec) -> Self {
        match value {
            ProcessCommandSpec::Argv(argv) => Self::Argv(argv),
            ProcessCommandSpec::Shell(command) => Self::Shell(command),
        }
    }
}

impl ProcessCommandSpec {
    pub(crate) fn is_empty(&self) -> bool {
        match self {
            Self::Argv(argv) => argv.is_empty() || argv.first().is_some_and(String::is_empty),
            Self::Shell(command) => command.is_empty(),
        }
    }
}

/// Process-spawn fields shared by SDK command specs.
///
/// `process_command` is the explicit launch-mode path. The older `command`
/// field is retained for low-level tmux-compatible specs where a single item
/// means `$SHELL -c`; new SDK surfaces should use [`ProcessSpec::argv`],
/// [`ProcessSpec::shell`], or the matching session/pane builders rather than
/// struct literals.
#[derive(Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProcessSpec {
    /// Legacy optional command vector. A single item keeps the historical
    /// `$SHELL -c` behavior in protocol handlers.
    #[serde(default)]
    pub command: Option<Vec<String>>,
    /// Explicit process launch mode.
    #[serde(default)]
    pub process_command: Option<ProcessCommandSpec>,
    /// Optional per-spawn environment overrides in `NAME=VALUE` form.
    #[serde(default)]
    pub environment: Option<Vec<String>>,
}

impl ProcessSpec {
    /// Creates a process spec that executes direct argv.
    #[must_use]
    pub fn argv<I, S>(command: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            process_command: Some(ProcessCommandSpec::Argv(
                command.into_iter().map(Into::into).collect(),
            )),
            ..Self::default()
        }
    }

    /// Creates a process spec that executes command text through the shell.
    #[must_use]
    pub fn shell(command: impl Into<String>) -> Self {
        Self {
            process_command: Some(ProcessCommandSpec::Shell(command.into())),
            ..Self::default()
        }
    }

    pub(crate) fn into_proto_parts(
        self,
    ) -> (
        Option<Vec<String>>,
        Option<rmux_proto::ProcessCommand>,
        Option<Vec<String>>,
    ) {
        (
            self.command,
            self.process_command.map(rmux_proto::ProcessCommand::from),
            self.environment,
        )
    }
}

impl fmt::Debug for ProcessSpec {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ProcessSpec")
            .field("command", &self.command)
            .field("process_command", &self.process_command)
            .finish_non_exhaustive()
    }
}

/// Reuse-related flags for `new-session` specs.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NewSessionReuse {
    /// Attach to an existing target session instead of treating it as an error.
    #[serde(default)]
    pub attach_if_exists: bool,
    /// Detach other attached clients before attaching.
    #[serde(default)]
    pub detach_other_clients: bool,
    /// Detach and terminate other attached clients before attaching.
    #[serde(default)]
    pub kill_other_clients: bool,
    /// Optional tmux client-flag names such as `read-only` or `active-pane`.
    #[serde(default)]
    pub flags: Option<Vec<String>>,
}

/// Reuse-related flags for `attach-session` specs.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttachSessionReuse {
    /// Detach other attached clients before attaching.
    #[serde(default)]
    pub detach_other_clients: bool,
    /// Detach and terminate other attached clients before attaching.
    #[serde(default)]
    pub kill_other_clients: bool,
    /// Enable readonly attach mode.
    #[serde(default)]
    pub read_only: bool,
    /// Skip client environment updates.
    #[serde(default)]
    pub skip_environment_update: bool,
    /// Optional tmux client-flag names such as `read-only` or `active-pane`.
    #[serde(default)]
    pub flags: Option<Vec<String>>,
}

/// Terminal/runtime hints captured by a caller.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClientTerminalSpec {
    /// Explicit terminal feature names contributed by top-level client flags.
    #[serde(default)]
    pub terminal_features: Vec<String>,
    /// Whether the invoking client should be treated as UTF-8 capable.
    #[serde(default)]
    pub utf8: bool,
}

impl From<ClientTerminalSpec> for rmux_proto::ClientTerminalContext {
    fn from(value: ClientTerminalSpec) -> Self {
        Self {
            terminal_features: value.terminal_features,
            utf8: value.utf8,
        }
    }
}

impl From<rmux_proto::ClientTerminalContext> for ClientTerminalSpec {
    fn from(value: rmux_proto::ClientTerminalContext) -> Self {
        Self {
            terminal_features: value.terminal_features,
            utf8: value.utf8,
        }
    }
}

/// SDK value object for `new-session`.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NewSessionSpec {
    /// Optional exact session name to create.
    #[serde(default)]
    pub session_name: Option<SessionName>,
    /// Optional tmux format-expanded start directory for the new session.
    #[serde(default)]
    pub working_directory: Option<String>,
    /// Whether the session should remain detached after creation.
    #[serde(default)]
    pub detached: bool,
    /// Initial pane geometry, when explicitly requested.
    #[serde(default)]
    pub size: Option<TerminalSizeSpec>,
    /// Process-spawn fields for the initial pane.
    #[serde(default)]
    pub process: ProcessSpec,
    /// Optional target session or group name for grouped-session creation.
    #[serde(default)]
    pub group_target: Option<SessionName>,
    /// Reuse and client-detach flags.
    #[serde(default)]
    pub reuse: NewSessionReuse,
    /// Optional initial active-window name.
    #[serde(default)]
    pub window_name: Option<String>,
    /// Whether to print formatted session information.
    #[serde(default)]
    pub print_session_info: bool,
    /// Optional format template used when printing session information.
    #[serde(default)]
    pub print_format: Option<String>,
}

impl From<NewSessionSpec> for rmux_proto::NewSessionExtRequest {
    fn from(value: NewSessionSpec) -> Self {
        let (command, process_command, environment) = value.process.into_proto_parts();
        Self {
            session_name: value.session_name,
            working_directory: value.working_directory,
            detached: value.detached,
            size: value.size.map(Into::into),
            environment,
            group_target: value.group_target,
            attach_if_exists: value.reuse.attach_if_exists,
            detach_other_clients: value.reuse.detach_other_clients,
            kill_other_clients: value.reuse.kill_other_clients,
            flags: value.reuse.flags,
            window_name: value.window_name,
            print_session_info: value.print_session_info,
            print_format: value.print_format,
            command,
            process_command,
        }
    }
}

/// SDK value object for `attach-session`.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttachSessionSpec {
    /// Optional exact target session name.
    #[serde(default)]
    pub target: Option<SessionName>,
    /// Optional raw tmux-style target text, including window or pane selectors.
    #[serde(default)]
    pub target_spec: Option<String>,
    /// Reuse and client-detach flags.
    #[serde(default)]
    pub reuse: AttachSessionReuse,
    /// Optional tmux format-expanded working directory applied to the target.
    #[serde(default)]
    pub working_directory: Option<String>,
    /// Terminal/runtime hints captured from the invoking client.
    #[serde(default)]
    pub client_terminal: ClientTerminalSpec,
    /// The invoking client terminal size, when known.
    #[serde(default)]
    pub client_size: Option<TerminalSizeSpec>,
}

impl From<AttachSessionSpec> for rmux_proto::AttachSessionExt2Request {
    fn from(value: AttachSessionSpec) -> Self {
        Self {
            target: value.target,
            target_spec: value.target_spec,
            detach_other_clients: value.reuse.detach_other_clients,
            kill_other_clients: value.reuse.kill_other_clients,
            read_only: value.reuse.read_only,
            skip_environment_update: value.reuse.skip_environment_update,
            flags: value.reuse.flags,
            working_directory: value.working_directory,
            client_terminal: value.client_terminal.into(),
            client_size: value.client_size.map(Into::into),
        }
    }
}

/// Control-mode subscription fields for `refresh-client`.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubscriptionSpec {
    /// Control-mode subscription updates from `refresh-client -A`.
    #[serde(default)]
    pub subscriptions: Vec<String>,
    /// Control-mode subscription definitions from `refresh-client -B`.
    #[serde(default)]
    pub subscriptions_format: Vec<String>,
}

/// SDK value object for `refresh-client`.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RefreshClientSpec {
    /// Optional target-client identifier or `=`.
    #[serde(default)]
    pub target_client: Option<String>,
    /// Optional pan adjustment used with directional panning.
    #[serde(default)]
    pub adjustment: Option<u32>,
    /// Whether client panning should be cleared.
    #[serde(default)]
    pub clear_pan: bool,
    /// Whether the client view should pan left.
    #[serde(default)]
    pub pan_left: bool,
    /// Whether the client view should pan right.
    #[serde(default)]
    pub pan_right: bool,
    /// Whether the client view should pan up.
    #[serde(default)]
    pub pan_up: bool,
    /// Whether the client view should pan down.
    #[serde(default)]
    pub pan_down: bool,
    /// Whether only the status line should be redrawn.
    #[serde(default)]
    pub status_only: bool,
    /// Whether the client clipboard should be queried.
    #[serde(default)]
    pub clipboard_query: bool,
    /// Optional client-flag string from `-f`.
    #[serde(default)]
    pub flags: Option<String>,
    /// Optional client-flag string from `-F`.
    #[serde(default)]
    pub flags_alias: Option<String>,
    /// Control-mode subscription fields.
    #[serde(default)]
    pub subscriptions: SubscriptionSpec,
    /// Optional control-mode size string from `-C`.
    #[serde(default)]
    pub control_size: Option<String>,
    /// Optional control-mode colour report request from `-r`.
    #[serde(default)]
    pub colour_report: Option<String>,
}

impl From<RefreshClientSpec> for rmux_proto::RefreshClientRequest {
    fn from(value: RefreshClientSpec) -> Self {
        Self {
            target_client: value.target_client,
            adjustment: value.adjustment,
            clear_pan: value.clear_pan,
            pan_left: value.pan_left,
            pan_right: value.pan_right,
            pan_up: value.pan_up,
            pan_down: value.pan_down,
            status_only: value.status_only,
            clipboard_query: value.clipboard_query,
            flags: value.flags,
            flags_alias: value.flags_alias,
            subscriptions: value.subscriptions.subscriptions,
            subscriptions_format: value.subscriptions.subscriptions_format,
            control_size: value.control_size,
            colour_report: value.colour_report,
        }
    }
}

/// Low-level split orientation used by [`SplitSpec`] and the script-level
/// `RmuxCommand` API.
///
/// Variants follow tmux's flag convention (pane arrangement). Prefer the
/// ergonomic [`SplitDirection`](crate::SplitDirection) (`Right`/`Left`/`Up`/
/// `Down`) on [`Pane::split`](crate::Pane::split), which removes the
/// arrangement-vs-divider-line ambiguity.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SplitDirectionSpec {
    /// Stacked panes (top + bottom). Matches tmux `split-window -v`,
    /// the tmux default when no flag is passed.
    #[default]
    Vertical,
    /// Side-by-side panes (left + right). Matches tmux `split-window -h`.
    Horizontal,
}

impl From<SplitDirectionSpec> for rmux_proto::SplitDirection {
    fn from(value: SplitDirectionSpec) -> Self {
        match value {
            SplitDirectionSpec::Vertical => Self::Vertical,
            SplitDirectionSpec::Horizontal => Self::Horizontal,
        }
    }
}

impl From<rmux_proto::SplitDirection> for SplitDirectionSpec {
    fn from(value: rmux_proto::SplitDirection) -> Self {
        match value {
            rmux_proto::SplitDirection::Vertical => Self::Vertical,
            rmux_proto::SplitDirection::Horizontal => Self::Horizontal,
        }
    }
}

/// SDK split target.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SplitTargetSpec {
    /// Split the active pane in the addressed session.
    Session(SessionName),
    /// Split the addressed pane directly.
    Pane(PaneRef),
}

impl From<SplitTargetSpec> for rmux_proto::SplitWindowTarget {
    fn from(value: SplitTargetSpec) -> Self {
        match value {
            SplitTargetSpec::Session(session_name) => Self::Session(session_name),
            SplitTargetSpec::Pane(target) => Self::Pane(target.into()),
        }
    }
}

impl From<rmux_proto::SplitWindowTarget> for SplitTargetSpec {
    fn from(value: rmux_proto::SplitWindowTarget) -> Self {
        match value {
            rmux_proto::SplitWindowTarget::Session(session_name) => Self::Session(session_name),
            rmux_proto::SplitWindowTarget::Pane(target) => Self::Pane(target.into()),
        }
    }
}

impl From<&SplitTargetSpec> for rmux_proto::SplitWindowTarget {
    fn from(value: &SplitTargetSpec) -> Self {
        match value {
            SplitTargetSpec::Session(session_name) => Self::Session(session_name.clone()),
            SplitTargetSpec::Pane(target) => Self::Pane(target.into()),
        }
    }
}

/// SDK value object for `split-window`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SplitSpec {
    /// Exact split target.
    pub target: SplitTargetSpec,
    /// Requested split direction.
    #[serde(default)]
    pub direction: SplitDirectionSpec,
    /// Whether the new pane is inserted before the target on the chosen
    /// axis (tmux `-b`). Default `false` puts the new pane after the target.
    #[serde(default)]
    pub before: bool,
    /// Process-spawn fields for the new pane.
    #[serde(default)]
    pub process: ProcessSpec,
}

impl From<SplitSpec> for rmux_proto::SplitWindowExtRequest {
    fn from(value: SplitSpec) -> Self {
        let (command, process_command, environment) = value.process.into_proto_parts();
        Self {
            target: value.target.into(),
            direction: value.direction.into(),
            before: value.before,
            environment,
            command,
            process_command,
            start_directory: None,
            keep_alive_on_exit: None,
        }
    }
}