aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/search_popup.rs
blob: 965e6c1fcded26757c4d6a17b221cbed74a9b5c2 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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>,

    matches: Vec<usize>,
    selected_match: Option<usize>,
}

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

            matches: vec![],
            selected_match: None,
        };

        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.matches = vec![];
            self.selected_match = None;
            None
        } else {
            self.matches = indexes;
            self.selected_match = Some(0);
            return Some(self.matches[0]);
        }
    }

    pub fn current_match(&self) -> Option<usize> {
        if let Some(index) = self.selected_match {
            self.matches.get(index).copied()
        } else {
            None
        }
    }

    pub fn next_match(&mut self) -> Option<usize> {
        if let Some(index) = self.selected_match {
            self.selected_match = Some((index + 1) % self.matches.len());
        }

        self.current_match()
    }

    pub fn previous_match(&mut self) -> Option<usize> {
        if let Some(index) = self.selected_match {
            self.selected_match = Some((index + self.matches.len() - 1) % self.matches.len());
        }

        self.current_match()
    }

    pub fn match_status(&self) -> Option<(usize, usize)> {
        self.selected_match
            .map(|selected| (selected + 1, self.matches.len()))
    }

    pub fn draw(&mut self, frame: &mut Frame, area: Rect) {
        if self.visible {
            let title = match self.match_status() {
                Some((current, total)) => format!("Search {current}/{total}"),
                None => "Search".to_string(),
            };

            let mut popup_block = Block::default()
                .title(title)
                .borders(Borders::ALL)
                .border_type(ratatui::widgets::BorderType::Rounded)
                .border_style(Theme::block())
                .style(Style::default().bg(Theme::BG));

            if self.selected_match.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);
        }
    }
}