This commit is contained in:
@@ -27,6 +27,7 @@ pub struct FileBrowserState {
|
||||
pub entries: Vec<DirEntry>,
|
||||
pub selected: usize,
|
||||
pub scroll_offset: usize,
|
||||
pub audio_counts: Vec<Option<usize>>,
|
||||
}
|
||||
|
||||
impl FileBrowserState {
|
||||
@@ -37,6 +38,7 @@ impl FileBrowserState {
|
||||
entries: Vec::new(),
|
||||
selected: 0,
|
||||
scroll_offset: 0,
|
||||
audio_counts: Vec::new(),
|
||||
};
|
||||
state.refresh_entries();
|
||||
state
|
||||
@@ -49,6 +51,7 @@ impl FileBrowserState {
|
||||
entries: Vec::new(),
|
||||
selected: 0,
|
||||
scroll_offset: 0,
|
||||
audio_counts: Vec::new(),
|
||||
};
|
||||
state.refresh_entries();
|
||||
state
|
||||
@@ -119,10 +122,27 @@ impl FileBrowserState {
|
||||
});
|
||||
|
||||
self.entries = entries;
|
||||
self.audio_counts = Vec::new();
|
||||
self.selected = 0;
|
||||
self.scroll_offset = 0;
|
||||
}
|
||||
|
||||
pub fn compute_audio_counts(&mut self) {
|
||||
let dir = self.current_dir();
|
||||
self.audio_counts = self
|
||||
.entries
|
||||
.iter()
|
||||
.map(|entry| {
|
||||
if !entry.is_dir || entry.name == ".." {
|
||||
return None;
|
||||
}
|
||||
let path = dir.join(&entry.name);
|
||||
let count = count_audio_files(&path);
|
||||
if count > 0 { Some(count) } else { None }
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
pub fn autocomplete(&mut self) {
|
||||
let real_entries: Vec<&DirEntry> =
|
||||
self.entries.iter().filter(|e| e.name != "..").collect();
|
||||
@@ -249,6 +269,23 @@ fn ensure_parent_dirs(path: &Path) {
|
||||
}
|
||||
}
|
||||
|
||||
fn count_audio_files(path: &Path) -> usize {
|
||||
let Ok(read_dir) = fs::read_dir(path) else {
|
||||
return 0;
|
||||
};
|
||||
read_dir
|
||||
.flatten()
|
||||
.filter(|e| {
|
||||
let name = e.file_name();
|
||||
let name = name.to_string_lossy();
|
||||
matches!(
|
||||
name.rsplit('.').next().map(|ext| ext.to_lowercase()).as_deref(),
|
||||
Some("wav" | "flac" | "ogg" | "aiff" | "aif" | "mp3")
|
||||
)
|
||||
})
|
||||
.count()
|
||||
}
|
||||
|
||||
fn longest_common_prefix(entries: &[&DirEntry]) -> String {
|
||||
if entries.is_empty() {
|
||||
return String::new();
|
||||
|
||||
Reference in New Issue
Block a user