From 81f73ac7a9ed5c295aaf84f517d447864c08b7df Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 21 Feb 2022 23:58:10 -0500 Subject: [PATCH] Add support for a very basic randomize. --- Cargo.toml | 6 +++++- src/main.rs | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 05ef905..28b2e70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,9 @@ name = "record_watch" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[[bin]] +name = "rw" +path = "src/main.rs" [dependencies] walkdir = "2.3.2" @@ -13,3 +15,5 @@ dirs = "4.0.0" serde_json = "1.0.79" serde = { version = "1.0.136", features= ["derive"] } bytes = "1.1.0" +rand = "0.8.5" + diff --git a/src/main.rs b/src/main.rs index 01bce03..20df4d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,24 @@ use walkdir::DirEntry; use walkdir::WalkDir; mod mpvctl; use crate::mpvctl::MpvCtl; +use rand::seq::SliceRandom; +use rand::thread_rng; use tokio::time::{sleep, Duration}; #[tokio::main] async fn main() -> Result<(), Box> { - let mut args = env::args(); + let mut args = env::args().peekable(); let _program = args.next().expect("argv[0] should be this program?"); + let randomize: bool = match args.peek() { + Some(flag) if flag == "-r" => { + // Consume the flag + args.next().expect("We already know this arg exists"); + true + } + _ => false, + }; let profile = args.next().expect("Must provide a profile"); + let directories: Vec = args.collect(); let mut files: Vec = Vec::new(); @@ -58,6 +69,9 @@ async fn main() -> Result<(), Box> { let mut mpvctl = MpvCtl::connect("/tmp/recordwatchsocket").await?; let mut end_file_listener = mpvctl.listen(&["end-file"])?; + if randomize { + files.shuffle(&mut thread_rng()); + } for f in files { let canonicalized_path = f.path().canonicalize()?; let already_watched_count: i64 = sqlx::query(r#"SELECT count(*) FROM watched WHERE path = ? AND profile = (SELECT id FROM PROFILE WHERE name = ?)"#)