Compare commits

..

No commits in common. "8a10b627558af2f1b93581e510d417951e8dee5c" and "e64aee2b31ba684bc75391b943d455ac44a3566d" have entirely different histories.

View File

@ -1,11 +1,7 @@
#![feature(iter_map_windows)] #![feature(iter_map_windows)]
use iced::{ use iced::{
Element, Length, Task, clipboard, Element, Task, clipboard,
widget::{ widget::{self, button, column, combo_box, row, scrollable, text, text_input},
self, button, column, combo_box, row, scrollable,
scrollable::{Direction, Scrollbar},
text, text_input,
},
}; };
use std::{env, fs, path::PathBuf}; use std::{env, fs, path::PathBuf};
use texts::*; use texts::*;
@ -39,18 +35,19 @@ fn main() -> iced::Result {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Message { enum Message {
BibleSearchInput(usize, String), BibleSearchInput(String),
BibleSelected(usize, String), BibleSelected(String),
BibleSearchSubmit(usize), BibleSearchSubmit,
CopyText(usize), CopyText,
Clear(usize), Clear,
WordSearchInput(usize, String), WordSearchInput(String),
SubmitWordSearch(usize), SubmitWordSearch,
QuickSearch(usize, String), QuickSearch(String),
AddColRight, AddColRight,
} }
struct ColState { struct State {
files: combo_box::State<String>,
selected_file: Option<String>, selected_file: Option<String>,
bible_selected: Option<bible::Bible>, bible_selected: Option<bible::Bible>,
@ -59,29 +56,9 @@ struct ColState {
word_search: String, word_search: String,
word_search_results: Option<Vec<String>>, word_search_results: Option<Vec<String>>,
}
impl Default for ColState {
fn default() -> Self {
Self {
selected_file: None,
bible_selected: None,
bible_search: String::new(),
scripture_body: None,
word_search: String::new(),
word_search_results: None,
}
}
}
struct State {
files: combo_box::State<String>,
states: Vec<ColState>,
cols: usize, cols: usize,
} }
impl Default for State { impl Default for State {
fn default() -> Self { fn default() -> Self {
let files = if let Ok(contents) = fs::read_dir(BIBLE_DIRECTORY) { let files = if let Ok(contents) = fs::read_dir(BIBLE_DIRECTORY) {
@ -108,7 +85,15 @@ impl Default for State {
Self { Self {
files: combo_box::State::new(files), files: combo_box::State::new(files),
states: vec![ColState::default()], selected_file: None,
bible_selected: None,
bible_search: String::new(),
scripture_body: None,
word_search: String::new(),
word_search_results: None,
cols: 1, cols: 1,
} }
} }
@ -117,122 +102,110 @@ impl Default for State {
impl State { impl State {
fn update(&mut self, msg: Message) -> Task<Message> { fn update(&mut self, msg: Message) -> Task<Message> {
match msg { match msg {
Message::AddColRight => { Message::AddColRight => self.cols += 1,
self.cols += 1; Message::BibleSelected(file) => {
// clone this state into the new one
// self.states.push(self.states.into_iter().last().unwrap().clone())
self.states.push(ColState::default())
}
Message::BibleSelected(i, file) => {
if let Ok(contents) = fs::read_to_string(&file) { if let Ok(contents) = fs::read_to_string(&file) {
if let Ok(bible) = quick_xml::de::from_str::<bible::Bible>(&contents) { if let Ok(bible) = quick_xml::de::from_str::<bible::Bible>(&contents) {
// State is held technically in this variable, although // State is held technically in this variable, although
// the Bible is held in memory in it's own variable. // the Bible is held in memory in it's own variable.
self.states[i].selected_file = Some(format!("{bible}")); self.selected_file = Some(format!("{bible}"));
self.states[i].bible_selected = Some(bible); self.bible_selected = Some(bible);
// automatically focus the search bar when done here // automatically focus the search bar when done here
// return text_input::focus("bible-search"); return text_input::focus("bible-search");
} }
} }
} }
Message::QuickSearch(i, s) => { Message::QuickSearch(s) => {
self.states[i].bible_search = s; self.bible_search = s;
return self.update(Message::BibleSearchSubmit(i)); return self.update(Message::BibleSearchSubmit);
} }
Message::BibleSearchInput(i, query) => self.states[i].bible_search = query, Message::BibleSearchInput(query) => self.bible_search = query,
Message::WordSearchInput(i, query) => self.states[i].word_search = query, Message::WordSearchInput(query) => self.word_search = query,
Message::BibleSearchSubmit(i) => { Message::BibleSearchSubmit => {
let mut splits = self.states[i].bible_search.split_whitespace(); let mut splits = self.bible_search.split_whitespace();
let book = splits.next(); let book = splits.next();
let location = splits.next(); let location = splits.next();
if let (Some(book), Some(chap_and_ver), Some(bible)) = if let (Some(book), Some(chap_and_ver), Some(bible)) =
(book, location, &self.states[i].bible_selected) (book, location, &self.bible_selected)
{ {
self.states[i].scripture_body = bible::get(book, chap_and_ver, vec![bible]); self.scripture_body = bible::get(book, chap_and_ver, vec![bible]);
} }
} }
Message::SubmitWordSearch(i) => { Message::SubmitWordSearch => {
if let Some(bible) = &self.states[i].bible_selected { if let Some(bible) = &self.bible_selected {
let res = bible::search_for_word(&self.states[i].word_search, bible); let res = bible::search_for_word(&self.word_search, bible);
self.states[i].word_search_results = Some(res); self.word_search_results = Some(res);
} }
} }
Message::CopyText(i) => { Message::CopyText => {
if let Some(text) = &self.states[i].scripture_body { if let Some(text) = &self.scripture_body {
return clipboard::write::<Message>(parse(text)); return clipboard::write::<Message>(parse(text));
} }
} }
Message::Clear(i) => { Message::Clear => {
self.states[i].scripture_body = None; self.scripture_body = None;
self.states[i].word_search_results = None; self.word_search_results = None;
self.states[i].bible_search = String::new(); self.bible_search = String::new();
self.states[i].word_search = String::new(); self.word_search = String::new();
// return text_input::focus("bible-search"); return text_input::focus("bible-search");
} }
}; };
Task::none() Task::none()
} }
fn view(&self) -> Element<Message> { fn view(&self) -> Element<Message> {
scrollable(row![ let body = if let Some(body) = self.scripture_body.clone() {
row((0..self.cols).map(|col_index| { parse(&body)
} else {
String::new()
};
scrollable(row((0..self.cols).map(|_| {
row![
button("-"),
column![ column![
combo_box( combo_box(
&self.files, &self.files,
"Select Bible", "Select Bible",
self.states[col_index].selected_file.as_ref(), self.selected_file.as_ref(),
move |s| Message::BibleSelected(col_index, s) Message::BibleSelected
), ),
text_input( text_input("Search query, ie: Gen 1:1", &self.bible_search)
"Search query, ie: Gen 1:1", .on_input(Message::BibleSearchInput)
&self.states[col_index].bible_search .on_submit(Message::BibleSearchSubmit)
) .id("bible-search"),
.on_input(move |s| Message::BibleSearchInput(col_index, s)) widget::text_input("Word Search", &self.word_search)
.on_submit(Message::BibleSearchSubmit(col_index)), .on_input(Message::WordSearchInput)
widget::text_input("Word Search", &self.states[col_index].word_search) .on_submit(Message::SubmitWordSearch)
.on_input(move |s| Message::WordSearchInput(col_index, s)) .id("word-search"),
.on_submit(Message::SubmitWordSearch(col_index)),
row![ row![
button("Clear All").on_press_with(move || Message::Clear(col_index)), button("Clear All").on_press(Message::Clear),
button("Copy Scripture") button("Copy Scripture").on_press(Message::CopyText),
.on_press_with(move || Message::CopyText(col_index)),
], ],
row![ row![
scrollable( scrollable(column(
column( self.word_search_results
self.states[col_index] .clone()
.word_search_results .unwrap_or(Vec::new())
.clone() .into_iter()
.unwrap_or(Vec::new()) .map(|i| button(text(i.clone()))
.into_iter() .on_press_with(move || Message::QuickSearch(i.to_owned()))
.map(|i| button(text(i.clone())) .into())
.on_press_with(move || Message::QuickSearch( )),
col_index, scrollable(text(body.clone()))
i.to_owned()
))
.into())
)
.padding([5, 5])
),
scrollable(text(
if let Some(body) = self.states[col_index].scripture_body.clone() {
parse(&body)
} else {
String::new()
}
))
], ],
] ],
.padding([10, 5]) button(text("+").center())
.width(800.) .height(iced::Length::FillPortion(1))
.into() .on_press(Message::AddColRight)
})), ]
button(text("+").center()) .into()
.height(Length::Fixed(200.)) })))
.on_press(Message::AddColRight) .direction(scrollable::Direction::Both {
]) vertical: scrollable::Scrollbar::new(),
.direction(Direction::Horizontal(Scrollbar::new())) horizontal: scrollable::Scrollbar::new(),
})
.into() .into()
} }
} }