Skip to main content

lib/mprocs/proc/
mod.rs

1mod inst;
2pub mod msg;
3pub mod proc;
4pub mod view;
5
6use std::fmt::Debug;
7
8use anyhow::bail;
9use serde::{Deserialize, Serialize};
10
11use crate::mprocs::yaml_val::Val;
12use crate::term::key::Key;
13
14#[derive(Clone, Debug, Default)]
15pub enum StopSignal {
16  SIGINT,
17  #[default]
18  SIGTERM,
19  SIGKILL,
20  SendKeys(Vec<Key>),
21  HardKill,
22  /// Run a shell command as the stop action. Useful for tools like
23  /// `podman compose` that don't reliably respond to signals but do have
24  /// an explicit teardown command (e.g. `podman compose down`). The main
25  /// process is expected to exit on its own once the stop command
26  /// completes (e.g. `compose up` exits when containers go away).
27  Cmd(String),
28}
29
30impl StopSignal {
31  pub fn from_val(val: &Val) -> anyhow::Result<Self> {
32    match val.raw() {
33      serde_yaml::Value::String(str) => match str.as_str() {
34        "SIGINT" => return Ok(Self::SIGINT),
35        "SIGTERM" => return Ok(Self::SIGTERM),
36        "SIGKILL" => return Ok(Self::SIGKILL),
37        "hard-kill" => return Ok(Self::HardKill),
38        _ => (),
39      },
40      serde_yaml::Value::Mapping(map) => {
41        if map.len() == 1 {
42          if let Some(keys) = map.get("send-keys") {
43            let keys: Vec<Key> = serde_yaml::from_value(keys.clone())?;
44            return Ok(Self::SendKeys(keys));
45          }
46          if let Some(cmd) = map.get("cmd") {
47            if let serde_yaml::Value::String(shell) = cmd {
48              return Ok(Self::Cmd(shell.clone()));
49            }
50            bail!("Expected 'cmd' to be a string");
51          }
52        }
53      }
54      _ => (),
55    }
56    bail!("Unexpected 'stop' value: {:?}.", val.raw());
57  }
58}
59
60#[derive(Clone)]
61pub struct Size {
62  width: u16,
63  height: u16,
64}
65
66#[allow(clippy::large_enum_variant)]
67pub enum CopyMode {
68  None(Option<Pos>),
69  Active(crate::term::Screen, Pos, Option<Pos>),
70}
71
72impl Default for CopyMode {
73  fn default() -> Self {
74    CopyMode::None(None)
75  }
76}
77
78#[derive(
79  Clone, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize,
80)]
81pub struct Pos {
82  pub y: i32,
83  pub x: i32,
84}
85
86impl Pos {
87  pub fn to_low_high<'a>(a: &'a Self, b: &'a Self) -> (&'a Self, &'a Self) {
88    if a.y < b.y || a.y == b.y && a.x < b.x {
89      (a, b)
90    } else {
91      (b, a)
92    }
93  }
94
95  pub fn within(start: &Self, end: &Self, target: &Self) -> bool {
96    let y = target.y;
97    let x = target.x;
98    let (low, high) = Pos::to_low_high(start, end);
99
100    if y > low.y {
101      y < high.y || y == high.y && x <= high.x
102    } else if y == low.y {
103      if y < high.y {
104        x >= low.x
105      } else if y == high.y {
106        x >= low.x && x <= high.x
107      } else {
108        false
109      }
110    } else {
111      false
112    }
113  }
114}