Skip to main content

rgx/ui/
replace_input.rs

1use ratatui::{
2    buffer::Buffer,
3    layout::Rect,
4    style::{Modifier, Style},
5    text::{Line, Span},
6    widgets::{Block, Borders, Paragraph, Widget},
7};
8
9use crate::input::editor::Editor;
10use crate::ui::theme;
11
12pub struct ReplaceInput<'a> {
13    pub editor: &'a Editor,
14    pub focused: bool,
15}
16
17impl<'a> Widget for ReplaceInput<'a> {
18    fn render(self, area: Rect, buf: &mut Buffer) {
19        let border_style = if self.focused {
20            Style::default().fg(theme::BLUE)
21        } else {
22            Style::default().fg(theme::OVERLAY)
23        };
24
25        let block = Block::default()
26            .borders(Borders::ALL)
27            .border_style(border_style)
28            .title(Span::styled(
29                " Replacement ($1, ${name}) ",
30                Style::default().fg(theme::TEXT),
31            ));
32
33        let content = self.editor.content();
34        let line = Line::from(Span::styled(
35            content.to_string(),
36            Style::default().fg(theme::TEXT),
37        ));
38
39        let paragraph = Paragraph::new(line)
40            .block(block)
41            .style(Style::default().bg(theme::BASE));
42
43        paragraph.render(area, buf);
44
45        // Render cursor
46        if self.focused {
47            let cursor_x = area.x + 1 + self.editor.visual_cursor() as u16;
48            let cursor_y = area.y + 1;
49            if cursor_x < area.x + area.width.saturating_sub(1)
50                && cursor_y < area.y + area.height.saturating_sub(1)
51            {
52                if let Some(cell) = buf.cell_mut((cursor_x, cursor_y)) {
53                    cell.set_style(
54                        Style::default()
55                            .fg(theme::BASE)
56                            .bg(theme::TEXT)
57                            .add_modifier(Modifier::BOLD),
58                    );
59                }
60            }
61        }
62    }
63}