Style directories in file browser

This commit is contained in:
Denys Rybalka 2024-12-23 23:59:28 +01:00
parent 0b01885059
commit 52a9cef7b0
No known key found for this signature in database
GPG key ID: 1F6284E97DB46ED1
3 changed files with 44 additions and 41 deletions

View file

@ -2996,7 +2996,7 @@ fn file_browser(cx: &mut Context) {
return;
}
if let Ok(picker) = ui::file_browser(root) {
if let Ok(picker) = ui::file_browser(root, cx.editor) {
cx.push_layer(Box::new(overlaid(picker)));
}
}
@ -3023,7 +3023,7 @@ fn file_browser_in_current_buffer_directory(cx: &mut Context) {
}
};
if let Ok(picker) = ui::file_browser(path) {
if let Ok(picker) = ui::file_browser(path, cx.editor) {
cx.push_layer(Box::new(overlaid(picker)));
}
}
@ -3036,7 +3036,7 @@ fn file_browser_in_current_directory(cx: &mut Context) {
return;
}
if let Ok(picker) = ui::file_browser(cwd) {
if let Ok(picker) = ui::file_browser(cwd, cx.editor) {
cx.push_layer(Box::new(overlaid(picker)));
}
}

View file

@ -20,6 +20,7 @@ use crate::job::{self, Callback};
pub use completion::Completion;
pub use editor::EditorView;
use helix_stdx::rope;
use helix_view::theme::Style;
pub use markdown::Markdown;
pub use menu::Menu;
pub use picker::{Column as PickerColumn, FileLocation, Picker};
@ -29,6 +30,7 @@ pub use spinner::{ProgressSpinners, Spinner};
pub use text::Text;
use helix_view::Editor;
use tui::text::Span;
use std::path::Path;
use std::{error::Error, path::PathBuf};
@ -277,16 +279,18 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
picker
}
pub fn file_browser(root: PathBuf) -> Result<FilePicker, std::io::Error> {
let root = helix_stdx::path::canonicalize(root);
type FileBrowser = Picker<(PathBuf, bool), (PathBuf, Style)>;
pub fn file_browser(root: PathBuf, editor: &Editor) -> Result<FileBrowser, std::io::Error> {
let directory_style = editor.theme.get("ui.text.directory");
let directory_content = directory_content(&root)?;
let columns = [PickerColumn::new(
"path",
|item: &PathBuf, root: &PathBuf| {
let name = item.strip_prefix(root).unwrap_or(item).to_string_lossy();
if item.is_dir() {
format!("{}/", name).into()
|(path, is_dir): &(PathBuf, bool), (root, directory_style): &(PathBuf, Style)| {
let name = path.strip_prefix(root).unwrap_or(path).to_string_lossy();
if *is_dir {
Span::styled(format!("{}/", name), *directory_style).into()
} else {
name.into()
}
@ -296,14 +300,14 @@ pub fn file_browser(root: PathBuf) -> Result<FilePicker, std::io::Error> {
columns,
0,
directory_content,
root,
move |cx, path: &PathBuf, action| {
if path.is_dir() {
(root, directory_style),
move |cx, (path, is_dir): &(PathBuf, bool), action| {
if *is_dir {
let owned_path = path.clone();
let callback = Box::pin(async move {
let call: Callback =
Callback::EditorCompositor(Box::new(move |_editor, compositor| {
if let Ok(picker) = file_browser(owned_path) {
Callback::EditorCompositor(Box::new(move |editor, compositor| {
if let Ok(picker) = file_browser(owned_path, editor) {
compositor.push(Box::new(overlay::overlaid(picker)));
}
}));
@ -320,30 +324,26 @@ pub fn file_browser(root: PathBuf) -> Result<FilePicker, std::io::Error> {
}
},
)
.with_preview(|_editor, path| Some((path.as_path().into(), None)));
.with_preview(|_editor, (path, _is_dir)| Some((path.as_path().into(), None)));
Ok(picker)
}
fn directory_content(path: &Path) -> Result<Vec<PathBuf>, std::io::Error> {
let mut dirs = Vec::new();
let mut files = Vec::new();
for entry in std::fs::read_dir(path)?.flatten() {
if entry.path().is_dir() {
dirs.push(entry.path());
} else {
files.push(entry.path());
}
}
dirs.sort();
files.sort();
fn directory_content(path: &Path) -> Result<Vec<(PathBuf, bool)>, std::io::Error> {
let mut content: Vec<_> = std::fs::read_dir(path)?
.flatten()
.map(|entry| {
(
entry.path(),
entry.file_type().is_ok_and(|file_type| file_type.is_dir()),
)
})
.collect();
let mut content = Vec::new();
content.sort_by(|(path1, is_dir1), (path2, is_dir2)| (!is_dir1, path1).cmp(&(!is_dir2, path2)));
if path.parent().is_some() {
content.insert(0, path.join(".."));
content.insert(0, (path.join(".."), true));
}
content.extend(dirs);
content.extend(files);
Ok(content)
}

View file

@ -85,7 +85,7 @@ pub type FileLocation<'a> = (PathOrId<'a>, Option<(usize, usize)>);
pub enum CachedPreview {
Document(Box<Document>),
Directory(Vec<String>),
Directory(Vec<(String, bool)>),
Binary,
LargeFile,
NotFound,
@ -107,7 +107,7 @@ impl Preview<'_, '_> {
}
}
fn dir_content(&self) -> Option<&Vec<String>> {
fn dir_content(&self) -> Option<&Vec<(String, bool)>> {
match self {
Preview::Cached(CachedPreview::Directory(dir_content)) => Some(dir_content),
_ => None,
@ -599,12 +599,12 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
let files = super::directory_content(&path)?;
let file_names: Vec<_> = files
.iter()
.filter_map(|file| {
let name = file.file_name()?.to_string_lossy();
if file.is_dir() {
Some(format!("{}/", name))
.filter_map(|(path, is_dir)| {
let name = path.file_name()?.to_string_lossy();
if *is_dir {
Some((format!("{}/", name), true))
} else {
Some(name.into_owned())
Some((name.into_owned(), false))
}
})
.collect();
@ -857,6 +857,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
// clear area
let background = cx.editor.theme.get("ui.background");
let text = cx.editor.theme.get("ui.text");
let directory = cx.editor.theme.get("ui.text.directory");
surface.clear_with(area, background);
const BLOCK: Block<'_> = Block::bordered();
@ -879,14 +880,16 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
}
_ => {
if let Some(dir_content) = preview.dir_content() {
for (i, entry) in dir_content.iter().take(inner.height as usize).enumerate()
for (i, (path, is_dir)) in
dir_content.iter().take(inner.height as usize).enumerate()
{
let style = if *is_dir { directory } else { text };
surface.set_stringn(
inner.x,
inner.y + i as u16,
entry,
path,
inner.width as usize,
text,
style,
);
}
return;