From 7b0f7dbfbe0f44666daae84f359afb83612b5954 Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Mon, 22 Jul 2024 12:49:37 +0200 Subject: [PATCH] Add file browser --- helix-term/src/commands.rs | 12 ++++++ helix-term/src/ui/mod.rs | 53 +++++++++++++++++++++++ helix-term/src/ui/picker.rs | 84 +++++++++++++++++++++++++++---------- 3 files changed, 128 insertions(+), 21 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b1c29378d..231372a1a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -375,6 +375,7 @@ impl MappableCommand { file_picker, "Open file picker", file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory", file_picker_in_current_directory, "Open file picker at current working directory", + file_browser, "Open file browser at current buffer's directory", code_action, "Perform code action", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", @@ -2948,6 +2949,17 @@ fn file_picker_in_current_directory(cx: &mut Context) { cx.push_layer(Box::new(overlaid(picker))); } +fn file_browser(cx: &mut Context) { + let cwd = helix_stdx::env::current_working_dir(); + if !cwd.exists() { + cx.editor + .set_error("Current working directory does not exist"); + return; + } + let picker = ui::file_browser(cwd, &cx.editor.config()); + cx.push_layer(Box::new(overlaid(picker))); +} + fn buffer_picker(cx: &mut Context) { let current = view!(cx.editor).doc; diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 6a3e198c1..49a3d87ac 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -30,6 +30,7 @@ pub use text::Text; use helix_view::Editor; +use std::path::Path; use std::{error::Error, path::PathBuf}; pub fn prompt( @@ -265,6 +266,58 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi picker } +pub fn file_browser(root: PathBuf, _config: &helix_view::editor::Config) -> FilePicker { + let directory_content = directory_content(&root); + + let columns = [PickerColumn::new( + "path", + |item: &PathBuf, root: &PathBuf| { + item.strip_prefix(root) + .unwrap_or(item) + .to_string_lossy() + .into() + }, + )]; + let picker = Picker::new(columns, 0, [], root, move |cx, path: &PathBuf, action| { + if let Err(e) = cx.editor.open(path, action) { + let err = if let Some(err) = e.source() { + format!("{}", err) + } else { + format!("unable to open \"{}\"", path.display()) + }; + cx.editor.set_error(err); + } + }) + .with_preview(|_editor, path| Some((path.as_path().into(), None))); + let injector = picker.injector(); + + if let Ok(files) = directory_content { + for file in files { + if injector.push(file).is_err() { + break; + } + } + } + picker +} + +fn directory_content(path: &Path) -> Result, std::io::Error> { + let mut dirs = Vec::new(); + let mut files = Vec::new(); + for entry in std::fs::read_dir(path)?.flatten() { + let entry_path = entry.path(); + if entry.path().is_dir() { + dirs.push(entry_path); + } else { + files.push(entry_path); + } + } + dirs.sort(); + files.sort(); + dirs.extend(files); + Ok(dirs) +} + pub mod completers { use crate::ui::prompt::Completion; use helix_core::fuzzy::fuzzy_match; diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 82fe96891..766bbf357 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -99,6 +99,7 @@ pub type FileLocation<'a> = (PathOrId<'a>, Option<(usize, usize)>); pub enum CachedPreview { Document(Box), + Directory(Vec), Binary, LargeFile, NotFound, @@ -120,12 +121,20 @@ impl Preview<'_, '_> { } } + fn dir_content(&self) -> Option<&Vec> { + match self { + Preview::Cached(CachedPreview::Directory(dir_content)) => Some(dir_content), + _ => None, + } + } + /// Alternate text to show for the preview. fn placeholder(&self) -> &str { match *self { Self::EditorDocument(_) => "", Self::Cached(preview) => match preview { CachedPreview::Document(_) => "", + CachedPreview::Directory(_) => "", CachedPreview::Binary => "", CachedPreview::LargeFile => "", CachedPreview::NotFound => "", @@ -599,33 +608,52 @@ impl Picker { } let path: Arc = path.into(); - let data = std::fs::File::open(&path).and_then(|file| { - let metadata = file.metadata()?; - // Read up to 1kb to detect the content type - let n = file.take(1024).read_to_end(&mut self.read_buffer)?; - let content_type = content_inspector::inspect(&self.read_buffer[..n]); - self.read_buffer.clear(); - Ok((metadata, content_type)) - }); - let preview = data - .map( - |(metadata, content_type)| match (metadata.len(), content_type) { - (_, content_inspector::ContentType::BINARY) => CachedPreview::Binary, - (size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => { - CachedPreview::LargeFile + let preview = std::fs::metadata(&path) + .and_then(|metadata| { + if metadata.is_dir() { + let files = super::directory_content(&path)?; + let file_names: Vec<_> = files + .iter() + .filter_map(|file| file.file_name()) + .map(|name| name.to_string_lossy().into_owned()) + .collect(); + Ok(CachedPreview::Directory(file_names)) + } else if metadata.is_file() { + if metadata.len() > MAX_FILE_SIZE_FOR_PREVIEW { + return Ok(CachedPreview::LargeFile); + } + let content_type = std::fs::File::open(&path).and_then(|file| { + // Read up to 1kb to detect the content type + let n = file.take(1024).read_to_end(&mut self.read_buffer)?; + let content_type = + content_inspector::inspect(&self.read_buffer[..n]); + self.read_buffer.clear(); + Ok(content_type) + })?; + if content_type.is_binary() { + return Ok(CachedPreview::Binary); } - _ => Document::open(&path, None, None, editor.config.clone()) - .map(|doc| { + Document::open(&path, None, None, editor.config.clone()).map_or( + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Cannot open document", + )), + |doc| { // Asynchronously highlight the new document helix_event::send_blocking( &self.preview_highlight_handler, path.clone(), ); - CachedPreview::Document(Box::new(doc)) - }) - .unwrap_or(CachedPreview::NotFound), - }, - ) + Ok(CachedPreview::Document(Box::new(doc))) + }, + ) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Neither a dir, nor a file", + )) + } + }) .unwrap_or(CachedPreview::NotFound); self.preview_cache.insert(path.clone(), preview); Some((Preview::Cached(&self.preview_cache[&path]), range)) @@ -859,6 +887,20 @@ impl Picker { doc } _ => { + if let Some(dir_content) = preview.dir_content() { + for (i, entry) in dir_content.iter().take(inner.height as usize).enumerate() + { + surface.set_stringn( + inner.x, + inner.y + i as u16, + entry, + inner.width as usize, + text, + ); + } + return; + } + let alt_text = preview.placeholder(); let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2; let y = inner.y + inner.height / 2;