zellij/zellij-server/src/ui/overlay/prompt.rs
哇呜哇呜呀咦耶 c5b1eb0a9e
improve error handling in ui module (#1870)
* improve error handling in ui module

* resolve problems in the review
2022-10-28 14:15:16 +00:00

65 lines
1.7 KiB
Rust

use zellij_utils::pane_size::Size;
use super::{Overlay, OverlayType, Overlayable};
use crate::{ClientId, ServerInstruction};
use zellij_utils::errors::prelude::*;
use std::fmt::Write;
#[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) -> Result<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() {
write!(
&mut output,
"\u{1b}[{};{}H\u{1b}[48;5;238m{}",
rows,
x + 1,
h,
)
.context("failed to generate VTE output from prompt")?;
}
Ok(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),
}
}