@@ -0,0 +1,205 @@
1+use crate::compositor::{Component, Compositor, Context, EventResult};
2+use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3+use tui::buffer::Buffer as Surface;
4+use tui::{
5+ layout::Rect,
6+ style::{Color, Style},
7+ widgets::{Block, Borders},
8+};
9+
10+use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
11+use fuzzy_matcher::FuzzyMatcher;
12+use ignore::Walk;
13+
14+use std::path::PathBuf;
15+
16+use crate::ui::{Prompt, PromptEvent};
17+use helix_core::Position;
18+use helix_view::Editor;
19+
20+pub struct Picker {
21+ files: Vec<PathBuf>,
22+ // filter: String,
23+ matcher: Box<Matcher>,
24+
25+ cursor: usize,
26+ // pattern: String,
27+ prompt: Prompt,
28+}
29+
30+impl Picker {
31+ pub fn new() -> Self {
32+ let files = Walk::new("./").filter_map(|entry| match entry {
33+ Ok(entry) => {
34+ // filter dirs, but we might need special handling for symlinks!
35+ if !entry.file_type().unwrap().is_dir() {
36+ Some(entry.into_path())
37+ } else {
38+ None
39+ }
40+ }
41+ Err(_err) => None,
42+ });
43+
44+ let prompt = Prompt::new(
45+ "".to_string(),
46+ |pattern: &str| Vec::new(),
47+ |editor: &mut Editor, pattern: &str, event: PromptEvent| {
48+ //
49+ },
50+ );
51+
52+ const MAX: usize = 1024;
53+
54+ Self {
55+ files: files.take(MAX).collect(),
56+ matcher: Box::new(Matcher::default()),
57+ cursor: 0,
58+ prompt,
59+ }
60+ }
61+
62+ pub fn score(&mut self, pattern: &str) {
63+ self.files.iter().filter_map(|path| match path.to_str() {
64+ // TODO: using fuzzy_indices could give us the char idx for match highlighting
65+ Some(path) => (self.matcher.fuzzy_match(path, pattern)),
66+ None => None,
67+ });
68+ }
69+
70+ pub fn move_up(&mut self) {
71+ self.cursor = self.cursor.saturating_sub(1);
72+ }
73+
74+ pub fn move_down(&mut self) {
75+ // TODO: len - 1
76+ if self.cursor < self.files.len() {
77+ self.cursor += 1;
78+ }
79+ }
80+}
81+
82+// process:
83+// - read all the files into a list, maxed out at a large value
84+// - on input change:
85+// - score all the names in relation to input
86+
87+impl Component for Picker {
88+ fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
89+ let key_event = match event {
90+ Event::Key(event) => event,
91+ Event::Resize(..) => return EventResult::Consumed(None),
92+ _ => return EventResult::Ignored,
93+ };
94+
95+ let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
96+ // remove the layer
97+ compositor.pop();
98+ })));
99+
100+ match key_event {
101+ // KeyEvent {
102+ // code: KeyCode::Char(c),
103+ // modifiers: KeyModifiers::NONE,
104+ // } => {
105+ // self.insert_char(c);
106+ // (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
107+ // }
108+ KeyEvent {
109+ code: KeyCode::Up, ..
110+ }
111+ | KeyEvent {
112+ code: KeyCode::Char('k'),
113+ modifiers: KeyModifiers::CONTROL,
114+ } => self.move_up(),
115+ KeyEvent {
116+ code: KeyCode::Down,
117+ ..
118+ }
119+ | KeyEvent {
120+ code: KeyCode::Char('j'),
121+ modifiers: KeyModifiers::CONTROL,
122+ } => self.move_down(),
123+ KeyEvent {
124+ code: KeyCode::Esc, ..
125+ } => {
126+ return close_fn;
127+ }
128+ _ => return self.prompt.handle_event(event, cx),
129+ }
130+
131+ EventResult::Consumed(None)
132+ }
133+
134+ fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
135+ let padding_vertical = area.height * 20 / 100;
136+ let padding_horizontal = area.width * 20 / 100;
137+
138+ let area = Rect::new(
139+ area.x + padding_horizontal,
140+ area.y + padding_vertical,
141+ area.width - padding_horizontal * 2,
142+ area.height - padding_vertical * 2,
143+ );
144+
145+ // -- Render the frame:
146+
147+ // clear area
148+ for y in area.top()..area.bottom() {
149+ for x in area.left()..area.right() {
150+ surface.get_mut(x, y).reset()
151+ }
152+ }
153+
154+ use tui::widgets::Widget;
155+ // don't like this but the lifetime sucks
156+ let block = Block::default().borders(Borders::ALL);
157+
158+ // calculate the inner area inside the box
159+ let inner = block.inner(area);
160+
161+ block.render(area, surface);
162+ // TODO: abstract into a clear(area) fn
163+ // surface.set_style(inner, Style::default().bg(Color::Rgb(150, 50, 0)));
164+
165+ // -- Render the input bar:
166+
167+ let area = Rect::new(inner.x + 1, inner.y, inner.width - 1, 1);
168+ self.prompt.render(area, surface, cx);
169+
170+ // -- Separator
171+ use tui::widgets::BorderType;
172+ let style = Style::default().fg(Color::Rgb(90, 89, 119));
173+ let symbols = BorderType::line_symbols(BorderType::Plain);
174+ for x in inner.left()..inner.right() {
175+ surface
176+ .get_mut(x, inner.y + 1)
177+ .set_symbol(symbols.horizontal)
178+ .set_style(style);
179+ }
180+
181+ // -- Render the contents:
182+
183+ let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
184+ let selected = Style::default().fg(Color::Rgb(255, 255, 255));
185+
186+ let rows = inner.height - 2; // -1 for search bar
187+ for (i, file) in self.files.iter().take(rows as usize).enumerate() {
188+ if i == self.cursor {
189+ surface.set_string(inner.x + 1, inner.y + 2 + i as u16, ">", selected);
190+ }
191+
192+ surface.set_stringn(
193+ inner.x + 3,
194+ inner.y + 2 + i as u16,
195+ file.strip_prefix("./").unwrap().to_str().unwrap(), // TODO: render paths without ./
196+ inner.width as usize - 1,
197+ if i == self.cursor { selected } else { style },
198+ );
199+ }
200+ }
201+
202+ fn cursor_position(&self, area: Rect, ctx: &mut Context) -> Option<Position> {
203+ self.prompt.cursor_position(area, ctx)
204+ }
205+}