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