fux 0.11.0

Minimal persistent terminal multiplexer built on bevy_ecs
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! CLI for existing-pane layout edits. Exported revisions make concurrent edits explicit.
use anyhow::{Context, Result, bail};
use clap::{Args, Subcommand};
use fux::ids::{PaneId, TabId};
use fux::layout::{Direction, LayoutDocument, NodeId};
use fux::proto::control::{
    CommandResult, LayoutAction, LayoutZoom, MAX_FRAME_BYTES, PaneDestination, Reply, Request,
};
use std::io::Read;
use std::path::PathBuf;

#[derive(Debug, Args)]
pub struct LayoutArgs {
    /// Stable tab ID from `fux list`.
    tab: u32,
    /// Observed layout generation; required for changes.
    #[arg(long, global = true)]
    generation: Option<u64>,
    /// Observed server instance from layout export; required for changes.
    #[arg(long, global = true)]
    instance: Option<String>,
    #[command(subcommand)]
    action: Edit,
}

#[derive(Debug, Default, Args)]
struct TransferFocus {
    /// Select the moved pane in the destination workspace default.
    #[arg(long, conflicts_with = "no_focus")]
    focus: bool,
    /// Preserve existing selections (default); source removal may require a fallback.
    #[arg(long)]
    no_focus: bool,
}

#[derive(Debug, Args)]
pub struct WorkspaceTransferArgs {
    #[command(flatten)]
    focus: TransferFocus,
    /// Existing live pane to transfer using the global manager socket.
    pane: u32,
    #[arg(long)]
    instance: String,
    #[arg(long)]
    source_tab: u32,
    #[arg(long)]
    generation: u64,
    /// Destination workspace name and lifetime from its listing.
    #[arg(long)]
    workspace: String,
    #[arg(
        long,
        required_unless_present = "new_workspace",
        conflicts_with = "new_workspace"
    )]
    stream: Option<u64>,
    /// Create the destination workspace around this pane, without starting another process.
    #[arg(long, conflicts_with = "destination_tab")]
    new_workspace: bool,
    /// Omit to create a new tab around the existing pane.
    #[arg(long, requires_all = ["destination_generation", "target"], conflicts_with = "label")]
    destination_tab: Option<u32>,
    #[arg(long, requires = "destination_tab")]
    destination_generation: Option<u64>,
    #[arg(long, requires = "destination_tab")]
    target: Option<u32>,
    #[arg(long, value_enum, default_value = "right")]
    side: Direction,
    /// Existing target's share on a 10000 scale; only applies to an existing tab.
    #[arg(long, requires = "destination_tab", value_parser = clap::value_parser!(u16).range(500..=9500))]
    ratio: Option<u16>,
    /// Label for a new destination tab.
    #[arg(long)]
    label: Option<String>,
}

impl WorkspaceTransferArgs {
    pub fn request(self) -> Result<fux::daemon::ManagerRequest> {
        let destination = match (
            self.destination_tab,
            self.destination_generation,
            self.target,
        ) {
            (Some(tab), Some(generation), Some(target)) => PaneDestination::Tab {
                ratio: self.ratio.unwrap_or(5000),
                tab: TabId(tab),
                generation,
                target: PaneId(target),
            },
            (None, None, None) => PaneDestination::NewTab { label: self.label },
            _ => bail!(
                "existing destination needs --destination-tab, --destination-generation and --target"
            ),
        };
        Ok(fux::daemon::ManagerRequest::Transfer {
            transfer: fux::proto::control::WorkspaceTransfer {
                focus: self.focus.focus,
                follow: None,
                instance: self.instance,
                source: TabId(self.source_tab),
                generation: self.generation,
                pane: PaneId(self.pane),
                workspace: if self.new_workspace {
                    fux::proto::control::WorkspaceDestination::New {
                        name: self.workspace,
                    }
                } else {
                    fux::proto::control::WorkspaceDestination::Existing {
                        name: self.workspace,
                        stream: self.stream.context("destination stream required")?,
                    }
                },
                destination,
                side: self.side,
            },
        })
    }
}

#[derive(Debug, Subcommand)]
enum Edit {
    /// Move the existing pane to a new tab in this workspace.
    NewTab {
        pane: u32,
        label: Option<String>,
        #[command(flatten)]
        focus: TransferFocus,
    },
    /// Move beside a pane in another tab using its observed generation.
    ToTab {
        pane: u32,
        #[command(flatten)]
        focus: TransferFocus,
        tab: u32,
        destination_generation: u64,
        target: u32,
        #[arg(value_enum)]
        side: Direction,
        /// Existing target pane share on a 10000 scale.
        #[arg(long, default_value_t = 5000, value_parser = clap::value_parser!(u16).range(500..=9500))]
        ratio: u16,
    },
    /// Read pane geometry, directional neighbors and outer-edge flags without changing focus.
    Inspect { pane: u32 },
    /// Export geometry, existing pane IDs and the revision for later edits.
    Export,
    /// Exchange two panes' positions without changing their processes.
    Swap { pane: u32, target: u32 },
    /// Remove/reinsert a pane beside another pane in this tab.
    Move {
        pane: u32,
        target: u32,
        #[arg(value_enum)]
        side: Direction,
    },
    /// Grow the branch toward a directional boundary (negative delta shrinks).
    Resize {
        pane: u32,
        #[arg(value_enum)]
        direction: Direction,
        #[arg(allow_hyphen_values = true)]
        delta: i16,
    },
    /// Set an exported split node's ratio in units of 1/10,000 (500..9500).
    Ratio { split: u32, ratio: u16 },
    /// Show one pane at full size; omit PANE to restore the split layout.
    Zoom { pane: Option<u32> },
    /// Apply a document or complete export reply from FILE to these existing panes.
    Apply {
        file: PathBuf,
        /// Complete source-to-existing-pane mapping; repeat for every source pane.
        #[arg(long = "map", value_parser = parse_mapping)]
        remap: Vec<(PaneId, PaneId)>,
    },
}

impl LayoutArgs {
    pub fn request(self) -> Result<Request> {
        if !matches!(self.action, Edit::Export | Edit::Inspect { .. })
            && (self.generation.is_none() || self.instance.is_none())
        {
            bail!("export the layout first; changes require --generation and --instance");
        }
        let action = match self.action {
            Edit::Export => LayoutAction::Export,
            Edit::Inspect { pane } => LayoutAction::Inspect { pane: PaneId(pane) },
            Edit::NewTab { pane, label, focus } => LayoutAction::Transfer {
                focus: focus.focus,
                pane: PaneId(pane),
                destination: PaneDestination::NewTab { label },
                side: Direction::Right,
            },
            Edit::ToTab {
                focus,
                pane,
                tab,
                destination_generation,
                target,
                side,
                ratio,
            } => LayoutAction::Transfer {
                focus: focus.focus,
                pane: PaneId(pane),
                destination: PaneDestination::Tab {
                    ratio,
                    tab: TabId(tab),
                    generation: destination_generation,
                    target: PaneId(target),
                },
                side,
            },
            Edit::Swap { pane, target } => LayoutAction::Swap {
                pane: PaneId(pane),
                target: PaneId(target),
            },
            Edit::Move { pane, target, side } => LayoutAction::Relocate {
                pane: PaneId(pane),
                target: PaneId(target),
                side,
            },
            Edit::Resize {
                pane,
                direction,
                delta,
            } => LayoutAction::ResizeToward {
                pane: PaneId(pane),
                direction,
                delta,
            },
            Edit::Ratio { split, ratio } => LayoutAction::SetRatio {
                split: NodeId(split),
                ratio,
            },
            Edit::Zoom { pane } => LayoutAction::Zoom {
                pane: pane.map(PaneId),
            },
            Edit::Apply { file, remap } => {
                let (document, zoom, labels) = read_document(&file)?;
                LayoutAction::Apply {
                    document,
                    remap,
                    zoom,
                    labels,
                }
            }
        };
        let request = Request::Layout {
            id: 1,
            instance: self.instance,
            tab: TabId(self.tab),
            generation: self.generation,
            action,
        };
        request.validate()?;
        Ok(request)
    }
}

fn parse_mapping(value: &str) -> Result<(PaneId, PaneId), String> {
    let (source, destination) = value
        .split_once('=')
        .ok_or_else(|| "use SOURCE=DESTINATION".to_owned())?;
    Ok((
        PaneId(source.parse().map_err(|_| "invalid source pane ID")?),
        PaneId(
            destination
                .parse()
                .map_err(|_| "invalid destination pane ID")?,
        ),
    ))
}

type ImportedLayout = (
    LayoutDocument<PaneId>,
    LayoutZoom,
    Option<fux::proto::control::PaneLabels>,
);

fn read_document(path: &std::path::Path) -> Result<ImportedLayout> {
    let file =
        std::fs::File::open(path).with_context(|| format!("open layout {}", path.display()))?;
    let mut bytes = Vec::new();
    file.take((MAX_FRAME_BYTES + 1) as u64)
        .read_to_end(&mut bytes)?;
    if bytes.len() > MAX_FRAME_BYTES {
        bail!("layout exceeds the control frame size limit");
    }
    parse_document(&bytes)
}

fn parse_document(bytes: &[u8]) -> Result<ImportedLayout> {
    if let Ok(document) = serde_json::from_slice::<LayoutDocument<PaneId>>(bytes) {
        return Ok((document, LayoutZoom::Preserve, None));
    }
    match serde_json::from_slice::<Reply>(bytes)
        .context("expected a layout document or layout export reply")?
    {
        Reply::Completed {
            result:
                CommandResult::Layout {
                    document,
                    zoomed,
                    labels,
                    ..
                },
            ..
        } => Ok((document, LayoutZoom::Set { pane: zoomed }, Some(labels))),
        _ => bail!("file is not a successful layout export"),
    }
}

pub fn read_archive(path: &std::path::Path) -> Result<fux::proto::control::LayoutArchive> {
    let mut bytes = Vec::new();
    std::fs::File::open(path)?
        .take((MAX_FRAME_BYTES + 1) as u64)
        .read_to_end(&mut bytes)?;
    if bytes.len() > MAX_FRAME_BYTES {
        bail!("archive exceeds the control frame size limit");
    }
    if let Ok(archive) = serde_json::from_slice::<fux::proto::control::LayoutArchive>(&bytes) {
        return Ok(archive);
    }
    match serde_json::from_slice::<fux::daemon::ManagerReply>(&bytes)? {
        fux::daemon::ManagerReply::LayoutArchive { archive } => Ok(archive),
        _ => bail!("file is not a layout archive"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    #[derive(Parser)]
    struct TestCli {
        #[command(flatten)]
        layout: LayoutArgs,
    }

    #[derive(Parser)]
    struct TransferCli {
        #[command(flatten)]
        transfer: WorkspaceTransferArgs,
    }

    #[test]
    fn creating_a_destination_is_explicit_and_incompatible_with_existing_targets() -> Result<()> {
        let base = [
            "transfer",
            "1",
            "--instance",
            "test-instance",
            "--source-tab",
            "1",
            "--generation",
            "7",
            "--workspace",
            "new",
        ];
        assert!(TransferCli::try_parse_from(base).is_err());
        assert!(
            TransferCli::try_parse_from(base.into_iter().chain([
                "--new-workspace",
                "--focus",
                "--no-focus"
            ]))
            .is_err()
        );
        let focused =
            TransferCli::try_parse_from(base.into_iter().chain(["--new-workspace", "--focus"]))?;
        assert!(matches!(
            focused.transfer.request()?,
            fux::daemon::ManagerRequest::Transfer {
                transfer: fux::proto::control::WorkspaceTransfer { focus: true, .. }
            }
        ));

        let create = TransferCli::try_parse_from(base.into_iter().chain(["--new-workspace"]))?;
        assert!(matches!(
            create.transfer.request()?,
            fux::daemon::ManagerRequest::Transfer {
                transfer: fux::proto::control::WorkspaceTransfer {
                    focus: false,
                    workspace: fux::proto::control::WorkspaceDestination::New { .. },
                    destination: PaneDestination::NewTab { .. },
                    ..
                }
            }
        ));
        assert!(
            TransferCli::try_parse_from(base.into_iter().chain([
                "--new-workspace",
                "--stream",
                "4"
            ]))
            .is_err()
        );
        assert!(
            TransferCli::try_parse_from(base.into_iter().chain([
                "--new-workspace",
                "--destination-tab",
                "2",
                "--destination-generation",
                "8",
                "--target",
                "3"
            ]))
            .is_err()
        );
        assert!(
            TransferCli::try_parse_from(base.into_iter().chain([
                "--new-workspace",
                "--ratio",
                "7000"
            ]))
            .is_err()
        );
        for ratio in ["499", "9501", "nan"] {
            assert!(
                TransferCli::try_parse_from(base.into_iter().chain([
                    "--stream",
                    "4",
                    "--destination-tab",
                    "2",
                    "--destination-generation",
                    "8",
                    "--target",
                    "3",
                    "--ratio",
                    ratio
                ]))
                .is_err()
            );
        }
        let configured = TransferCli::try_parse_from(base.into_iter().chain([
            "--stream",
            "4",
            "--destination-tab",
            "2",
            "--destination-generation",
            "8",
            "--target",
            "3",
            "--ratio",
            "7000",
        ]))?;
        assert!(matches!(
            configured.transfer.request()?,
            fux::daemon::ManagerRequest::Transfer {
                transfer: fux::proto::control::WorkspaceTransfer {
                    focus: false,
                    destination: PaneDestination::Tab { ratio: 7000, .. },
                    ..
                }
            }
        ));
        let existing = TransferCli::try_parse_from(base.into_iter().chain(["--stream", "4"]))?;
        assert!(matches!(
            existing.transfer.request()?,
            fux::daemon::ManagerRequest::Transfer {
                transfer: fux::proto::control::WorkspaceTransfer {
                    focus: false,
                    workspace: fux::proto::control::WorkspaceDestination::Existing {
                        stream: 4,
                        ..
                    },
                    ..
                }
            }
        ));
        Ok(())
    }

    #[test]
    fn full_exports_restore_zoom_but_bare_documents_preserve_it() -> Result<()> {
        let document = LayoutDocument {
            root: Some(0),
            nodes: vec![fux::layout::LayoutNode::Pane { pane: PaneId(7) }],
        };
        assert_eq!(
            parse_document(&serde_json::to_vec(&document)?)?,
            (document.clone(), LayoutZoom::Preserve, None)
        );
        for zoomed in [None, Some(PaneId(7))] {
            let reply = Reply::Completed {
                id: 1,
                result: CommandResult::Layout {
                    instance: "source".into(),
                    tab: TabId(3),
                    generation: 9,
                    document: document.clone(),
                    zoomed,
                    labels: vec![(PaneId(7), "saved label".into())],
                },
            };
            assert_eq!(
                parse_document(&serde_json::to_vec(&reply)?)?,
                (
                    document.clone(),
                    LayoutZoom::Set { pane: zoomed },
                    Some(vec![(PaneId(7), "saved label".into())])
                )
            );
        }
        Ok(())
    }

    #[test]
    fn changes_require_observed_identity_and_export_needs_no_guard() -> Result<()> {
        let export = TestCli::try_parse_from(["layout", "1", "export"])?;
        assert!(matches!(
            export.layout.request()?,
            Request::Layout {
                action: LayoutAction::Export,
                generation: None,
                ..
            }
        ));
        let inspect = TestCli::try_parse_from(["layout", "1", "inspect", "2"])?;
        assert!(matches!(
            inspect.layout.request()?,
            Request::Layout {
                action: LayoutAction::Inspect { pane: PaneId(2) },
                generation: None,
                instance: None,
                ..
            }
        ));
        let missing = TestCli::try_parse_from(["layout", "1", "swap", "1", "2"])?;
        assert!(missing.layout.request().is_err());
        let resize = TestCli::try_parse_from([
            "layout",
            "1",
            "--generation",
            "7",
            "--instance",
            "test-instance",
            "resize",
            "2",
            "left",
            "-500",
        ])?;
        assert!(matches!(
            resize.layout.request()?,
            Request::Layout {
                action: LayoutAction::ResizeToward {
                    delta: -500,
                    direction: Direction::Left,
                    ..
                },
                generation: Some(7),
                ..
            }
        ));
        assert!(TestCli::try_parse_from(["layout", "1", "move", "1", "2", "diagonal"]).is_err());
        Ok(())
    }
}