summaryrefslogtreecommitdiffstats
path: root/zellij-server/src/pty_writer.rs
blob: f0aeaf42b499ecf5c1a5638387526fdb73457a61 (plain)
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
use zellij_utils::errors::{ContextType, PtyWriteContext};

use crate::thread_bus::Bus;

#[derive(Debug, Clone)]
pub(crate) enum PtyWriteInstruction {
    Write(Vec<u8>, i32),
    Exit,
}

impl From<&PtyWriteInstruction> for PtyWriteContext {
    fn from(tty_write_instruction: &PtyWriteInstruction) -> Self {
        match *tty_write_instruction {
            PtyWriteInstruction::Write(..) => PtyWriteContext::Write,
            PtyWriteInstruction::Exit => PtyWriteContext::Exit,
        }
    }
}

pub(crate) fn pty_writer_main(bus: Bus<PtyWriteInstruction>) {
    loop {
        let (event, mut err_ctx) = bus.recv().expect("failed to receive event on channel");
        err_ctx.add_call(ContextType::PtyWrite((&event).into()));
        let os_input = bus.os_input.clone().unwrap();
        match event {
            PtyWriteInstruction::Write(bytes, terminal_id) => {
                if let Err(e) = os_input.write_to_tty_stdin(terminal_id, &bytes) {
                    log::error!("failed to write to terminal: {}", e);
                }
                if let Err(e) = os_input.tcdrain(terminal_id) {
                    log::error!("failed to drain terminal: {}", e);
                };
            },
            PtyWriteInstruction::Exit => {
                break;
            },
        }
    }
}