on master help sign in
File picker mockup, reuses the line editor work done on Prompt.
commit7c75ec04e8db825f24f311d0c26109310738183d authorBlaž Hrastnik <blaz@mxxn.io> dateThu 17 Dec 2020, 18:08 (6 years ago) parented60866
8 files changed+314−3
Cargo.lock+86 helix-term/Cargo.toml+4 helix-term/src/commands.rs+7−1 helix-term/src/keymap.rs+2 helix-term/src/ui/helix.log helix-term/src/ui/mod.rs+2 helix-term/src/ui/picker.rs+205 helix-term/src/ui/prompt.rs+8−2
Cargo.lock+86
@@ -181,6 +181,15 @@ dependencies = [
181181 "once_cell",
182182]
183183
184+[[package]]
185+name = "bstr"
186+version = "0.2.14"
187+source = "registry+https://github.com/rust-lang/crates.io-index"
188+checksum = "473fc6b38233f9af7baa94fb5852dca389e3d95b8e21c8e3719301462c5d9faf"
189+dependencies = [
190+ "memchr",
191+]
192+
184193[[package]]
185194name = "cache-padded"
186195version = "1.1.1"
@@ -339,6 +348,12 @@ dependencies = [
339348 "log",
340349]
341350
351+[[package]]
352+name = "fnv"
353+version = "1.0.7"
354+source = "registry+https://github.com/rust-lang/crates.io-index"
355+checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
356+
342357[[package]]
343358name = "form_urlencoded"
344359version = "1.0.0"
@@ -429,6 +444,15 @@ dependencies = [
429444 "slab",
430445]
431446
447+[[package]]
448+name = "fuzzy-matcher"
449+version = "0.3.7"
450+source = "registry+https://github.com/rust-lang/crates.io-index"
451+checksum = "54614a3312934d066701a80f20f15fa3b56d67ac7722b39eea5b4c9dd1d66c94"
452+dependencies = [
453+ "thread_local",
454+]
455+
432456[[package]]
433457name = "getrandom"
434458version = "0.1.15"
@@ -446,6 +470,19 @@ version = "0.3.0"
446470source = "registry+https://github.com/rust-lang/crates.io-index"
447471checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
448472
473+[[package]]
474+name = "globset"
475+version = "0.4.6"
476+source = "registry+https://github.com/rust-lang/crates.io-index"
477+checksum = "c152169ef1e421390738366d2f796655fec62621dabbd0fd476f905934061e4a"
478+dependencies = [
479+ "aho-corasick",
480+ "bstr",
481+ "fnv",
482+ "log",
483+ "regex",
484+]
485+
449486[[package]]
450487name = "hashbrown"
451488version = "0.9.1"
@@ -508,9 +545,11 @@ dependencies = [
508545 "crossterm",
509546 "fern",
510547 "futures-util",
548+ "fuzzy-matcher",
511549 "helix-core",
512550 "helix-lsp",
513551 "helix-view",
552+ "ignore",
514553 "log",
515554 "num_cpus",
516555 "once_cell",
@@ -551,6 +590,24 @@ dependencies = [
551590 "unicode-normalization",
552591]
553592
593+[[package]]
594+name = "ignore"
595+version = "0.4.17"
596+source = "registry+https://github.com/rust-lang/crates.io-index"
597+checksum = "b287fb45c60bb826a0dc68ff08742b9d88a2fea13d6e0c286b3172065aaf878c"
598+dependencies = [
599+ "crossbeam-utils",
600+ "globset",
601+ "lazy_static",
602+ "log",
603+ "memchr",
604+ "regex",
605+ "same-file",
606+ "thread_local",
607+ "walkdir",
608+ "winapi-util",
609+]
610+
554611[[package]]
555612name = "indexmap"
556613version = "1.6.0"
@@ -929,6 +986,15 @@ version = "1.0.5"
929986source = "registry+https://github.com/rust-lang/crates.io-index"
930987checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
931988
989+[[package]]
990+name = "same-file"
991+version = "1.0.6"
992+source = "registry+https://github.com/rust-lang/crates.io-index"
993+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
994+dependencies = [
995+ "winapi-util",
996+]
997+
932998[[package]]
933999name = "scopeguard"
9341000version = "1.1.0"
@@ -1228,6 +1294,17 @@ version = "1.1.0"
12281294source = "registry+https://github.com/rust-lang/crates.io-index"
12291295checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
12301296
1297+[[package]]
1298+name = "walkdir"
1299+version = "2.3.1"
1300+source = "registry+https://github.com/rust-lang/crates.io-index"
1301+checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d"
1302+dependencies = [
1303+ "same-file",
1304+ "winapi",
1305+ "winapi-util",
1306+]
1307+
12311308[[package]]
12321309name = "wasi"
12331310version = "0.9.0+wasi-snapshot-preview1"
@@ -1265,6 +1342,15 @@ version = "0.4.0"
12651342source = "registry+https://github.com/rust-lang/crates.io-index"
12661343checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
12671344
1345+[[package]]
1346+name = "winapi-util"
1347+version = "0.1.5"
1348+source = "registry+https://github.com/rust-lang/crates.io-index"
1349+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
1350+dependencies = [
1351+ "winapi",
1352+]
1353+
12681354[[package]]
12691355name = "winapi-x86_64-pc-windows-gnu"
12701356version = "0.4.0"
helix-term/Cargo.toml+4
@@ -32,3 +32,7 @@ futures-util = "0.3"
3232fern = "0.6"
3333chrono = "0.4"
3434log = "0.4"
35+
36+# File picker
37+fuzzy-matcher = "0.3"
38+ignore = "0.4"
helix-term/src/commands.rs+7−1
@@ -10,7 +10,7 @@ use helix_core::{
1010use once_cell::sync::Lazy;
1111
1212use crate::compositor::Compositor;
13−use crate::ui::{Prompt, PromptEvent};
13+use crate::ui::{self, Prompt, PromptEvent};
1414
1515use helix_view::{
1616 document::Mode,
@@ -456,6 +456,12 @@ pub fn command_mode(cx: &mut Context) {
456456 compositor.push(Box::new(prompt));
457457 }));
458458}
459+pub fn file_picker(cx: &mut Context) {
460+ cx.callback = Some(Box::new(|compositor: &mut Compositor| {
461+ let picker = ui::Picker::new();
462+ compositor.push(Box::new(picker));
463+ }));
464+}
459465
460466// calculate line numbers for each selection range
461467fn selection_lines(state: &State) -> Vec<usize> {
helix-term/src/keymap.rs+2
@@ -183,6 +183,8 @@ pub fn default() -> Keymaps {
183183 }] => commands::page_down,
184184 vec![ctrl!('u')] => commands::half_page_up,
185185 vec![ctrl!('d')] => commands::half_page_down,
186+
187+ vec![ctrl!('p')] => commands::file_picker,
186188 ),
187189 Mode::Insert => hashmap!(
188190 vec![Key {
helix-term/src/ui/helix.lognew file

No changes to the content.

helix-term/src/ui/mod.rs+2
@@ -1,7 +1,9 @@
11mod editor;
2+mod picker;
23mod prompt;
34
45pub use editor::EditorView;
6+pub use picker::Picker;
57pub use prompt::{Prompt, PromptEvent};
68
79pub use tui::layout::Rect;
helix-term/src/ui/picker.rsnew file+205
@@ -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+}
helix-term/src/ui/prompt.rs+8−2
@@ -149,8 +149,13 @@ impl Prompt {
149149 }
150150 let line = area.height - 1;
151151 // render buffer text
152− surface.set_string(0, line, &self.prompt, text_color);
153− surface.set_string(self.prompt.len() as u16, line, &self.line, text_color);
152+ surface.set_string(area.x, area.y + line, &self.prompt, text_color);
153+ surface.set_string(
154+ area.x + self.prompt.len() as u16,
155+ area.y + line,
156+ &self.line,
157+ text_color,
158+ );
154159 }
155160}
156161
@@ -158,6 +163,7 @@ impl Component for Prompt {
158163 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
159164 let event = match event {
160165 Event::Key(event) => event,
166+ Event::Resize(..) => return EventResult::Consumed(None),
161167 _ => return EventResult::Ignored,
162168 };
163169