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, BorderType, 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    pub border_type: BorderType,
16}
17
18impl<'a> Widget for ReplaceInput<'a> {
19    fn render(self, area: Rect, buf: &mut Buffer) {
20        let border_style = if self.focused {
21            Style::default().fg(theme::BLUE)
22        } else {
23            Style::default().fg(theme::OVERLAY)
24        };
25
26        let block = Block::default()
27            .borders(Borders::ALL)
28            .border_type(self.border_type)
29            .border_style(border_style)
30            .title(Span::styled(
31                " Replacement ($1, ${name}) ",
32                Style::default().fg(theme::TEXT),
33            ));
34
35        let content = self.editor.content();
36        let line = Line::from(Span::styled(
37            content.to_string(),
38            Style::default().fg(theme::TEXT),
39        ));
40
41        let paragraph = Paragraph::new(line)
42            .block(block)
43            .style(Style::default().bg(theme::BASE));
44
45        paragraph.render(area, buf);
46
47        // Render cursor
48        if self.focused {
49            let cursor_x = area.x + 1 + self.editor.visual_cursor() as u16;
50            let cursor_y = area.y + 1;
51            if cursor_x < area.x + area.width.saturating_sub(1)
52                && cursor_y < area.y + area.height.saturating_sub(1)
53            {
54                if let Some(cell) = buf.cell_mut((cursor_x, cursor_y)) {
55                    cell.set_style(
56                        Style::default()
57                            .fg(theme::BASE)
58                            .bg(theme::TEXT)
59                            .add_modifier(Modifier::BOLD),
60                    );
61                }
62            }
63        }
64    }
65}