mprocs 0.9.3

TUI for running multiple processes
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
use std::collections::HashMap;

use crate::{
  console::views::pane::Pane,
  term::{Size, grid::Rect},
};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct PaneId(u32);

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Axis {
  Horizontal,
  Vertical,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Dir {
  Up,
  Down,
  Left,
  Right,
}

impl Dir {
  fn axis(self) -> Axis {
    match self {
      Dir::Left | Dir::Right => Axis::Horizontal,
      Dir::Up | Dir::Down => Axis::Vertical,
    }
  }

  fn after(self) -> bool {
    matches!(self, Dir::Right | Dir::Down)
  }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SizeSpec {
  /// Fixed extent in cells along the parent's axis.
  Fixed(u16),
  /// Take an even share of whatever's left after fixed-size siblings.
  Fill,
}

struct Node {
  parent: Option<PaneId>,
  /// Size along the parent's axis.
  size: SizeSpec,
  area: Rect,
  kind: NodeKind,
}

enum NodeKind {
  Container { axis: Axis, children: Vec<PaneId> },
  Leaf { pane: Box<dyn Pane> },
}

pub struct Layout {
  size: Size,
  nodes: HashMap<PaneId, Node>,
  next_id: u32,
  root: PaneId,
  dirty: bool,
}

impl Layout {
  pub fn new(size: Size) -> Self {
    let root = PaneId(0);
    let mut nodes = HashMap::new();
    nodes.insert(
      root,
      Node {
        parent: None,
        size: SizeSpec::Fill,
        area: Rect::new(0, 0, size.width, size.height),
        kind: NodeKind::Container {
          axis: Axis::Horizontal,
          children: Vec::new(),
        },
      },
    );
    Self {
      size,
      nodes,
      next_id: 1,
      root,
      dirty: true,
    }
  }

  pub fn resize(&mut self, size: Size) {
    if self.size != size {
      self.size = size;
      self.dirty = true;
    }
  }

  pub fn root(&self) -> PaneId {
    self.root
  }

  /// Insert a new pane next to `anchor` in `dir` direction. If `anchor` is a
  /// container, descends to its first or last child (per `dir`) and splits
  /// there; an empty container gets the new pane as its first child. Pass
  /// [`Layout::root`] to add into the root.
  pub fn insert(
    &mut self,
    anchor: PaneId,
    dir: Dir,
    pane: Box<dyn Pane>,
    size: SizeSpec,
  ) -> PaneId {
    let mut cur = anchor;
    loop {
      match &self.nodes[&cur].kind {
        NodeKind::Leaf { .. } => return self.split(cur, pane, size, dir),
        NodeKind::Container { children, .. } => {
          if children.is_empty() {
            return self.append_child(cur, pane, size);
          }
          cur = if dir.after() {
            *children.last().unwrap()
          } else {
            *children.first().unwrap()
          };
        }
      }
    }
  }

  fn append_child(
    &mut self,
    container: PaneId,
    pane: Box<dyn Pane>,
    size: SizeSpec,
  ) -> PaneId {
    let id = self.fresh_id();
    self.nodes.insert(
      id,
      Node {
        parent: Some(container),
        size,
        area: Rect::default(),
        kind: NodeKind::Leaf { pane },
      },
    );
    let parent = self.nodes.get_mut(&container).expect("container missing");
    let NodeKind::Container { children, .. } = &mut parent.kind else {
      panic!("not a container");
    };
    children.push(id);
    self.dirty = true;
    id
  }

  /// Re-layout if dirty and return the rendered geometry of every leaf in
  /// tree order. Use [`Layout::pane_mut`] to access the pane object for each
  /// id while iterating.
  pub fn render(&mut self) -> Vec<(PaneId, Rect)> {
    if self.dirty {
      self.relayout();
    }
    let mut out = Vec::new();
    self.collect_leaves(self.root, &mut out);
    out
  }

  pub fn area(&mut self, id: PaneId) -> Option<Rect> {
    if self.dirty {
      self.relayout();
    }
    self.nodes.get(&id).map(|n| n.area)
  }

  pub fn pane_mut(&mut self, id: PaneId) -> &mut dyn Pane {
    match &mut self.nodes.get_mut(&id).expect("no such pane").kind {
      NodeKind::Leaf { pane } => pane.as_mut(),
      NodeKind::Container { .. } => panic!("not a leaf"),
    }
  }

  /// Find the closest leaf neighbor of `from` in the given direction. Wraps
  /// around at the layout edges. Returns `None` only if no ancestor of `from`
  /// has the matching axis (e.g. moving vertically in a purely horizontal
  /// layout).
  pub fn neighbor(&self, from: PaneId, dir: Dir) -> Option<PaneId> {
    let target_axis = dir.axis();
    let after = dir.after();

    let mut current = from;
    let mut topmost_match: Option<PaneId> = None;
    loop {
      let Some(parent_id) = self.nodes.get(&current)?.parent else {
        break;
      };
      let parent = &self.nodes[&parent_id];
      if let NodeKind::Container { axis, children } = &parent.kind {
        if *axis == target_axis {
          topmost_match = Some(parent_id);
          let idx = children.iter().position(|&c| c == current).unwrap();
          let sibling_idx = if after {
            (idx + 1 < children.len()).then(|| idx + 1)
          } else {
            idx.checked_sub(1)
          };
          if let Some(si) = sibling_idx {
            return Some(self.descend_to_leaf(children[si], after));
          }
        }
      }
      current = parent_id;
    }

    // No sibling found anywhere up the chain. Wrap around within the
    // outermost matching-axis container we walked through.
    let top = topmost_match?;
    let NodeKind::Container { children, .. } = &self.nodes[&top].kind else {
      return None;
    };
    let wrap_idx = if after { 0 } else { children.len() - 1 };
    Some(self.descend_to_leaf(children[wrap_idx], after))
  }

  /// Descend a subtree to a leaf. `prefer_first` picks the first child of
  /// each container (use when entering a subtree from its left/top); `false`
  /// picks the last child (entering from right/bottom).
  fn descend_to_leaf(&self, node: PaneId, prefer_first: bool) -> PaneId {
    match &self.nodes[&node].kind {
      NodeKind::Leaf { .. } => node,
      NodeKind::Container { children, .. } => {
        let pick = if prefer_first {
          *children.first().expect("empty container")
        } else {
          *children.last().expect("empty container")
        };
        self.descend_to_leaf(pick, prefer_first)
      }
    }
  }

  fn split(
    &mut self,
    anchor: PaneId,
    pane: Box<dyn Pane>,
    size: SizeSpec,
    dir: Dir,
  ) -> PaneId {
    let target_axis = dir.axis();
    let after = dir.after();

    let anchor_parent = self
      .nodes
      .get(&anchor)
      .expect("no such anchor")
      .parent
      .expect("can't split root");
    let parent_axis = match &self.nodes[&anchor_parent].kind {
      NodeKind::Container { axis, .. } => *axis,
      NodeKind::Leaf { .. } => panic!("anchor's parent is not a container"),
    };

    let new_id = self.fresh_id();

    if parent_axis == target_axis {
      // Same axis as parent: just add a sibling next to anchor.
      self.nodes.insert(
        new_id,
        Node {
          parent: Some(anchor_parent),
          size,
          area: Rect::default(),
          kind: NodeKind::Leaf { pane },
        },
      );
      let parent = self.nodes.get_mut(&anchor_parent).unwrap();
      if let NodeKind::Container { children, .. } = &mut parent.kind {
        let idx = children.iter().position(|&c| c == anchor).unwrap();
        let at = if after { idx + 1 } else { idx };
        children.insert(at, new_id);
      }
    } else {
      // Cross-axis: wrap anchor in a new container that takes its place.
      let container_id = self.fresh_id();
      let anchor_size = self.nodes[&anchor].size;

      self.nodes.insert(
        new_id,
        Node {
          parent: Some(container_id),
          size,
          area: Rect::default(),
          kind: NodeKind::Leaf { pane },
        },
      );

      let container_children = if after {
        vec![anchor, new_id]
      } else {
        vec![new_id, anchor]
      };
      self.nodes.insert(
        container_id,
        Node {
          parent: Some(anchor_parent),
          size: anchor_size,
          area: Rect::default(),
          kind: NodeKind::Container {
            axis: target_axis,
            children: container_children,
          },
        },
      );

      let anchor_node = self.nodes.get_mut(&anchor).unwrap();
      anchor_node.parent = Some(container_id);
      anchor_node.size = SizeSpec::Fill;

      let parent = self.nodes.get_mut(&anchor_parent).unwrap();
      if let NodeKind::Container { children, .. } = &mut parent.kind {
        let idx = children.iter().position(|&c| c == anchor).unwrap();
        children[idx] = container_id;
      }
    }

    self.dirty = true;
    new_id
  }

  fn relayout(&mut self) {
    self.dirty = false;
    let area = Rect::new(0, 0, self.size.width, self.size.height);
    self.layout_node(self.root, area);
  }

  fn layout_node(&mut self, id: PaneId, area: Rect) {
    self.nodes.get_mut(&id).unwrap().area = area;

    let (axis, children) = match &self.nodes[&id].kind {
      NodeKind::Container { axis, children } => (*axis, children.clone()),
      NodeKind::Leaf { .. } => return,
    };

    let extent = match axis {
      Axis::Horizontal => area.width,
      Axis::Vertical => area.height,
    };

    let mut total_fixed = 0u16;
    let mut fill_count = 0u16;
    for c in &children {
      match self.nodes[c].size {
        SizeSpec::Fixed(n) => total_fixed = total_fixed.saturating_add(n),
        SizeSpec::Fill => fill_count += 1,
      }
    }
    let remaining = extent.saturating_sub(total_fixed);
    let (fill_each, fill_extra) = if fill_count > 0 {
      (remaining / fill_count, remaining % fill_count)
    } else {
      (0, 0)
    };

    let mut offset = 0u16;
    let mut fill_seen = 0u16;
    for c in &children {
      let len = match self.nodes[c].size {
        SizeSpec::Fixed(n) => n,
        SizeSpec::Fill => {
          let extra = if fill_seen < fill_extra { 1 } else { 0 };
          fill_seen += 1;
          fill_each + extra
        }
      };
      let child_area = match axis {
        Axis::Horizontal => {
          Rect::new(area.x + offset, area.y, len, area.height)
        }
        Axis::Vertical => Rect::new(area.x, area.y + offset, area.width, len),
      };
      self.layout_node(*c, child_area);
      offset = offset.saturating_add(len);
    }
  }

  fn collect_leaves(&self, id: PaneId, out: &mut Vec<(PaneId, Rect)>) {
    let node = &self.nodes[&id];
    match &node.kind {
      NodeKind::Leaf { .. } => out.push((id, node.area)),
      NodeKind::Container { children, .. } => {
        for c in children {
          self.collect_leaves(*c, out);
        }
      }
    }
  }

  fn fresh_id(&mut self) -> PaneId {
    let id = PaneId(self.next_id);
    self.next_id += 1;
    id
  }
}