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
|
use std::ffi::OsStr;
use std::path::PathBuf;
use crate::impl_content;
use crate::impl_selectable;
use crate::modes::ContentWindow;
#[derive(Debug)]
pub struct Fuzzy {
pub content: Vec<PathBuf>,
pub index: usize,
pub window: ContentWindow,
}
impl Fuzzy {
pub fn new(content: Vec<PathBuf>, terminal_height: usize) -> Self {
Self {
window: ContentWindow::new(content.len(), terminal_height),
content,
index: 0,
}
}
pub fn select_next(&mut self) {
self.next();
self.window.scroll_down_one(self.index);
}
pub fn select_prev(&mut self) {
self.prev();
self.window.scroll_up_one(self.index);
}
pub fn select_first(&mut self) {
self.index = 0;
self.window.scroll_to(0);
}
pub fn select_last(&mut self) {
self.index = self.content.len().checked_sub(1).unwrap_or_default();
self.window.scroll_to(self.index);
}
/// Set the index to the minimum of given index and the maximum possible index (len - 1)
pub fn select_index(&mut self, index: usize) {
self.index = index.min(self.content.len().checked_sub(1).unwrap_or_default());
self.window.scroll_to(self.index);
}
pub fn select_row(&mut self, row: u16) {
let index = row.checked_sub(3).unwrap_or_default() as usize + self.window.top;
self.select_index(index);
}
pub fn page_down(&mut self) {
for _ in 0..10 {
if self.index + 1 == self.content.len() {
break;
}
self.select_next();
}
}
pub fn page_up(&mut self) {
for _ in 0..10 {
if self.index == 0 {
break;
}
self.select_prev();
}
}
pub fn push(&mut self, path: PathBuf) {
self.content.push(path);
self.reset_window();
}
pub fn reset_window(&mut self) {
self.window.reset(self.content.len())
}
pub fn update(&mut self, content: Vec<PathBuf>) {
self.content = content;
self.reset_window();
self.index = 0;
}
pub fn clear(&mut self) {
self.content = vec![];
self.reset_window();
self.index = 0;
}
pub fn remove_selected(&mut self) {
self.content.remove(self.index);
self.index.checked_sub(0).unwrap_or_default();
self.reset_window();
}
pub fn replace_selected(&mut self, new_path: PathBuf) {
if self.content.is_empty() {
return;
}
self.content[self.index] = new_path;
}
pub fn filenames_containing(&self, input_string: &str) -> Vec<String> {
let to_filename: fn(&PathBuf) -> Option<&OsStr> = |path| path.file_name();
let to_str: fn(&OsStr) -> Option<&str> = |filename| filename.to_str();
self.content
.iter()
.filter_map(to_filename)
.filter_map(to_str)
.filter(|&p| p.contains(input_string))
.map(|p| p.to_owned())
.collect()
}
}
impl_content!(PathBuf, Fuzzy);
impl_selectable!(Fuzzy);
|