mprocs 0.9.2

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
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
use std::fmt::Debug;
use std::future::pending;

use assert_matches::assert_matches;
use tokio::io::AsyncWriteExt;
use tokio::select;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};

use crate::config::ProcConfig;
use crate::encode_term::{encode_key, encode_mouse_event, KeyCodeEncodeModes};
use crate::error::ResultLogger;
use crate::kernel::kernel_message::{KernelCommand, ProcContext};
use crate::kernel::proc::{ProcId, ProcInit, ProcStatus};
use crate::key::Key;
use crate::mouse::{MouseEvent, MouseEventKind};
use crate::proc_log_config::LogConfig;
use crate::process::process::Process as _;
use crate::process::process_spec::ProcessSpec;
use crate::vt100::grid::Rect;
use crate::vt100::{self, VtEvent};

use super::inst::Inst;
use super::msg::{ProcCmd, ProcEvent};
use super::view::ProcView;
use super::Size;
use super::StopSignal;

pub struct Proc {
  pub id: ProcId,
  pub spec: ProcessSpec,
  size: Size,

  name: String,
  stop_signal: StopSignal,
  scrollback_len: usize,
  log: Option<LogConfig>,

  pub tx: UnboundedSender<ProcEvent>,

  pub inst: ProcState,
}

#[derive(Debug)]
pub enum ProcState {
  None,
  Some(Inst),
}

pub fn launch_proc(
  parent_ks: &ProcContext,
  cfg: ProcConfig,
  proc_id: ProcId,
  deps: Vec<ProcId>,
  size: Rect,
) -> ProcView {
  let cfg_ = cfg.clone();
  let child_id = parent_ks.add_proc_with_id(
    proc_id,
    Box::new(move |ks| {
      let (cmd_sender, cmd_receiver) = tokio::sync::mpsc::unbounded_channel();

      let cfg = cfg_;
      tokio::spawn(async move {
        let proc_id = ks.proc_id;
        proc_main_loop(ks, proc_id, &cfg, size, cmd_receiver).await;
      });

      ProcInit {
        sender: cmd_sender,
        stop_on_quit: true,
        status: ProcStatus::Down,
        deps,
      }
    }),
  );

  ProcView::new(child_id, cfg)
}

async fn proc_main_loop(
  ks: ProcContext,
  proc_id: ProcId,
  cfg: &ProcConfig,
  size: Rect,
  mut cmd_receiver: UnboundedReceiver<ProcCmd>,
) -> ProcView {
  let (internal_sender, mut internal_receiver) =
    tokio::sync::mpsc::unbounded_channel();
  let mut proc = Proc::new(proc_id, cfg, internal_sender, size).await;

  let mut vt_events_buf = Vec::new();

  loop {
    enum NextValue {
      Cmd(Option<ProcCmd>),
      Internal(Option<ProcEvent>),
      Read(std::io::Result<usize>),
    }
    let mut read_buf = [0u8; 8 * 1024];
    let value = select! {
      cmd = cmd_receiver.recv() => NextValue::Cmd(cmd),
      event = internal_receiver.recv() => NextValue::Internal(event),
      count = proc.read(&mut read_buf) => NextValue::Read(count),
    };
    match value {
      NextValue::Cmd(Some(cmd)) => {
        let mut rendered = false;
        proc.handle_cmd(cmd, &mut rendered).await;
        if rendered {
          ks.send(KernelCommand::ProcRendered);
        }
      }
      NextValue::Cmd(None) => (),
      NextValue::Internal(Some(proc_event)) => match proc_event {
        ProcEvent::Exited(exit_code) => {
          proc.handle_exited(exit_code);
          if !proc.is_up() {
            ks.send(KernelCommand::ProcStopped(exit_code));
          }
        }
        ProcEvent::Started => {
          ks.send(KernelCommand::ProcStarted);
        }
        ProcEvent::SetVt(vt) => {
          ks.send(KernelCommand::ProcUpdatedScreen(vt));
        }
      },
      NextValue::Internal(None) => (),
      NextValue::Read(Ok(count)) => {
        let inst = match &mut proc.inst {
          ProcState::Some(inst) => inst,
          ProcState::None => {
            log::error!("Expected proc.inst to be Some after a read.");
            continue;
          }
        };
        if count == 0 {
          inst.stdout_eof = true;
          if !proc.is_up() {
            ks.send(KernelCommand::ProcStopped(
              proc.exit_code().unwrap_or(199),
            ));
          }
        } else {
          let bytes = &read_buf[..count];

          // Write to log file if configured
          if let Some(ref mut writer) = inst.log_writer {
            writer.write_all(bytes).await.log_ignore();
            writer.flush().await.log_ignore();
          }

          if let Ok(mut vt) = inst.vt.write() {
            vt.screen.process(bytes, &mut vt_events_buf);
            drop(vt);
          }
          for vt_event in vt_events_buf.drain(..) {
            match vt_event {
              VtEvent::Bell => {
                //
              }
              VtEvent::Reply(s) => {
                inst.process.write_all(s.as_bytes()).await.log_ignore();
              }
            };
          }
          ks.send(KernelCommand::ProcRendered);
        }
      }
      NextValue::Read(Err(e)) => {
        log::error!("Process read() error: {}", e);
        match &mut proc.inst {
          ProcState::Some(inst) => {
            inst.stdout_eof = true;
            if !proc.is_up() {
              ks.send(KernelCommand::ProcStopped(
                proc.exit_code().unwrap_or(198),
              ));
            }
          }
          ProcState::None => {}
        };
      }
    }
  }
}

impl Proc {
  pub async fn new(
    id: ProcId,
    cfg: &ProcConfig,
    tx: UnboundedSender<ProcEvent>,
    area: Rect,
  ) -> Self {
    let size = Size {
      width: area.width,
      height: area.height,
    };
    let mut proc = Proc {
      id,
      spec: cfg.into(),
      size,

      name: cfg.name.clone(),
      stop_signal: cfg.stop.clone(),
      scrollback_len: cfg.scrollback_len,
      log: cfg.log.clone(),

      tx,

      inst: ProcState::None,
    };

    if cfg.autostart {
      proc.spawn_new_inst().await;
    }

    proc
  }

  async fn spawn_new_inst(&mut self) {
    assert_matches!(self.inst, ProcState::None);

    let spawned = Inst::spawn(
      self.id,
      &self.name,
      &self.spec,
      self.tx.clone(),
      &self.size,
      self.scrollback_len,
      self.log.as_ref(),
    )
    .await;
    let inst = match spawned {
      Ok(inst) => ProcState::Some(inst),
      Err(err) => {
        log::error!("Process spawn error: {}", err);
        ProcState::None
      }
    };
    self.inst = inst;
  }

  pub async fn start(&mut self) {
    if !self.is_up() {
      self.inst = ProcState::None;
      self.spawn_new_inst().await;
    }
  }

  pub fn handle_exited(&mut self, exit_code: u32) {
    match &mut self.inst {
      ProcState::None => (),
      ProcState::Some(inst) => {
        inst.exit_code = Some(exit_code);
        inst.process.on_exited();
      }
    }
  }

  pub fn is_up(&self) -> bool {
    if let ProcState::Some(inst) = &self.inst {
      inst.exit_code.is_none() || !inst.stdout_eof
    } else {
      false
    }
  }

  pub fn exit_code(&self) -> Option<u32> {
    match &self.inst {
      ProcState::Some(inst) => inst.exit_code,
      ProcState::None => None,
    }
  }

  pub fn lock_vt(
    &self,
  ) -> Option<std::sync::RwLockReadGuard<'_, vt100::Parser>> {
    match &self.inst {
      ProcState::None => None,
      ProcState::Some(inst) => inst.vt.read().ok(),
    }
  }

  pub fn lock_vt_mut(
    &mut self,
  ) -> Option<std::sync::RwLockWriteGuard<'_, vt100::Parser>> {
    match &self.inst {
      ProcState::None => None,
      ProcState::Some(inst) => inst.vt.write().ok(),
    }
  }

  pub async fn kill(&mut self) {
    if self.is_up() {
      if let ProcState::Some(inst) = &mut self.inst {
        inst.process.kill().await.log_ignore();
      }
    }
  }

  #[cfg(not(windows))]
  pub async fn stop(&mut self) {
    match self.stop_signal.clone() {
      StopSignal::SIGINT => self.send_signal(libc::SIGINT),
      StopSignal::SIGTERM => self.send_signal(libc::SIGTERM),
      StopSignal::SIGKILL => self.send_signal(libc::SIGKILL),
      StopSignal::SendKeys(keys) => {
        for key in keys {
          self.send_key(&key).await;
        }
      }
      StopSignal::HardKill => self.kill().await,
    }
  }

  #[cfg(windows)]
  pub async fn stop(&mut self) {
    match self.stop_signal.clone() {
      StopSignal::SIGINT => log::warn!("SIGINT signal is ignored on Windows"),
      StopSignal::SIGTERM => self.kill().await,
      StopSignal::SIGKILL => self.kill().await,
      StopSignal::SendKeys(keys) => {
        for key in keys {
          self.send_key(&key).await;
        }
      }
      StopSignal::HardKill => self.kill().await,
    }
  }

  #[cfg(not(windows))]
  fn send_signal(&mut self, sig: libc::c_int) {
    if let ProcState::Some(inst) = &self.inst {
      unsafe { libc::kill(inst.pid as i32, sig) };
    }
  }

  pub fn resize(&mut self, size: Size) {
    if let ProcState::Some(inst) = &mut self.inst {
      inst.resize(&size);
    }
    self.size = size;
  }

  pub async fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
    if let ProcState::Some(inst) = &mut self.inst {
      if !inst.stdout_eof {
        return inst.process.read(buf).await;
      }
    }
    pending().await
  }

  pub async fn send_key(&mut self, key: &Key) {
    if self.is_up() {
      let application_cursor_keys = self
        .lock_vt()
        .is_some_and(|vt| vt.screen().application_cursor());
      let encoder = encode_key(
        key,
        KeyCodeEncodeModes {
          enable_csi_u_key_encoding: true,
          application_cursor_keys,
          newline_mode: false,
        },
      );
      match encoder {
        Ok(encoder) => {
          self.write_all(encoder.as_bytes()).await;
        }
        Err(_) => {
          log::warn!("Failed to encode key: {}", key.to_string());
        }
      }
    }
  }

  pub async fn write_all(&mut self, bytes: &[u8]) {
    if self.is_up() {
      if let Some(mut vt) = self.lock_vt_mut() {
        if vt.screen().scrollback() > 0 {
          vt.set_scrollback(0);
        }
      }
      if let ProcState::Some(inst) = &mut self.inst {
        inst.process.write_all(bytes).await.log_ignore();
      }
    }
  }

  pub fn scroll_up_lines(&mut self, n: usize) {
    if let Some(mut vt) = self.lock_vt_mut() {
      vt.screen.scroll_screen_up(n);
    }
  }

  pub fn scroll_down_lines(&mut self, n: usize) {
    if let Some(mut vt) = self.lock_vt_mut() {
      vt.screen.scroll_screen_down(n);
    }
  }

  pub fn scroll_half_screen_up(&mut self) {
    self.scroll_up_lines(self.size.height as usize / 2);
  }

  pub fn scroll_half_screen_down(&mut self) {
    self.scroll_down_lines(self.size.height as usize / 2);
  }

  pub async fn handle_mouse(&mut self, event: MouseEvent) {
    if let ProcState::Some(inst) = &mut self.inst {
      let mouse_mode = inst.vt.read().unwrap().screen().mouse_protocol_mode();
      let seq = match mouse_mode {
        vt100::MouseProtocolMode::None => String::new(),
        vt100::MouseProtocolMode::Press => match event.kind {
          MouseEventKind::Down(_)
          | MouseEventKind::ScrollDown
          | MouseEventKind::ScrollUp
          | MouseEventKind::ScrollLeft
          | MouseEventKind::ScrollRight => encode_mouse_event(event),
          _ => String::new(),
        },
        vt100::MouseProtocolMode::PressRelease => match event.kind {
          MouseEventKind::Down(_)
          | MouseEventKind::Up(_)
          | MouseEventKind::ScrollDown
          | MouseEventKind::ScrollUp
          | MouseEventKind::ScrollLeft
          | MouseEventKind::ScrollRight => encode_mouse_event(event),
          MouseEventKind::Drag(_) | MouseEventKind::Moved => String::new(),
        },
        vt100::MouseProtocolMode::ButtonMotion => match event.kind {
          MouseEventKind::Down(_)
          | MouseEventKind::Up(_)
          | MouseEventKind::ScrollDown
          | MouseEventKind::Drag(_)
          | MouseEventKind::ScrollUp
          | MouseEventKind::ScrollLeft
          | MouseEventKind::ScrollRight => encode_mouse_event(event),
          MouseEventKind::Moved => String::new(),
        },
        vt100::MouseProtocolMode::AnyMotion => encode_mouse_event(event),
      };
      let _r = inst.process.write_all(seq.as_bytes()).await;
    }
  }
}

impl Proc {
  pub async fn handle_cmd(&mut self, cmd: ProcCmd, rendered: &mut bool) {
    match cmd {
      ProcCmd::Start => {
        self.start().await;
        *rendered = true;
      }
      ProcCmd::Stop => self.stop().await,
      ProcCmd::Kill => self.kill().await,

      ProcCmd::SendKey(key) => self.send_key(&key).await,
      ProcCmd::SendMouse(event) => self.handle_mouse(event).await,

      ProcCmd::ScrollUp => {
        self.scroll_half_screen_up();
        *rendered = true;
      }
      ProcCmd::ScrollDown => {
        self.scroll_half_screen_down();
        *rendered = true;
      }
      ProcCmd::ScrollUpLines { n } => {
        self.scroll_up_lines(n);
        *rendered = true;
      }
      ProcCmd::ScrollDownLines { n } => {
        self.scroll_down_lines(n);
        *rendered = true;
      }

      ProcCmd::Resize { w, h } => {
        self.resize(Size {
          width: w,
          height: h,
        });
        *rendered = true;
      }

      ProcCmd::Custom(custom) => {
        log::error!("Proc received unknown custom command: {:?}", custom);
      }

      ProcCmd::OnProcUpdate(_, _) => {
        log::warn!("Proc received ProcCmd::OnProcUpdate.");
      }
    }
  }
}