Skip to content

Commit

Permalink
Add option to only list directories
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxcode committed Jan 29, 2024
1 parent 5e27b12 commit 9924644
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
13 changes: 9 additions & 4 deletions src/data/directory_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ impl DirectoryContent {
Self { content: vec![] }
}

pub fn from_path(path: &Path) -> io::Result<Self> {
pub fn from_path(path: &Path, include_files: bool) -> io::Result<Self> {
Ok(Self {
content: load_directory(path)?,
content: load_directory(path, include_files)?,
})
}

Expand All @@ -60,13 +60,18 @@ impl DirectoryContent {
}
}

fn load_directory(path: &Path) -> io::Result<Vec<DirectoryEntry>> {
fn load_directory(path: &Path, include_files: bool) -> io::Result<Vec<DirectoryEntry>> {
let paths = fs::read_dir(path)?;

let mut result: Vec<DirectoryEntry> = Vec::new();
for path in paths {
match path {
Ok(entry) => result.push(DirectoryEntry::from_path(entry.path().as_path())),
Ok(entry) => {
if !include_files && entry.path().is_file() {
continue;
}
result.push(DirectoryEntry::from_path(entry.path().as_path()))
}
Err(_) => continue,
};
}
Expand Down
15 changes: 9 additions & 6 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct FileDialog {
mode: DialogMode,
state: DialogState,
initial_directory: PathBuf,
show_files: bool,

user_directories: Option<UserDirs>,
system_disks: Disks,
Expand Down Expand Up @@ -60,6 +61,7 @@ impl FileDialog {
mode: DialogMode::SelectDirectory,
state: DialogState::Closed,
initial_directory: std::env::current_dir().unwrap_or_default(),
show_files: true,

user_directories: UserDirs::new(),
system_disks: Disks::new_with_refreshed_list(),
Expand Down Expand Up @@ -87,11 +89,12 @@ impl FileDialog {
self
}

pub fn open(&mut self, mode: DialogMode) -> io::Result<()> {
pub fn open(&mut self, mode: DialogMode, show_files: bool) -> io::Result<()> {
self.reset();

self.mode = mode;
self.state = DialogState::Open;
self.show_files = show_files;

self.window_title = match mode {
DialogMode::SelectDirectory => "📁 Select Folder".to_string(),
Expand All @@ -103,15 +106,15 @@ impl FileDialog {
}

pub fn select_directory(&mut self) {
let _ = self.open(DialogMode::SelectDirectory);
let _ = self.open(DialogMode::SelectDirectory, false);
}

pub fn select_file(&mut self) {
let _ = self.open(DialogMode::SelectFile);
let _ = self.open(DialogMode::SelectFile, true);
}

pub fn save_file(&mut self) {
let _ = self.open(DialogMode::SaveFile);
let _ = self.open(DialogMode::SaveFile, true);
}

pub fn mode(&self) -> DialogMode {
Expand Down Expand Up @@ -550,8 +553,8 @@ impl FileDialog {

fn reset(&mut self) {
self.state = DialogState::Closed;
self.show_files = true;

self.user_directories = UserDirs::new();
self.system_disks = Disks::new_with_refreshed_list();

self.directory_stack = vec![];
Expand Down Expand Up @@ -712,7 +715,7 @@ impl FileDialog {
fn load_directory_content(&mut self, path: &Path) -> io::Result<()> {
self.directory_error = None;

self.directory_content = match DirectoryContent::from_path(path) {
self.directory_content = match DirectoryContent::from_path(path, self.show_files) {
Ok(content) => content,
Err(err) => {
self.directory_error = Some(err.to_string());
Expand Down

0 comments on commit 9924644

Please sign in to comment.