From 5e17ea2044db62586a993120e7a91d5b75da6062 Mon Sep 17 00:00:00 2001 From: Tyler Schmidt Date: Thu, 16 May 2024 10:39:34 -0700 Subject: [PATCH] Hook up Text Extraction Preferences and Change version to v1.0.0. --- CHANGELOG.md | 3 +- Cargo.lock | 49 +++++++++++++++-------------- Cargo.toml | 2 +- src/text/viewer.rs | 78 ++++++++++++++++++++++++++++++++++++++++------ src/ui/app.rs | 18 ++++++----- 5 files changed, 107 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f892cb2..a54e2e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,10 @@ - This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) - This project uses [ISO Standard](https://www.iso.org/iso-8601-date-and-time-format.html) date formatting -## Unreleased +## [1.0.0] - 2024-05-16 ### Added - Wayland support. +- Text Extraction Preferences. ### Changed - Updated FLTK and various developer dependencies. diff --git a/Cargo.lock b/Cargo.lock index e73bc92..9afda2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -269,9 +269,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", "windows-sys 0.52.0", @@ -285,22 +285,23 @@ checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" [[package]] name = "fltk" -version = "1.4.29" +version = "1.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1c0639e68289e58cdbc1cf93713b2e9b2aab551bedd4616ae51ce55aa8054e6" +checksum = "0bb1e40653f01b69606be720c2fab9fa75606f40c4f21c493529c0bd651e1a0d" dependencies = [ "bitflags 2.5.0", "crossbeam-channel", "fltk-sys", + "once_cell", "paste", "ttf-parser", ] [[package]] name = "fltk-sys" -version = "1.4.29" +version = "1.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4d8e7a6651c3a838f9dfffdfc415543aecb843f36d062bd2e3f6467121a5d65" +checksum = "5b0d1de52f091602e11fc767f9404d8a02049d89d8a7f45bfb614bffb69033cd" dependencies = [ "cmake", ] @@ -472,7 +473,7 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "narrative-director-rs" -version = "0.1.2" +version = "1.0.0" dependencies = [ "anyhow", "cpal", @@ -730,18 +731,18 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.200" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddc6f9cc94d67c0e21aaf7eda3a010fd3af78ebf6e096aa6e2e13c79749cce4f" +checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.200" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb" +checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" dependencies = [ "proc-macro2", "quote", @@ -750,9 +751,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.116" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" dependencies = [ "itoa", "ryu", @@ -761,9 +762,9 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "0.6.5" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0" dependencies = [ "serde", ] @@ -776,9 +777,9 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "syn" -version = "2.0.61" +version = "2.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c993ed8ccba56ae856363b1845da7266a7cb78e1d146c8a32d54b45a8b831fc9" +checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f" dependencies = [ "proc-macro2", "quote", @@ -819,21 +820,21 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.12" +version = "0.8.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" +checksum = "a4e43f8cc456c9704c851ae29c67e17ef65d2c30017c17a9765b89c382dc8bba" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.12", + "toml_edit 0.22.13", ] [[package]] name = "toml_datetime" -version = "0.6.5" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" dependencies = [ "serde", ] @@ -851,9 +852,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.12" +version = "0.22.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef" +checksum = "c127785850e8c20836d49732ae6abfa47616e60bf9d9f57c43c250361a9db96c" dependencies = [ "indexmap", "serde", diff --git a/Cargo.toml b/Cargo.toml index 64e73c9..3c3d9c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "narrative-director-rs" -version = "0.1.2" +version = "1.0.0" authors = ["Tyler Schmidt "] edition = "2021" license = "gpl-3.0" diff --git a/src/text/viewer.rs b/src/text/viewer.rs index e5f0d4e..6056d5a 100644 --- a/src/text/viewer.rs +++ b/src/text/viewer.rs @@ -87,19 +87,20 @@ impl ParagraphViewer { } } - pub fn load_paragraphs(&mut self, text_file_path: PathBuf) { + pub fn load_paragraphs(&mut self, text_file_path: PathBuf, delimiters: &str, amount: usize) { let mut text_file = File::open(text_file_path).expect("Could not load file."); let mut whole_text_content = String::new(); text_file .read_to_string(&mut whole_text_content) .expect("Could not read text file."); + let delimiter_tokens = delimiters.chars().collect::>(); let split_paragraphs: Vec<&str> = whole_text_content - .split_inclusive(|character| character == '.' || character == '?' || character == '!') + .split_inclusive(&*delimiter_tokens) .collect(); self.paragraphs = split_paragraphs - .chunks(4) + .chunks(amount) .map(|sentences| sentences.concat()) .collect(); @@ -108,6 +109,32 @@ impl ParagraphViewer { self.progress_counter.update(); } + /// Changes currently loaded text to be split by the provided + /// delimiters. + pub fn reload_text_with(&mut self, delimiters: &str, amount: usize) { + let existing_text = self.paragraphs.join(""); + + let delimiter_tokens = delimiters.chars().collect::>(); + let new_splitted_text: Vec<&str> = + existing_text.split_inclusive(&*delimiter_tokens).collect(); + + let new_chunked_text: Vec = new_splitted_text + .chunks(amount) + .map(|line| line.concat()) + .collect(); + + if new_chunked_text == self.paragraphs { + return; + } + + self.paragraphs = new_chunked_text; + + self.progress_counter.set_current(0); + self.progress_counter.set_total(self.paragraphs.len()); + self.progress_counter.update(); + self.show_paragraph_at(0); + } + pub fn show_next_paragraph(&mut self) { self.paragraph_num += 1; @@ -183,6 +210,9 @@ mod tests { use std::io::Write; use tempfile::NamedTempFile; + const DELIMITERS: &str = ".?!"; + const GATHERING_AMOUNT: usize = 4; + const FIRST_PARAGRAPH: &str = "This is the first paragraph. It will eventually contain four sentences. I'm serious! Okay, here is the last sentence."; const SECOND_PARAGRAPH: &str = "This is the second paragraph. It will also contain four sentences. This paragraph is similar to the first one. It really is?"; @@ -246,7 +276,11 @@ mod tests { #[test] fn goto_exceeds_paragraphs() { let mut paragraph_viewer = get_paragraph_viewer(); - paragraph_viewer.load_paragraphs(get_file_many_paragraphs().path().to_path_buf()); + paragraph_viewer.load_paragraphs( + get_file_many_paragraphs().path().to_path_buf(), + DELIMITERS, + GATHERING_AMOUNT, + ); assert_eq!(MANY_PARAGRAPHS_LEN, paragraph_viewer.num_paragraphs()); let goto_paragraph_num = 3; @@ -260,7 +294,11 @@ mod tests { #[test] fn goto_paragraph_exists() { let mut paragraph_viewer = get_paragraph_viewer(); - paragraph_viewer.load_paragraphs(get_file_many_paragraphs().path().to_path_buf()); + paragraph_viewer.load_paragraphs( + get_file_many_paragraphs().path().to_path_buf(), + DELIMITERS, + GATHERING_AMOUNT, + ); assert_eq!(MANY_PARAGRAPHS_LEN, paragraph_viewer.num_paragraphs()); let goto_paragraph_num = 1; @@ -291,7 +329,11 @@ mod tests { #[test] fn next_exceeds_paragraphs() { let mut paragraph_viewer = get_paragraph_viewer(); - paragraph_viewer.load_paragraphs(get_file_one_paragraph().path().to_path_buf()); + paragraph_viewer.load_paragraphs( + get_file_one_paragraph().path().to_path_buf(), + DELIMITERS, + GATHERING_AMOUNT, + ); assert_eq!(1, paragraph_viewer.num_paragraphs()); paragraph_viewer.show_paragraph_at(0); @@ -308,7 +350,11 @@ mod tests { #[test] fn next_paragraph_exists() { let mut paragraph_viewer = get_paragraph_viewer(); - paragraph_viewer.load_paragraphs(get_file_many_paragraphs().path().to_path_buf()); + paragraph_viewer.load_paragraphs( + get_file_many_paragraphs().path().to_path_buf(), + DELIMITERS, + GATHERING_AMOUNT, + ); assert_eq!(MANY_PARAGRAPHS_LEN, paragraph_viewer.num_paragraphs()); paragraph_viewer.show_paragraph_at(0); @@ -341,7 +387,11 @@ mod tests { #[test] fn previous_negative_paragraphs() { let mut paragraph_viewer = get_paragraph_viewer(); - paragraph_viewer.load_paragraphs(get_file_one_paragraph().path().to_path_buf()); + paragraph_viewer.load_paragraphs( + get_file_one_paragraph().path().to_path_buf(), + DELIMITERS, + GATHERING_AMOUNT, + ); assert_eq!(1, paragraph_viewer.num_paragraphs()); paragraph_viewer.show_paragraph_at(0); @@ -358,7 +408,11 @@ mod tests { #[test] fn previous_paragraph_exists() { let mut paragraph_viewer = get_paragraph_viewer(); - paragraph_viewer.load_paragraphs(get_file_many_paragraphs().path().to_path_buf()); + paragraph_viewer.load_paragraphs( + get_file_many_paragraphs().path().to_path_buf(), + DELIMITERS, + GATHERING_AMOUNT, + ); assert_eq!(MANY_PARAGRAPHS_LEN, paragraph_viewer.num_paragraphs()); paragraph_viewer.show_paragraph_at(MANY_PARAGRAPHS_LEN - 1); @@ -379,7 +433,11 @@ mod tests { #[test] fn shows_paragraph() { let mut paragraph_viewer = get_paragraph_viewer(); - paragraph_viewer.load_paragraphs(get_file_one_paragraph().path().to_path_buf()); + paragraph_viewer.load_paragraphs( + get_file_one_paragraph().path().to_path_buf(), + DELIMITERS, + GATHERING_AMOUNT, + ); assert_eq!(1, paragraph_viewer.num_paragraphs()); paragraph_viewer.show_paragraph_at(0); diff --git a/src/ui/app.rs b/src/ui/app.rs index c93caa1..6c789f4 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -165,7 +165,11 @@ impl MainApplication { let session = Session::load(file_location.clone()) .unwrap_or_else(|| Session::new(file_location.clone())); - self.paragraph_viewer.load_paragraphs(file_location); + self.paragraph_viewer.load_paragraphs( + file_location, + &session.gathering_delimiters(), + session.gathering_amount(), + ); self.paragraph_viewer .show_paragraph_at(session.paragraph_num()); @@ -228,13 +232,13 @@ impl MainApplication { // TODO: Split session into AudioPreferences, TextPreferences, and Session. // That way, users can use the Preferences dialog without needing an existing // session open. - if self.session.is_some() { - self.preferences_dialog.show( - self.session.as_mut().expect( - "Session is needed to fetch current audio information.", - ), - ); + if let Some(session) = self.session.as_mut() { + self.preferences_dialog.show(session); + self.paragraph_viewer.reload_text_with( + &session.gathering_delimiters(), + session.gathering_amount(), + ); self.load_audio_file(); } }