aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-server/src/ui/components/component_coordinates.rs
blob: b60293629abae7035e67c15a7e8cf7f0b1f8dd53 (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
use std::fmt::{self, Display, Formatter};

#[derive(Debug, Clone)]
pub struct Coordinates {
    pub x: usize,
    pub y: usize,
    pub width: Option<usize>,
    pub height: Option<usize>,
}

impl Coordinates {
    pub fn stringify_with_y_offset(&self, y_offset: usize) -> String {
        format!("\u{1b}[{};{}H", self.y + y_offset + 1, self.x + 1)
    }
}

impl Display for Coordinates {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "\u{1b}[{};{}H", self.y + 1, self.x + 1)
    }
}

pub fn is_too_wide(
    character_width: usize,
    current_width: usize,
    component_coordinates: &Option<Coordinates>,
) -> bool {
    if let Some(max_width) = component_coordinates.as_ref().and_then(|p| p.width) {
        if current_width + character_width > max_width {
            return true;
        }
    }
    false
}

pub fn is_too_high(current_height: usize, component_coordinates: &Option<Coordinates>) -> bool {
    if let Some(max_height) = component_coordinates.as_ref().and_then(|p| p.height) {
        if current_height > max_height {
            return true;
        }
    }
    false
}