aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-server/src/ui/overlay/prompt.rs
blob: f28e3c405b299cc3f2ef9697a0990baa4f358c24 (plain) (blame)
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
use zellij_utils::pane_size::Size;

use super::{Overlay, OverlayType, Overlayable};
use crate::{ClientId, ServerInstruction};

#[derive(Clone, Debug)]
pub struct Prompt {
    pub message: String,
    on_confirm: Option<Box<ServerInstruction>>,
    on_deny: Option<Box<ServerInstruction>>,
}

impl Prompt {
    pub fn new(
        message: String,
        on_confirm: Option<Box<ServerInstruction>>,
        on_deny: Option<Box<ServerInstruction>>,
    ) -> Self {
        Self {
            message,
            on_confirm,
            on_deny,
        }
    }
    pub fn confirm(self) -> Option<Box<ServerInstruction>> {
        self.on_confirm
    }
    pub fn deny(self) -> Option<Box<ServerInstruction>> {
        self.on_deny
    }
}

impl Overlayable for Prompt {
    fn generate_overlay(&self, size: Size) -> String {
        let mut output = String::new();
        let rows = size.rows;
        let mut vte_output = self.message.clone();
        Overlay::pad_cols(&mut vte_output, size.cols);
        for (x, h) in vte_output.chars().enumerate() {
            output.push_str(&format!("\u{1b}[{};{}H\u{1b}[48;5;238m{}", rows, x + 1, h,));
        }
        output
    }
}

pub fn _generate_quit_prompt(client_id: ClientId) -> Overlay {
    let prompt = Prompt::new(
        (" Do you want to quit zellij? [Y]es / [N]o").to_string(),
        Some(Box::new(ServerInstruction::ClientExit(client_id))),
        None,
    );
    Overlay {
        overlay_type: OverlayType::Prompt(prompt),
    }
}