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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use crossterm::{
    cursor, event,
    event::{Event, KeyCode, KeyEvent, KeyModifiers},
    terminal,
    terminal::{
        ClearType, DisableLineWrap, EnableLineWrap, EnterAlternateScreen, LeaveAlternateScreen,
    },
    ErrorKind as CrosstermError, ExecutableCommand, QueueableCommand,
};
use fuzzy_matcher::skim::SkimMatcherV2;

use std::{
    borrow::Cow,
    fmt,
    fmt::{Display, Formatter},
    io::{Error as IoError, Write},
    slice,
    time::Duration,
};

macro_rules! impl_error {
    ($($err:ident),*) => {
        #[derive(Debug)]
        pub enum Error {
            $($err($err)),*
        }

        impl std::error::Error for Error {
            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                match self {
                    $(Self::$err(e) => Some(e)),*
                }
            }
        }

        impl Display for Error {
            fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
                match self {
                    $(Self::$err(e) => write!(f, "{}", e)),*
                }
            }
        }

        $(
            impl From<$err> for Error {
                fn from(e: $err) -> Self { Self::$err(e) }
            }
        )*
    }
}

pub fn select<'a, W: Write>(writer: W, list: &'a [&str]) -> Result<Cow<'a, [&'a str]>> {
    Fz::new(writer)?.select(list)
}

pub type Result<T> = std::result::Result<T, Error>;
impl_error!(IoError, CrosstermError);

struct Fz<'a, W: Write> {
    pattern: String,        // pattern written by user
    matches: Vec<&'a str>,  // items matched by the pattern
    offset: usize,          // offset of first item shown to user
    index: usize,           // visible position, upwards from the bottom
    selected: Vec<&'a str>, // selected items
    writer: W,              // stdout/stderr
    width: u16,             // height of terminal
    height: u16,            // width of terminal
}

impl<'a, W: Write> Fz<'a, W> {
    fn new(writer: W) -> Result<Self> {
        let (width, height) = terminal::size()?;
        Ok(Self {
            pattern: String::new(),
            matches: Vec::new(),
            offset: 0,
            index: 0,
            selected: Vec::new(),
            writer,
            width,
            height,
        })
    }

    #[inline]
    fn max_rows(&self) -> u16 {
        self.height - 2
    }

    fn move_cursor(&self) -> cursor::MoveTo {
        // move cursor to the last line, to the end of pattern
        cursor::MoveTo(self.pattern.chars().count() as u16, self.height - 1)
    }

    fn select(mut self, list: &'a [&str]) -> Result<Cow<'a, [&'a str]>> {
        // initially fill matches with the whole list
        self.update_matches(list);

        // setup
        terminal::enable_raw_mode()?;
        self.writer
            .queue(EnterAlternateScreen)?
            .queue(DisableLineWrap)?;

        // initial draw
        self.redraw()?;
        self.writer.execute(self.move_cursor())?;

        // event loop
        loop {
            // poll if an event is available
            if let Ok(true) = event::poll(Duration::from_secs(2)) {
                match event::read() {
                    // handle resize
                    Ok(Event::Resize(w, h)) => {
                        self.width = w;
                        self.height = h;
                        self.redraw()?;
                    }
                    // return selected items
                    Ok(Event::Key(
                        KeyEvent {
                            code: KeyCode::Enter,
                            ..
                        }
                        | KeyEvent {
                            code: KeyCode::Char('m'),
                            modifiers: KeyModifiers::CONTROL,
                        },
                    )) => break,
                    // move up a row
                    Ok(Event::Key(
                        KeyEvent {
                            code: KeyCode::Up, ..
                        }
                        | KeyEvent {
                            code: KeyCode::Char('p'),
                            modifiers: KeyModifiers::CONTROL,
                        },
                    )) => {
                        if !self.matches.is_empty() {
                            // don't go up if there are no more matches
                            if self.offset + self.index < self.matches.len() - 1 {
                                // clear previous position marker
                                self.position(false)?;

                                match self.index == self.max_rows() as usize {
                                    // increment index
                                    false => self.index += 1,
                                    // on topmost row -> move the whole view up
                                    true => {
                                        self.offset += 1;
                                        self.redraw()?;
                                    }
                                }

                                // draw new position marker
                                self.position(true)?;
                            }
                        }
                    }
                    // move down a row
                    Ok(Event::Key(
                        KeyEvent {
                            code: KeyCode::Down,
                            ..
                        }
                        | KeyEvent {
                            code: KeyCode::Char('n'),
                            modifiers: KeyModifiers::CONTROL,
                        },
                    )) => {
                        if !self.matches.is_empty() {
                            // don't go down if already at first match
                            if self.offset + self.index > 0 {
                                // clear previous position marker
                                self.position(false)?;

                                match self.index == 0 {
                                    // decrement index
                                    false => self.index -= 1,
                                    // on bottom row -> move the whole view down
                                    true => {
                                        self.offset -= 1;
                                        self.redraw()?;
                                    }
                                }

                                // draw new position marker
                                self.position(true)?;
                            }
                        }
                    }
                    // toggle selection
                    Ok(Event::Key(KeyEvent {
                        code: KeyCode::Tab, ..
                    })) => {
                        if !self.matches.is_empty() {
                            let current_item = self.matches[self.offset + self.index];

                            // find the index of current_item in selected if it has one
                            match self.selected.iter().position(|s| *s == current_item) {
                                // remove the (existing) selection
                                Some(index) => {
                                    self.selected.remove(index);
                                    self.selection(false, self.index as u16)?;
                                }
                                // add a new selection
                                None => {
                                    self.selected.push(current_item);
                                    self.selection(true, self.index as u16)?;
                                }
                            }
                        }
                    }
                    // erase a character from pattern
                    Ok(Event::Key(KeyEvent {
                        code: KeyCode::Backspace,
                        ..
                    })) => {
                        self.pattern.pop();

                        self.update_matches(list);
                        self.redraw()?;
                    }
                    // add a character to pattern (only if no modifiers except SHIFT are pressed)
                    Ok(Event::Key(KeyEvent {
                        code: KeyCode::Char(c),
                        modifiers: km,
                    })) if !km.intersects(!KeyModifiers::SHIFT) => {
                        match km {
                            KeyModifiers::NONE => self.pattern.push(c),
                            KeyModifiers::SHIFT => self.pattern.push(c.to_ascii_uppercase()),
                            _ => unreachable!(),
                        }

                        self.update_matches(list);
                        self.redraw()?;
                    }
                    _ => (),
                }
            }

            // move cursor and flush changes
            self.writer.execute(self.move_cursor())?;
        }

        // undo the setup
        self.writer
            .queue(LeaveAlternateScreen)?
            .execute(EnableLineWrap)?;
        terminal::disable_raw_mode()?;

        // return selected items
        let selected = match self.selected.is_empty() {
            true => match self.matches.is_empty() {
                true => Cow::Borrowed(&[] as &[&str]),
                false => {
                    // borrow selected item from list to satisfy borrow checker
                    let selected_item = list
                        .iter()
                        .find(|&i| i == &self.matches[self.offset + self.index])
                        .unwrap();
                    Cow::Borrowed(slice::from_ref(selected_item))
                }
            },
            false => Cow::from(self.selected),
        };
        Ok(selected)
    }

    fn redraw(&mut self) -> Result<()> {
        // clear the whole screen
        self.writer.queue(terminal::Clear(ClearType::All))?;

        // can't change during drawing
        let max_rows = self.max_rows();

        // draw rows
        for (i, m) in self
            .matches
            .iter()
            .skip(self.offset) // start iterating matches from offset
            .take(max_rows as usize) // only print matches that fit on screen
            .enumerate()
        {
            // draw the match
            self.writer
                .queue(cursor::MoveTo(2, max_rows - i as u16))?
                .write_all(m.as_bytes())?;

            // draw selection marker if the match is selected
            if self.selected.contains(m) {
                // inlined self.selection to satisfy borrow checker
                self.writer
                    .queue(cursor::MoveTo(1, max_rows - i as u16))?
                    .write_all(b"*")?;
            }

            // end overflowing lines with ..
            if m.chars().count() > self.width as usize - 2
            // matches start from third column
            {
                self.writer
                    .queue(cursor::MoveTo(self.width - 2, max_rows - i as u16))?
                    .write_all(b"..")?;
            }
        }

        if !self.matches.is_empty() {
            // draw position marker
            self.position(true)?;
        }

        // draw pattern
        self.writer
            .queue(cursor::MoveTo(0, self.height - 1))?
            .write_all(self.pattern.as_bytes())?;

        Ok(())
    }

    // shows or hides the position marker for current index
    fn position(&mut self, show: bool) -> Result<()> {
        let character = match show {
            true => b'>',
            false => b' ',
        };

        self.writer
            .queue(cursor::MoveTo(0, self.max_rows() - self.index as u16))?
            .write_all(&[character])?;

        Ok(())
    }

    // shows or hides the selection marker for given row
    fn selection(&mut self, show: bool, row: u16) -> Result<()> {
        let character = match show {
            true => b'*',
            false => b' ',
        };

        self.writer
            .queue(cursor::MoveTo(1, self.max_rows() - row))?
            .write_all(&[character])?;

        Ok(())
    }

    fn update_matches(&mut self, items: &'a [&str]) {
        self.matches.clear();

        match self.pattern.is_empty() {
            // match all items if pattern is empty
            true => {
                // add all items and sort them
                self.matches.extend(items);
                self.matches.sort_unstable();
                // there can't be less matches than previously
                //   -> offset + index will point to an existing item
            }
            // fuzzy match items with non-empty pattern
            false => {
                let matcher = SkimMatcherV2::default();
                // items with corresponding scores (for sorting)
                let mut scored = Vec::new();

                for item in items {
                    if let Some((score, _indices)) = matcher.fuzzy(item, &self.pattern, false) {
                        scored.push((item, score));
                    }
                }

                scored.sort_unstable_by(|(a_item, a_score), (b_item, b_score)| {
                    match a_score == b_score {
                        false => a_score.cmp(b_score), // sort by score
                        true => a_item.cmp(b_item),    // sort by item if scores are equal
                    }
                });

                // add sorted matches
                self.matches.extend(scored.into_iter().map(|(i, _s)| i));

                // reset offset so that matches with best scores are visible
                self.offset = 0;

                match self.matches.is_empty() {
                    // reset index back to 0 if there are no matches
                    true => self.index = 0,
                    false => {
                        if self.index >= self.matches.len() {
                            // set index to point to the last item
                            self.index = self.matches.len() - 1;
                        }
                    }
                }
            }
        }
    }
}