diff --git a/src/main.rs b/src/main.rs index ade4dc8..37431e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,105 @@ -use std::env; +use iced::{ + Alignment::Center, + Element, + widget::{self, Column, column, combo_box, text}, +}; +use std::{env, fmt::Display, fs, path::PathBuf}; +use texts::*; -mod strong; -mod bible; +mod texts; -fn main() { +pub const BIBLE_DIRECTORY: &str = "./Holy-Bible-XML-Format"; + +fn main() -> iced::Result { let arg = env::args().collect::>(); if let Some(query) = arg.get(1) { if query.starts_with("H") | query.starts_with("h") { if let Some(found) = strong::get(query) { print!("{found}"); - return; + return Ok(()); } - } - if let Some(second_query) = arg.get(2) { - bible::get(query,second_query); + } + if let Some(second_query) = arg.get(2) { + bible::get( + query, + second_query, + vec![format!("{BIBLE_DIRECTORY}/EnglishNASBBible.xml").into()], + ); + } + } + + iced::run("A cool counter", State::update, State::view) +} + +#[derive(Debug, Clone)] +enum Message { + BookSelected(&'static str), + BibleSelected(String), +} + +struct State { + book: combo_box::State<&'static str>, + selected: Option<&'static str>, + files: combo_box::State, + file_selected: Option, +} +impl Default for State { + fn default() -> Self { + let files = if let Ok(contents) = fs::read_dir(BIBLE_DIRECTORY) { + let paths = contents + .into_iter() + .filter_map(|f| f.ok()) + .map(|f| (f.path(), f.file_type())) + .filter(|(_, f)| f.is_ok()) + .map(|(a, b)| (a, b.unwrap())) + .filter(|(_, f)| f.is_file()) + .map(|(f, _)| f) + .filter(|f| f.extension().is_some()) + .filter(|f| f.extension().unwrap() == "xml") + .collect::>(); + paths + } else { + todo!() + }; + let files = files + .iter() + .filter_map(|f| f.to_str()) + .map(|f| f.to_owned()) + .collect::>(); + + Self { + files: combo_box::State::new(files), + book: combo_box::State::new(texts::bible::BOOKS_IN_ORDER.to_vec()), + selected: None, + file_selected: None, } } } + +impl State { + fn update(&mut self, msg: Message) { + match msg { + Message::BibleSelected(file) => self.file_selected = Some(file), + Message::BookSelected(version) => self.selected = Some(version), + } + } + + fn view(&self) -> Element { + column![ + widget::combo_box( + &self.files, + "Select Bible (type)", + self.file_selected.as_ref(), + Message::BibleSelected + ), + widget::combo_box( + &self.book, + "Select Book", + self.selected.as_ref(), + Message::BookSelected, + ), + + ] + .into() + } +} diff --git a/src/bible.rs b/src/texts/bible.rs similarity index 95% rename from src/bible.rs rename to src/texts/bible.rs index 9a09474..816418e 100644 --- a/src/bible.rs +++ b/src/texts/bible.rs @@ -1,10 +1,10 @@ use inline_colorization::*; -use std::{fmt::Display, fs}; +use std::{fmt::Display, fs, path::PathBuf}; use quick_xml::de::from_str; use serde::Deserialize; -const BOOKS_IN_ORDER: [&str; 66] = [ +pub const BOOKS_IN_ORDER: [&str; 66] = [ "Genesis", "Exodus", "Leviticus", @@ -73,20 +73,14 @@ const BOOKS_IN_ORDER: [&str; 66] = [ "Revelation", ]; -// This would eventaully be a list built by the user -const BIBLES: [&str; 2] = [ - "/usr/local/bible/EnglishNASBBible.xml", - "/usr/local/bible/EnglishNIVBible.xml", -]; - -pub fn get(query: &str, loc: &str) { +pub fn get(query: &str, location: &str, files: Vec) { // expecting query to be in format: // Gen 1:2 // Gen 1:2-5 // Gen 1 // The book name needs to be just long enough to be unique - let mut splits = loc.split(':'); + let mut splits = location.split(':'); let chapter = splits.next(); let verse = splits.next(); @@ -114,7 +108,7 @@ pub fn get(query: &str, loc: &str) { }; // Load Bibles into memory - let bibles = BIBLES + let bibles = files .iter() .filter_map(|path| fs::read_to_string(path).ok()) .filter_map(|contents| from_str::(&contents).ok()) diff --git a/src/texts/mod.rs b/src/texts/mod.rs new file mode 100644 index 0000000..a6a609c --- /dev/null +++ b/src/texts/mod.rs @@ -0,0 +1,2 @@ +pub mod bible; +pub mod strong; \ No newline at end of file diff --git a/src/strong.rs b/src/texts/strong.rs similarity index 100% rename from src/strong.rs rename to src/texts/strong.rs