Skip to content

Commit

Permalink
read favourites file
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrabovets committed Jul 9, 2023
1 parent 7532358 commit f83f653
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
check:
strategy:
matrix:
os: [ubuntu-latest, ubuntu-20.04]
os: [ubuntu-20.04]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
check:
strategy:
matrix:
os: [windows-latest, windows-2019]
os: [windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
16 changes: 13 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fast_image_resize = "2.7"
gif = "0.12"
gif-dispose = "4"
image = "0.24"
itertools = "0.11.0"
kamadak-exif = "0.5"
lexical-sort = "0.3"
libavif-image = {version = "0.10", optional = true}
Expand Down
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ package() {
install -Dm644 res/oculante.desktop "${pkgdir}/usr/share/applications/${pkgname}.desktop"
install -Dm644 LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}"
install -Dm644 README.md -t "${pkgdir}/usr/share/doc/${pkgname}"
}
}
17 changes: 13 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use clap::Arg;
use clap::Command;
use itertools::Itertools;
use log::debug;
use log::error;
use log::info;
Expand Down Expand Up @@ -48,6 +49,7 @@ mod image_editing;
pub mod paint;

pub const FONT: &[u8; 309828] = include_bytes!("../res/fonts/Inter-Regular.ttf");
const FAVOURITES_FILE: &str = "favourites.txt";

#[notan_main]
fn main() -> Result<(), String> {
Expand Down Expand Up @@ -663,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, false, false);
state.scrubber = scrubber::Scrubber::new(p, None, false, false);
state.scrubber.wrap = state.persistent_settings.wrap_folder;

// debug!("{:#?} from {}", &state.scrubber, p.display());
Expand Down Expand Up @@ -1073,7 +1075,12 @@ fn browse_for_folder_path(state: &mut OculanteState) {
_ = state.persistent_settings.save();
state.folder_selected = Option::from(folder_path.clone());

state.scrubber = Scrubber::new(folder_path.as_path(), true, true);
state.scrubber = Scrubber::new(
folder_path.as_path(),
Some(FAVOURITES_FILE),
true,
true,
);
let current_path = state.scrubber.next();

state.is_loaded = false;
Expand Down Expand Up @@ -1128,7 +1135,7 @@ fn add_to_favourites(state: &OculanteState) {
state.folder_selected
.as_ref()
.unwrap_or(&img_path.parent().unwrap().to_path_buf())
.join(Path::new("favourites.txt"))
.join(Path::new(FAVOURITES_FILE))
)
.expect("Unable to open file");

Expand All @@ -1137,7 +1144,9 @@ fn add_to_favourites(state: &OculanteState) {
"{}",
img_path.strip_prefix(state.folder_selected.as_ref().unwrap().as_path())
.unwrap()
.to_string_lossy()
.components()
.map(|component| component.as_os_str().to_str().unwrap())
.join("\t")
).expect("Unable to write data");
}
}
38 changes: 34 additions & 4 deletions src/scrubber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::utils::is_ext_compatible;
use anyhow::{bail, Context, Result};
use log::debug;
use rand::seq::SliceRandom;
use std::collections::HashSet;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

Expand All @@ -13,8 +15,8 @@ pub struct Scrubber {
}

impl Scrubber {
pub fn new(path: &Path, randomize: bool, walk_files: bool) -> Self {
let entries = get_image_filenames_for_directory(path, randomize, walk_files)
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)
.unwrap_or_default();
let index = entries.iter().position(|p| p == path).unwrap_or_default();
Self {
Expand Down Expand Up @@ -64,7 +66,7 @@ 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, 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) -> Result<Vec<PathBuf>> {
let mut folder_path = folder_path.to_path_buf();
if folder_path.is_file() {
folder_path = folder_path
Expand All @@ -73,6 +75,22 @@ pub fn get_image_filenames_for_directory(folder_path: &Path, randomize: bool, wa
.context("Can't get parent")?;
}

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
.lines()
.filter_map(|line| line.ok())
.map(|file_str| folder_path.join(join_path_parts(file_str)))
.filter(|file| file.exists())
.collect();
}
}

let mut dir_files: Vec<PathBuf>;

if walk_files {
Expand All @@ -91,6 +109,8 @@ pub fn get_image_filenames_for_directory(folder_path: &Path, randomize: bool, wa
.collect::<Vec<PathBuf>>();
}

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

// TODO: Are symlinks handled correctly?

if randomize {
Expand Down Expand Up @@ -118,9 +138,19 @@ 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, false, false).map(|x| {
get_image_filenames_for_directory(folder_path, None, false, false).map(|x| {
x.first()
.cloned()
.context("Folder does not have any supported images in it")
})?
}

fn join_path_parts(path_with_tabs: String) -> PathBuf {
let mut path = PathBuf::new();

for part in path_with_tabs.split("\t") {
path.push(part);
}

path
}

0 comments on commit f83f653

Please sign in to comment.