Skip to content

Commit

Permalink
intersperse files with favourites
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrabovets committed Jul 10, 2023
1 parent f83f653 commit 88121e9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, windows-2019, macOS-latest, ubuntu-latest]
os: [ubuntu-20.04, windows-2019]
rust: [stable]
steps:
- uses: actions/checkout@v2
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
Expand Up @@ -11,7 +11,7 @@ license = "MIT"
name = "oculante"
readme = "README.md"
repository = "https://github.com/woelper/oculante/"
version = "0.6.68-dev2"
version = "0.6.68-dev3"

[package.metadata.bundle]
icon = ["res/oculante.png"]
Expand Down
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Maintainer: Johann Woelper <woelper@gmail.com>
pkgname=oculante
pkgver=0.6.68-dev2
pkgver=0.6.68-dev3
pkgrel=1
depends=('aom' 'libwebp' 'expat' 'freetype2' 'gtk3' 'cairo')
makedepends=('rust' 'cargo' 'tar' 'nasm' 'cmake')
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
// fill image sequence
if state.folder_selected.is_none() {
if let Some(p) = &state.current_path {
state.scrubber = scrubber::Scrubber::new(p, None, false, false);
state.scrubber = scrubber::Scrubber::new(p, None, false, false, None);
state.scrubber.wrap = state.persistent_settings.wrap_folder;

// debug!("{:#?} from {}", &state.scrubber, p.display());
Expand Down Expand Up @@ -1080,6 +1080,7 @@ fn browse_for_folder_path(state: &mut OculanteState) {
Some(FAVOURITES_FILE),
true,
true,
Some(3),
);
let current_path = state.scrubber.next();

Expand Down
75 changes: 65 additions & 10 deletions src/scrubber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ pub struct Scrubber {
}

impl Scrubber {
pub fn new(path: &Path, favourites_file: Option<&str>, randomize: bool, walk_files: bool) -> Self {
let entries = get_image_filenames_for_directory(path, favourites_file, randomize, walk_files)
pub fn new(
path: &Path,
favourites_file: Option<&str>,
randomize: bool,
walk_files: bool,
intersperse_with_favs_every_n: Option<usize>,
) -> Self {
let entries = get_image_filenames_for_directory(
path,
favourites_file,
randomize,
walk_files,
intersperse_with_favs_every_n,
)
.unwrap_or_default();
let index = entries.iter().position(|p| p == path).unwrap_or_default();
Self {
Expand Down Expand Up @@ -66,7 +78,13 @@ impl Scrubber {
// Get sorted list of files in a folder
// TODO: Should probably return an Result<T,E> instead, but am too lazy to figure out + handle a dedicated error type here
// TODO: Cache this result, instead of doing it each time we need to fetch another file from the folder
pub fn get_image_filenames_for_directory(folder_path: &Path, favourites_file: Option<&str>, randomize: bool, walk_files: bool) -> Result<Vec<PathBuf>> {
pub fn get_image_filenames_for_directory(
folder_path: &Path,
favourites_file: Option<&str>,
randomize: bool,
walk_files: bool,
intersperse_with_favs_every_n: Option<usize>,
) -> Result<Vec<PathBuf>> {
let mut folder_path = folder_path.to_path_buf();
if folder_path.is_file() {
folder_path = folder_path
Expand All @@ -75,14 +93,14 @@ pub fn get_image_filenames_for_directory(folder_path: &Path, favourites_file: Op
.context("Can't get parent")?;
}

let mut _favourites: HashSet<PathBuf> = Default::default();
let mut favourites: HashSet<PathBuf> = Default::default();

if let Some(favourites_file) = favourites_file {
let favourites_path = folder_path.join(Path::new(favourites_file));
if favourites_path.exists() {
let file = std::fs::File::open(favourites_path)?;
let reader = BufReader::new(file);
_favourites = reader
favourites = reader
.lines()
.filter_map(|line| line.ok())
.map(|file_str| folder_path.join(join_path_parts(file_str)))
Expand All @@ -97,8 +115,8 @@ pub fn get_image_filenames_for_directory(folder_path: &Path, favourites_file: Op
dir_files = WalkDir::new(folder_path)
.into_iter()
.filter_map(|v| v.ok())
.filter(|x| is_ext_compatible(x.path()))
.map(|x| x.into_path())
.map(|entry| entry.into_path())
.filter(|x| is_ext_compatible(x))
.collect::<Vec<PathBuf>>();
} else {
let info = std::fs::read_dir(folder_path)?;
Expand All @@ -109,13 +127,14 @@ pub fn get_image_filenames_for_directory(folder_path: &Path, favourites_file: Op
.collect::<Vec<PathBuf>>();
}

debug!("number of files: {}", dir_files.len());

// TODO: Are symlinks handled correctly?

let mut favourites: Vec<PathBuf> = favourites.into_iter().collect();

if randomize {
let mut rng = rand::thread_rng();
dir_files.shuffle(&mut rng);
favourites.shuffle(&mut rng);
} else {
dir_files.sort_unstable_by(|a, b| {
lexical_sort::natural_lexical_cmp(
Expand All @@ -129,6 +148,10 @@ pub fn get_image_filenames_for_directory(folder_path: &Path, favourites_file: Op
});
}

if let Some(every_n) = intersperse_with_favs_every_n {
dir_files = insert_after_every(dir_files, favourites, every_n);
}
debug!("number of files: {}", dir_files.len());
return Ok(dir_files);
}

Expand All @@ -138,7 +161,14 @@ pub fn find_first_image_in_directory(folder_path: &PathBuf) -> Result<PathBuf> {
if !folder_path.is_dir() {
bail!("This is not a folder");
};
get_image_filenames_for_directory(folder_path, None, false, false).map(|x| {
get_image_filenames_for_directory(
folder_path,
None,
false,
false,
None,
)
.map(|x| {
x.first()
.cloned()
.context("Folder does not have any supported images in it")
Expand All @@ -154,3 +184,28 @@ fn join_path_parts(path_with_tabs: String) -> PathBuf {

path
}

fn insert_after_every(main_vector: Vec<PathBuf>, other_vector: Vec<PathBuf>, after: usize) -> Vec<PathBuf> {
let mut result = Vec::with_capacity(main_vector.len() + other_vector.len());
let mut other_vector_i = 0;
let other_vector_set: HashSet<PathBuf> = other_vector.clone().into_iter().collect();

for (i, element) in main_vector.into_iter().enumerate() {
if other_vector_set.contains(&element) {
continue
}

result.push(element);
if other_vector_i < other_vector.len() && (i + 1) % after == 0 {
result.push(other_vector[other_vector_i].clone());
other_vector_i += 1;
}
}

while other_vector_i < other_vector.len() {
result.push(other_vector[other_vector_i].clone());
other_vector_i += 1;
}

result
}

0 comments on commit 88121e9

Please sign in to comment.