@@ -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;
6+ style::{Color, Style},
7+ widgets::{Block, Borders},
10+use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
11+use fuzzy_matcher::FuzzyMatcher;
14+use std::path::PathBuf;
16+use crate::ui::{Prompt, PromptEvent};
17+use helix_core::Position;
18+use helix_view::Editor;
23+ matcher: Box<Matcher>,
31+ pub fn new() -> Self {
32+ let files = Walk::new("./").filter_map(|entry| match 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())
44+ let prompt = Prompt::new(
46+ |pattern: &str| Vec::new(),
47+ |editor: &mut Editor, pattern: &str, event: PromptEvent| {
52+ const MAX: usize = 1024;
55+ files: files.take(MAX).collect(),
56+ matcher: Box::new(Matcher::default()),
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)),
70+ pub fn move_up(&mut self) {
71+ self.cursor = self.cursor.saturating_sub(1);
74+ pub fn move_down(&mut self) {
76+ if self.cursor < self.files.len() {
83+// - read all the files into a list, maxed out at a large value
85+// - score all the names in relation to input
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,
95+ let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
102+ // code: KeyCode::Char(c),
103+ // modifiers: KeyModifiers::NONE,
105+ // self.insert_char(c);
106+ // (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
109+ code: KeyCode::Up, ..
112+ code: KeyCode::Char('k'),
113+ modifiers: KeyModifiers::CONTROL,
114+ } => self.move_up(),
116+ code: KeyCode::Down,
120+ code: KeyCode::Char('j'),
121+ modifiers: KeyModifiers::CONTROL,
122+ } => self.move_down(),
124+ code: KeyCode::Esc, ..
128+ _ => return self.prompt.handle_event(event, cx),
131+ EventResult::Consumed(None)
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;
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,
145+ // -- Render the frame:
148+ for y in area.top()..area.bottom() {
149+ for x in area.left()..area.right() {
150+ surface.get_mut(x, y).reset()
154+ use tui::widgets::Widget;
155+ // don't like this but the lifetime sucks
156+ let block = Block::default().borders(Borders::ALL);
158+ // calculate the inner area inside the box
159+ let inner = block.inner(area);
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)));
165+ // -- Render the input bar:
167+ let area = Rect::new(inner.x + 1, inner.y, inner.width - 1, 1);
168+ self.prompt.render(area, surface, cx);
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() {
176+ .get_mut(x, inner.y + 1)
177+ .set_symbol(symbols.horizontal)
181+ // -- Render the contents:
183+ let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
184+ let selected = Style::default().fg(Color::Rgb(255, 255, 255));
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);
192+ surface.set_stringn(
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 },
202+ fn cursor_position(&self, area: Rect, ctx: &mut Context) -> Option<Position> {
203+ self.prompt.cursor_position(area, ctx)