aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/search_popup.rs
blob: 0f47b10210aabdd5822adf2c222d4890dc76a3c5 (plain) (blame)
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
84
85
86
87
88
89
90
use ratatui::{
    Frame,
    layout::Rect,
    style::{Modifier, Style},
    widgets::{Block, Borders, Clear},
};
use ratatui_textarea::TextArea;

use crate::{gentoo::package::PackageKey, theme::Theme, widgets::helpers::search_popup_rect};

#[derive(Debug)]
pub struct SearchPopup {
    pub(crate) visible: bool,
    pub(crate) textarea: TextArea<'static>,
    pub(crate) result: Option<usize>,
}

impl Default for SearchPopup {
    fn default() -> Self {
        let mut search_popup = Self {
            visible: bool::default(),
            textarea: TextArea::default(),
            result: Option::default(),
        };

        search_popup.textarea.set_style(Theme::muted());
        search_popup
            .textarea
            .set_cursor_line_style(Style::default());

        search_popup
    }
}

impl SearchPopup {
    pub fn toggle(&mut self) {
        self.visible = !self.visible;
        self.textarea.clear();
    }

    pub fn search(&mut self, packages: Vec<&PackageKey>) -> Option<usize> {
        let indexes: Vec<usize> = packages
            .iter()
            .enumerate()
            .filter(|(_, s)| {
                s.qualified_name()
                    .to_lowercase()
                    .contains(&self.textarea.lines()[0].to_lowercase())
            })
            .map(|(i, _)| i)
            .collect();

        if indexes.is_empty() {
            self.result = None;
            None
        } else {
            self.result = Some(indexes[0]);
            Some(indexes[0])
        }
    }

    pub fn draw(&mut self, frame: &mut Frame, area: Rect) {
        if self.visible {
            let mut popup_block = Block::default()
                .title("Search")
                .borders(Borders::ALL)
                .border_type(ratatui::widgets::BorderType::Rounded)
                .border_style(Theme::block())
                .style(Style::default().bg(Theme::BG));

            if self.result.is_some() {
                popup_block = popup_block.border_style(Theme::success());
            } else {
                popup_block = popup_block.border_style(Theme::error());
            }

            self.textarea.set_block(popup_block);

            let area = search_popup_rect(70, area);

            frame.render_widget(
                Block::default().style(Style::default().bg(Theme::BG).add_modifier(Modifier::DIM)),
                frame.area(),
            );
            frame.render_widget(Clear, area);
            frame.render_widget(Block::default().style(Style::default().bg(Theme::BG)), area);
            frame.render_widget(&self.textarea, area);
        }
    }
}