Skip to content

Commit

Permalink
feat: Do not read hidden files (#3)
Browse files Browse the repository at this point in the history
We do not read hidden files any more, which allows, for example, to use
git to store wallpapers.
  • Loading branch information
Yag000 authored Sep 4, 2023
2 parents cdfc98c + 6030eb1 commit 72e9bd8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Rust
on:
push:
branches: [master]
pull_request:

env:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wallshift"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Yago Iglesias Vazquez <yago.iglesias.vazquez@gmail.com>"]
description = " Simple wallpaper updater using feh"
Expand Down
58 changes: 36 additions & 22 deletions src/wallpaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn get_current_wallpaper() -> PathBuf {

/// Gets a random wallpaper from the wallpaper directory.
/// It can also return a folder, which will be handled by the caller.
/// Hidden files will be ignored.
///
/// # Panics
///
Expand All @@ -59,6 +60,41 @@ pub fn get_current_wallpaper() -> PathBuf {
pub fn get_random_wallpaper(settings: &Settings) -> Option<PathBuf> {
let files = read_dir(settings.wallpaper_dir.clone())
.expect("failed to open wallpaper directory")
.filter(|entry| {
!entry
.as_ref()
.expect("failed to get entry")
.file_name()
.to_str()
.expect("failed to convert file name to str")
.starts_with('.')
})
.collect::<Vec<_>>();

if files.is_empty() {
return None;
}

let random_number = rand::thread_rng().gen_range(0..files.len());
Some(files.get(random_number).unwrap().as_ref().unwrap().path())
}

/// Gets a random wallpaper from the wallpaper directory.
/// It will not return a folder.
/// Hidden files will be ignored.
///
/// # Panics
///
/// Panics if the wallpaper directory does not exist.
///
pub fn get_random_wallpaper_file(settings: &Settings) -> Option<PathBuf> {
let files = read_dir(settings.wallpaper_dir.clone())
.expect("failed to open wallpaper directory")
// Filter out folders
.filter(|entry| {
let path = entry.as_ref().unwrap().path();
path.is_file() && !path.as_os_str().to_str().unwrap().starts_with('.')
})
.collect::<Vec<_>>();

if files.is_empty() {
Expand Down Expand Up @@ -103,28 +139,6 @@ pub fn is_animated_wallpaper(settings: &Settings, path: &Path) -> bool {
false
}

/// Gets a random wallpaper from the wallpaper directory.
/// It will not return a folder.
///
/// # Panics
///
/// Panics if the wallpaper directory does not exist.
///
pub fn get_random_wallpaper_file(settings: &Settings) -> Option<PathBuf> {
let files = read_dir(settings.wallpaper_dir.clone())
.expect("failed to open wallpaper directory")
// Filter out folders
.filter(|entry| entry.as_ref().unwrap().path().is_file())
.collect::<Vec<_>>();

if files.is_empty() {
return None;
}

let random_number = rand::thread_rng().gen_range(0..files.len());
Some(files.get(random_number).unwrap().as_ref().unwrap().path())
}

/// Gets the name of the folder that contains the given path.
/// If the path is a folder it will return the name of the folder.
///
Expand Down

0 comments on commit 72e9bd8

Please sign in to comment.