aboutsummaryrefslogtreecommitdiffstats
path: root/src/command/search.rs
blob: d51e29ef7b133cbb3c63897c72b5c46790014753 (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
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
217
218
219
220
use eyre::Result;
use itertools::Itertools;
use std::io::stdout;
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
use tui::{
    backend::TermionBackend,
    layout::{Alignment, Constraint, Corner, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Span, Spans, Text},
    widgets::{Block, Borders, List, ListItem, ListState, Paragraph},
    Terminal,
};
use unicode_width::UnicodeWidthStr;

use crate::command::event::{Event, Events};
use crate::local::database::Database;
use crate::local::history::History;

const VERSION: &str = env!("CARGO_PKG_VERSION");

struct State {
    input: String,

    results: Vec<History>,

    results_state: ListState,
}

fn query_results(app: &mut State, db: &mut impl Database) {
    let results = match app.input.as_str() {
        "" => db.list(),
        i => db.prefix_search(i),
    };

    if let Ok(results) = results {
        app.results = results.into_iter().rev().unique().collect();
    }

    if app.results.is_empty() {
        app.results_state.select(None);
    } else {
        app.results_state.select(Some(0));
    }
}

fn key_handler(input: Key, db: &mut impl Database, app: &mut State) -> Option<String> {
    match input {
        Key::Esc | Key::Char('\n') => {
            let i = app.results_state.selected().unwrap_or(0);

            return Some(app.results.get(i).unwrap().command.clone());
        }
        Key::Char(c) => {
            app.input.push(c);
            query_results(app, db);
        }
        Key::Backspace => {
            app.input.pop();
            query_results(app, db);
        }
        Key::Down => {
            let i = match app.results_state.selected() {
                Some(i) => {
                    if i == 0 {
                        0
                    } else {
                        i - 1
                    }
                }
                None => 0,
            };
            app.results_state.select(Some(i));
        }
        Key::Up => {
            let i = match app.results_state.selected() {
                Some(i) => {
                    if i >= app.results.len() - 1 {
                        app.results.len() - 1
                    } else {
                        i + 1
                    }
                }
                None => 0,
            };
            app.results_state.select(Some(i));
        }
        _ => {}
    };

    None
}

// this is a big blob of horrible! clean it up!
// for now, it works. But it'd be great if it were more easily readable, and
// modular. I'd like to add some more stats and stuff at some point
#[allow(clippy::clippy::cast_possible_truncation)]
fn select_history(query: &[String], db: &mut impl Database) -> Result<String> {
    let stdout = stdout().into_raw_mode()?;
    let stdout = MouseTerminal::from(stdout);
    let stdout = AlternateScreen::from(stdout);
    let backend = TermionBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    // Setup event handlers
    let events = Events::new();

    let mut app = State {
        input: query.join(" "),
        results: Vec::new(),
        results_state: ListState::default(),
    };

    query_results(&mut app, db);

    loop {
        // Handle input
        if let Event::Input(input) = events.next()? {
            if let Some(output) = key_handler(input, db, &mut app) {
                return Ok(output);
            }
        }

        terminal.draw(|f| {
            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .margin(1)
                .constraints(
                    [
                        Constraint::Length(2),
                        Constraint::Min(1),
                        Constraint::Length(3),
                    ]
                    .as_ref(),
                )
                .split(f.size());

            let top_chunks = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
                .split(chunks[0]);

            let top_left_chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Length(1), Constraint::Length(1)].as_ref())
                .split(top_chunks[0]);

            let top_right_chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Length(1), Constraint::Length(1)].as_ref())
                .split(top_chunks[1]);

            let title = Paragraph::new(Text::from(Span::styled(
                format!("A'tuin v{}", VERSION),
                Style::default().add_modifier(Modifier::BOLD),
            )));

            let help = vec![
                Span::raw("Press "),
                Span::styled("Esc", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(" to exit."),
            ];

            let help = Text::from(Spans::from(help));
            let help = Paragraph::new(help);

            let input = Paragraph::new(app.input.as_ref())
                .block(Block::default().borders(Borders::ALL).title("Search"));

            let results: Vec<ListItem> = app
                .results
                .iter()
                .enumerate()
                .map(|(i, m)| {
                    let mut content = Span::raw(m.command.to_string());

                    if let Some(selected) = app.results_state.selected() {
                        if selected == i {
                            content.style =
                                Style::default().fg(Color::Red).add_modifier(Modifier::BOLD);
                        }
                    }

                    ListItem::new(content)
                })
                .collect();

            let results = List::new(results)
                .block(Block::default().borders(Borders::ALL).title("History"))
                .start_corner(Corner::BottomLeft)
                .highlight_symbol(">> ");

            let stats = Paragraph::new(Text::from(Span::raw(format!(
                "history count: {}",
                db.history_count().unwrap()
            ))))
            .alignment(Alignment::Right);

            f.render_widget(title, top_left_chunks[0]);
            f.render_widget(help, top_left_chunks[1]);

            f.render_widget(stats, top_right_chunks[0]);
            f.render_stateful_widget(results, chunks[1], &mut app.results_state);
            f.render_widget(input, chunks[2]);

            f.set_cursor(
                // Put cursor past the end of the input text
                chunks[2].x + app.input.width() as u16 + 1,
                // Move one line down, from the border to the input line
                chunks[2].y + 1,
            );
        })?;
    }
}

pub fn run(query: &[String], db: &mut impl Database) -> Result<()> {
    let item = select_history(query, db)?;
    eprintln!("{}", item);

    Ok(())
}