matchmaker/ui/
display.rs

1#![allow(unused)]
2use log::debug;
3use ratatui::{
4    style::{Style, Stylize}, text::Text, widgets::{Paragraph, Wrap}
5};
6
7use crate::{config::DisplayConfig, utils::{serde::StringOrVec, text::left_pad}};
8
9#[derive(Debug, Clone)]
10pub struct DisplayUI {
11    height: u16,
12    text: Text<'static>,
13    pub show: bool,
14    pub config: DisplayConfig,
15}
16
17impl DisplayUI {
18    pub fn new(config: DisplayConfig) -> Self {
19        let text = match &config.content {
20            Some(StringOrVec::String(s)) => Text::from(s.clone()),
21            // todo
22            _ => Text::from(String::new()),
23        };
24        let height = text.lines.len() as u16;
25
26        Self {
27            height,
28            show: config.content.is_some(),
29            text,
30            config,
31        }
32    }
33
34    pub fn height(&self) -> u16 {
35        if !self.show {
36            return 0;
37        }
38        let mut height = self.height;
39        height += self.config.border.height();
40
41        height
42    }
43
44    /// Set text and visibility.
45    pub fn set(&mut self, text: impl Into<Text<'static>>) {
46        let text = text.into();
47        self.height = text.lines.len() as u16;
48        self.text = text;
49        self.show = true;
50    }
51
52    pub fn make_display(&self, result_indentation: usize) -> Paragraph<'_> {
53        // debug!("{result_indentation}, {}, {text}", self.config.match_indent);
54
55        let mut ret = Paragraph::new(self.text.clone())
56        .style(Style::default().fg(self.config.fg))
57        .add_modifier(self.config.modifier);
58
59        if self.config.wrap {
60            ret = ret.wrap(Wrap { trim: false });
61        }
62
63        let block = {
64            let ret = self.config.border.as_block();
65            if self.config.match_indent {
66                let mut padding = self.config.border.padding;
67                padding.left += result_indentation as u16;
68                ret.padding(padding)
69            } else {
70                ret
71            }
72        };
73
74
75        ret = ret.block(block);
76
77        ret
78    }
79}