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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crossterm::{
    cursor, execute,
    style::{Color, Print, PrintStyledContent, Stylize},
    terminal::{Clear, ClearType},
};
use std::io::{stdout, Write};
use textwrap::fill;

pub struct InfoBox {
    pub title: String,
    pub message: String,
    pub width: usize,
    pub padding: usize, 
    pub title_color: Color,
    pub border_color: Color,
    pub message_color: Color,
}

impl InfoBox {
    pub fn new(
        title: String,
        message: String,
        width: usize,
        padding: Option<usize>,
        title_color: Option<Color>,
        border_color: Option<Color>,
        message_color: Option<Color>,
    ) -> Self {
        Self {
            title,
            message,
            width,
            padding: padding.unwrap_or(2),
            title_color: title_color.unwrap_or(Color::White),  // Default to White if not provided
            border_color: border_color.unwrap_or(Color::Blue),
            message_color: message_color.unwrap_or(Color::Reset),
        }
    }

    pub fn render(&self) -> Result<(), Box<dyn std::error::Error>> {
        let mut stdout = stdout();
        let total_width = self.width + 2 * self.padding + 2;

        execute!(stdout, cursor::MoveToColumn(0))?;
        execute!(stdout, Clear(ClearType::UntilNewLine))?;

        execute!(
            stdout,
            PrintStyledContent(format!("{: <width$}", self.title, width = total_width as usize).with(self.title_color))
        )?;
        execute!(stdout, Print("\n"))?;

        execute!(stdout, PrintStyledContent("┌".with(self.border_color)))?;
        for _ in 0..total_width - 2 {
            execute!(stdout, PrintStyledContent("─".with(self.border_color)))?;
        }
        execute!(stdout, PrintStyledContent("┐\n".with(self.border_color)))?;

        let wrapped_message = fill(&self.message, self.width);
        for line in wrapped_message.lines() {
            execute!(stdout, PrintStyledContent("│".with(self.border_color)))?;
            for _ in 0..self.padding {
                execute!(stdout, Print(" "))?;
            }
            execute!(stdout, PrintStyledContent(line.with(self.message_color)))?; 
            for _ in 0..self.padding + (self.width - line.len()) {
                execute!(stdout, Print(" "))?;
            }
            execute!(stdout, PrintStyledContent("│\n".with(self.border_color)))?;
        }

        execute!(stdout, PrintStyledContent("└".with(self.border_color)))?;
        for _ in 0..total_width - 2 {
            execute!(stdout, PrintStyledContent("─".with(self.border_color)))?;
        }
        execute!(stdout, PrintStyledContent("┘\n".with(self.border_color)))?;

        stdout.flush()?;


        Ok(())
    }
}