From e64aee2b31ba684bc75391b943d455ac44a3566d Mon Sep 17 00:00:00 2001 From: rushmore75 Date: Thu, 27 Mar 2025 00:54:58 -0600 Subject: [PATCH] checkpoint --- src/main.rs | 108 +++++++++++++++++++++++++++++++-------------- src/texts/bible.rs | 59 ++++++++++++++++--------- 2 files changed, 114 insertions(+), 53 deletions(-) diff --git a/src/main.rs b/src/main.rs index 24e33d1..0b36ec9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![feature(iter_map_windows)] use iced::{ - clipboard, widget::{self, column, combo_box, row, text_input}, Element, Task + Element, Task, clipboard, + widget::{self, button, column, combo_box, row, scrollable, text, text_input}, }; use std::{env, fs, path::PathBuf}; use texts::*; @@ -24,7 +25,7 @@ fn main() -> iced::Result { // ) { // println!("{}", f); // } - return Ok(()) + return Ok(()); } } @@ -41,6 +42,8 @@ enum Message { Clear, WordSearchInput(String), SubmitWordSearch, + QuickSearch(String), + AddColRight, } struct State { @@ -49,9 +52,12 @@ struct State { bible_selected: Option, bible_search: String, - body: Option, + scripture_body: Option, word_search: String, + word_search_results: Option>, + + cols: usize, } impl Default for State { fn default() -> Self { @@ -83,9 +89,12 @@ impl Default for State { bible_selected: None, bible_search: String::new(), - body: None, + scripture_body: None, word_search: String::new(), + word_search_results: None, + + cols: 1, } } } @@ -93,6 +102,7 @@ impl Default for State { impl State { fn update(&mut self, msg: Message) -> Task { match msg { + Message::AddColRight => self.cols += 1, Message::BibleSelected(file) => { if let Ok(contents) = fs::read_to_string(&file) { if let Ok(bible) = quick_xml::de::from_str::(&contents) { @@ -105,6 +115,10 @@ impl State { } } } + Message::QuickSearch(s) => { + self.bible_search = s; + return self.update(Message::BibleSearchSubmit); + } Message::BibleSearchInput(query) => self.bible_search = query, Message::WordSearchInput(query) => self.word_search = query, Message::BibleSearchSubmit => { @@ -115,20 +129,25 @@ impl State { if let (Some(book), Some(chap_and_ver), Some(bible)) = (book, location, &self.bible_selected) { - self.body = bible::get(book, chap_and_ver, vec![bible]); + self.scripture_body = bible::get(book, chap_and_ver, vec![bible]); } } Message::SubmitWordSearch => { - println!("todo: search for {}", self.word_search); + if let Some(bible) = &self.bible_selected { + let res = bible::search_for_word(&self.word_search, bible); + self.word_search_results = Some(res); + } } Message::CopyText => { - if let Some(text) = &self.body { + if let Some(text) = &self.scripture_body { return clipboard::write::(parse(text)); } } Message::Clear => { - self.body = None; + self.scripture_body = None; + self.word_search_results = None; self.bible_search = String::new(); + self.word_search = String::new(); return text_input::focus("bible-search"); } }; @@ -136,33 +155,57 @@ impl State { } fn view(&self) -> Element { - let msg = if let Some(body) = self.body.clone() { + let body = if let Some(body) = self.scripture_body.clone() { parse(&body) } else { String::new() }; - column![ - widget::combo_box( - &self.files, - "Select Bible", - self.selected_file.as_ref(), - Message::BibleSelected - ), - widget::text_input("Search query, ie: Gen 1:1", &self.bible_search) - .on_input(Message::BibleSearchInput) - .on_submit(Message::BibleSearchSubmit) - .id("bible-search"), - widget::text_input("Word Search", &self.word_search) - .on_input(Message::WordSearchInput) - .on_submit(Message::SubmitWordSearch) - .id("word-search"), + scrollable(row((0..self.cols).map(|_| { row![ - widget::button("Copy").on_press(Message::CopyText), - widget::button("Clear").on_press(Message::Clear), - ], - widget::text(msg), - ] + button("-"), + column![ + combo_box( + &self.files, + "Select Bible", + self.selected_file.as_ref(), + Message::BibleSelected + ), + text_input("Search query, ie: Gen 1:1", &self.bible_search) + .on_input(Message::BibleSearchInput) + .on_submit(Message::BibleSearchSubmit) + .id("bible-search"), + widget::text_input("Word Search", &self.word_search) + .on_input(Message::WordSearchInput) + .on_submit(Message::SubmitWordSearch) + .id("word-search"), + row![ + button("Clear All").on_press(Message::Clear), + button("Copy Scripture").on_press(Message::CopyText), + ], + row![ + scrollable(column( + self.word_search_results + .clone() + .unwrap_or(Vec::new()) + .into_iter() + .map(|i| button(text(i.clone())) + .on_press_with(move || Message::QuickSearch(i.to_owned())) + .into()) + )), + scrollable(text(body.clone())) + ], + ], + button(text("+").center()) + .height(iced::Length::FillPortion(1)) + .on_press(Message::AddColRight) + ] + .into() + }))) + .direction(scrollable::Direction::Both { + vertical: scrollable::Scrollbar::new(), + horizontal: scrollable::Scrollbar::new(), + }) .into() } } @@ -238,12 +281,11 @@ fn parse(input: &str) -> String { to_remove.reverse(); let mut modified = input.to_owned(); - for (s,e) in to_remove { + for (s, e) in to_remove { let front = modified[..s].to_string(); - let back = modified[e+1..].to_string(); + let back = modified[e + 1..].to_string(); modified = front + &back; } - + modified } - diff --git a/src/texts/bible.rs b/src/texts/bible.rs index b49533f..19f9c38 100644 --- a/src/texts/bible.rs +++ b/src/texts/bible.rs @@ -12,12 +12,12 @@ pub const BOOKS_IN_ORDER: [&str; 66] = [ "Joshua", "Judges", "Ruth", - "1 Samuel", - "2 Samuel", - "1 Kings", - "2 Kings", - "1 Chronicles", - "2 Chronicles", + "1-Samuel", + "2-Samuel", + "1-Kings", + "2-Kings", + "1-Chronicles", + "2-Chronicles", "Ezra", "Nehemiah", "Esther", @@ -25,7 +25,7 @@ pub const BOOKS_IN_ORDER: [&str; 66] = [ "Psalms", "Proverbs", "Ecclesiastes", - "Song of Solomon", + "Song-of-Solomon", "Isaiah", "Jeremiah", "Lamentations", @@ -49,31 +49,43 @@ pub const BOOKS_IN_ORDER: [&str; 66] = [ "John", "Acts", "Romans", - "1 Corinthians", - "2 Corinthians", + "1-Corinthians", + "2-Corinthians", "Galations", "Ephesians", "Philippians", "Colossians", - "1 Thessalonians", - "2 Thessalonians", - "1 Timothy", - "2 Timothy", + "1-Thessalonians", + "2-Thessalonians", + "1-Timothy", + "2-Timothy", "Titus", "Philemon", "Hebrews", "James", - "1 Peter", - "2 Peter", - "1 John", - "2 John", - "3 John", + "1-Peter", + "2-Peter", + "1-John", + "2-John", + "3-John", "Jude", "Revelation", ]; pub fn search_for_word(word: &str, from_files: &Bible) -> Vec { - todo!() + let mut found = Vec::new(); + for test in &from_files.testaments { + for book in &test.books { + for chapter in &book.chapters { + for verse in &chapter.verses { + if verse.text.contains(word) { + found.push(format!("{} {}:{}", BOOKS_IN_ORDER[book.number-1], chapter.number, verse.number)); + } + } + } + } + } + found } pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option { @@ -92,7 +104,14 @@ pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option>(); let (book_idx, book_name) = match res.len() {