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())
80 .style(Style::default().fg(self.config.fg))
81 .add_modifier(self.config.modifier);
82
83 if self.config.wrap {
84 ret = ret.wrap(Wrap { trim: false });
85 }
86
87 let block = {
88 let ret = self.config.border.as_block();
89 if self.config.match_indent {
90 let mut padding = self.config.border.padding;
91 padding.left += result_indentation as u16;
92 ret.padding(padding)
93 } else {
94 ret
95 }
96 };
97
98 ret = ret.block(block);
99
100 ret
101 }
102}