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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use anyhow::Result;
use crossterm::event::{Event, KeyCode, KeyModifiers};
use tui::{
    backend::Backend,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Modifier, Style},
    text::{Span, Spans},
    widgets::{Block, Borders, List, ListItem, Paragraph},
    Frame,
};

use crate::{
    common::{OverflowText, StatefulList, Widget},
    model::Command,
    storage::SqliteStorage,
    theme::Theme,
    WidgetOutput,
};

/// Widget to search for [Command]
pub struct SearchWidget<'s> {
    /// Storage
    storage: &'s mut SqliteStorage,
    /// Current value of the filter box
    filter: String,
    /// Current cursor offset
    cursor_offset: usize,
    /// Command list of results
    commands: StatefulList<Command>,
}

impl<'s> SearchWidget<'s> {
    pub fn new(storage: &'s mut SqliteStorage, filter: String) -> Result<Self> {
        let commands = storage.find_commands(&filter)?;
        Ok(Self {
            commands: StatefulList::with_items(commands),
            cursor_offset: filter.len(),
            filter,
            storage,
        })
    }
}

impl<'s> Widget for SearchWidget<'s> {
    fn min_height(&self) -> usize {
        (self.commands.len() + 1).clamp(4, 15)
    }

    fn peek(&mut self) -> Result<Option<WidgetOutput>> {
        if self.storage.is_empty()? {
            let message = indoc::indoc! { r#"
                There are no stored commands yet!
                    - Try to bookmark some command with 'Ctrl + B'
                    - Or execute 'intelli-shell fetch' to download a bunch of tldr's useful commands"# 
            };
            Ok(Some(WidgetOutput::message(message)))
        } else if self.commands.len() == 1 {
            Ok(self.commands.current().map(|c| c.cmd.clone()).map(WidgetOutput::output))
        } else {
            Ok(None)
        }
    }

    fn render<B: Backend>(&mut self, frame: &mut Frame<B>, area: Rect, inline: bool, theme: Theme) {
        // Prepare main layout
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .margin(!inline as u16)
            .constraints([
                if inline {
                    Constraint::Length(1)
                } else {
                    Constraint::Length(3)
                },
                Constraint::Min(1),
            ])
            .split(area);

        let header = chunks[0];
        let body = chunks[1];

        // Display filter
        let mut filter_offset = self.cursor_offset;
        let max_width = header.width as usize - 1 - (2 * (!inline as usize));
        let text_inline = format!("(filter): {}", self.filter);
        let filter_text = if inline {
            filter_offset += 10;
            OverflowText::new(max_width, &text_inline)
        } else {
            OverflowText::new(max_width, &self.filter)
        };
        let filter_text_width = filter_text.width();
        if text_inline.len() > filter_text_width {
            let overflow = text_inline.len() as i32 - filter_text_width as i32;
            if overflow < filter_offset as i32 {
                filter_offset -= overflow as usize;
            } else {
                filter_offset = 0;
            }
        }
        let mut filter_input = Paragraph::new(filter_text).style(Style::default().fg(theme.main));
        if !inline {
            filter_input = filter_input.block(Block::default().borders(Borders::ALL).title(" Filter "));
        }
        frame.render_widget(filter_input, header);

        // Make the cursor visible and ask tui-rs to put it at the specified coordinates after rendering
        frame.set_cursor(
            // Put cursor past the end of the input text
            header.x + filter_offset as u16 + (!inline as u16),
            // Move one line down, from the border to the input line
            header.y + (!inline as u16),
        );

        // Display command suggestions
        let (commands, state) = self.commands.borrow();
        let commands: Vec<ListItem> = commands
            .iter()
            .map(|c| {
                let content = Spans::from(vec![
                    Span::raw(&c.cmd),
                    Span::styled(" # ", Style::default().fg(theme.secondary)),
                    Span::styled(&c.description, Style::default().fg(theme.secondary)),
                ]);
                ListItem::new(content)
            })
            .collect();

        let mut commands = List::new(commands)
            .style(Style::default().fg(theme.main))
            .highlight_style(
                Style::default()
                    .bg(theme.selected_background)
                    .add_modifier(Modifier::BOLD),
            )
            .highlight_symbol(">> ");
        if !inline {
            commands = commands.block(
                Block::default()
                    .border_style(Style::default().fg(theme.main))
                    .borders(Borders::ALL)
                    .title(" Commands "),
            );
        }
        frame.render_stateful_widget(commands, body, state);
    }

    fn process_event(&mut self, event: Event) -> Result<Option<WidgetOutput>> {
        if let Event::Key(key) = event {
            let has_ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
            match key.code {
                KeyCode::Char(c) if has_ctrl && c == 'd' => {
                    // Delete
                    if let Some(cmd) = self.commands.delete_current() {
                        self.storage.delete_command(cmd.id)?;
                    }
                }
                KeyCode::Enter | KeyCode::Tab => {
                    if let Some(cmd) = self.commands.current_mut() {
                        cmd.increment_usage();
                        self.storage.update_command(cmd)?;
                        return Ok(Some(WidgetOutput::output(cmd.cmd.clone())));
                    } else if self.filter.is_empty() {
                        return Ok(Some(WidgetOutput::empty()));
                    } else {
                        return Ok(Some(WidgetOutput::output(self.filter.clone())));
                    }
                }
                KeyCode::Char(c) => {
                    self.filter.insert(self.cursor_offset, c);
                    self.cursor_offset += 1;
                    self.commands.update_items(self.storage.find_commands(&self.filter)?);
                }
                KeyCode::Backspace => {
                    if !self.filter.is_empty() {
                        self.filter.remove(self.cursor_offset - 1);
                        self.cursor_offset -= 1;
                        self.commands.update_items(self.storage.find_commands(&self.filter)?);
                    }
                }
                KeyCode::Delete => {
                    if !self.filter.is_empty() && self.cursor_offset < self.filter.len() {
                        self.filter.remove(self.cursor_offset);
                        self.commands.update_items(self.storage.find_commands(&self.filter)?);
                    }
                }
                KeyCode::Right => {
                    if self.cursor_offset < self.filter.len() {
                        self.cursor_offset += 1;
                    }
                }
                KeyCode::Left => {
                    if self.cursor_offset > 0 {
                        self.cursor_offset -= 1;
                    }
                }
                KeyCode::Down => {
                    self.commands.next();
                }
                KeyCode::Up => {
                    self.commands.previous();
                }
                KeyCode::Esc => {
                    if self.filter.is_empty() {
                        return Ok(Some(WidgetOutput::empty()));
                    } else {
                        return Ok(Some(WidgetOutput::output(self.filter.clone())));
                    }
                }
                _ => (),
            }
        }
        // Continue waiting for input
        Ok(None)
    }
}