Tabbing now works, impelemented preliminary error message handling

This commit is contained in:
Oliver Atkinson 2025-04-08 12:13:19 -06:00
parent 727b2f0b51
commit acb49d7a4d
2 changed files with 66 additions and 113 deletions

View File

@ -1,7 +1,7 @@
#![feature(iter_map_windows)] #![feature(iter_map_windows)]
use iced::keyboard::key::Named; use iced::keyboard::key::Named;
use iced::keyboard::{Key, Modifiers}; use iced::keyboard::{Key, Modifiers};
use iced::{event, task, Event, Subscription}; use iced::{event, Event, Subscription};
use iced::widget::scrollable; use iced::widget::scrollable;
use iced::{ use iced::{
Element, Length, Task, Theme, clipboard, color, Element, Length, Task, Theme, clipboard, color,
@ -12,6 +12,7 @@ use iced::{
}, },
}; };
use std::sync::Arc;
use std::{env, fs, path::PathBuf}; use std::{env, fs, path::PathBuf};
use texts::*; use texts::*;
@ -32,7 +33,7 @@ fn main() -> iced::Result {
if let Some(second_query) = arg.get(2) { if let Some(second_query) = arg.get(2) {
if let Ok(contents) = fs::read_to_string(&format!("{BIBLE_DIRECTORY}/EnglishNASBBible.xml")) { if let Ok(contents) = fs::read_to_string(&format!("{BIBLE_DIRECTORY}/EnglishNASBBible.xml")) {
if let Ok(bible) = quick_xml::de::from_str::<bible::Bible>(&contents) { if let Ok(bible) = quick_xml::de::from_str::<bible::Bible>(&contents) {
if let Some(f) = bible::get( if let Ok(f) = bible::get(
query, query,
second_query, second_query,
vec![&bible], vec![&bible],
@ -70,10 +71,8 @@ enum Message {
QuickSearch(usize, String, usize), QuickSearch(usize, String, usize),
AddColRight, AddColRight,
DeleteColumn(usize), DeleteColumn(usize),
ErrorSelectingBible(usize), SetErrorMessage(usize, String),
RecievedEvent(Event), RecievedEvent(Event),
ColSelected(usize),
ColUnselected,
} }
struct ColState { struct ColState {
@ -106,46 +105,11 @@ impl Default for ColState {
} }
} }
#[derive(PartialEq, Eq)]
enum SelectedInputBox {
Bible,
VerseSearch,
WordSearch,
}
impl SelectedInputBox {
fn in_order() -> Vec<Self> {
vec![
SelectedInputBox::Bible,
SelectedInputBox::VerseSearch,
SelectedInputBox::WordSearch
]
}
fn next(current: &Option<Self>) -> Self {
if let Some(input) = current {
if let Some(found) = Self::in_order().into_iter().find(|a| a==input) {
return found
}
}
// I don't see why this would panic
Self::in_order().into_iter().next().unwrap()
}
fn as_id_prefix(&self) -> &str {
// magic values that relate to id() as setup in the GUI
match self {
SelectedInputBox::Bible => "",
SelectedInputBox::VerseSearch => "verse-",
SelectedInputBox::WordSearch => "word-",
}
}
}
struct State { struct State {
files: combo_box::State<String>, files: combo_box::State<String>,
states: Vec<ColState>, states: Vec<ColState>,
cols: usize, cols: usize,
/// Incase an event needs a best guess column tab_index: Option<i32>,
last_selected_column: Option<usize>,
last_selected_input: Option<SelectedInputBox>
} }
impl Default for State { impl Default for State {
@ -176,8 +140,7 @@ impl Default for State {
files: combo_box::State::new(files), files: combo_box::State::new(files),
states: vec![ColState::default()], states: vec![ColState::default()],
cols: 1, cols: 1,
last_selected_column: None, tab_index: None,
last_selected_input: None,
} }
} }
} }
@ -189,51 +152,27 @@ impl State {
} }
fn update(&mut self, msg: Message) -> Task<Message> { fn update(&mut self, msg: Message) -> Task<Message> {
// handle tab ordering
match msg {
Message::WordSearchInput(_, _) => self.last_selected_input = Some(SelectedInputBox::WordSearch),
Message::BibleSearchInput(_, _) => self.last_selected_input = Some(SelectedInputBox::VerseSearch),
Message::DeleteColumn(_) => self.last_selected_input = None,
_ => {}
}
// try to set last_selected_column for messages with side effects
match msg {
// All events that give column index
Message::ErrorSelectingBible(i) |
Message::BibleSelected(i, _) |
Message::QuickSearch(i, _, _) |
Message::BibleSearchInput(i,_) |
Message::WordSearchInput(i, _) |
Message::BibleSearchSubmit(i) |
Message::SubmitWordSearch(i) |
Message::CopyText(i) |
Message::Clear(i)
=> self.last_selected_column = Some(i),
Message::DeleteColumn(_) => self.last_selected_column = None,
_ => {}
}
// match normal messages // match normal messages
match msg { match msg {
Message::ColSelected(i) => self.last_selected_column = Some(i),
Message::ColUnselected => self.last_selected_column = None,
Message::RecievedEvent(event) => { Message::RecievedEvent(event) => {
if let Event::Keyboard(kbd) =event { if let Event::Keyboard(kbd) = event {
if let iced::keyboard::Event::KeyReleased {key, modifiers, ..} = kbd { if let iced::keyboard::Event::KeyReleased {key, modifiers, ..} = kbd {
if let Key::Named(n) = key { if let Key::Named(n) = key {
if let Named::Tab = n { if let Named::Tab = n {
if let Some(i) = self.last_selected_column { if let Some(idx) = self.tab_index {
if modifiers == Modifiers::SHIFT { let new_idx = if modifiers == Modifiers::SHIFT { idx-1 } else { idx+1 };
// Shirft + Tab
let next = SelectedInputBox::next(&self.last_selected_input);
let id = next.as_id_prefix();
return text_input::focus(format!("{id}{}", i));
} else {
// Tab
// TODO prev // 2 because two buttons on each col
return text_input::focus(format!("verse-{}", i)); let new_idx = new_idx.clamp(0, (self.cols*2) as i32 -1);
self.tab_index = Some(new_idx);
let id = format!("tabId-{}", new_idx);
return text_input::focus(id);
} else {
if self.cols > 0 {
self.tab_index = Some(0);
return text_input::focus("tabId-0");
} }
} }
} }
@ -241,8 +180,8 @@ impl State {
} }
} }
} }
Message::ErrorSelectingBible(i) => { Message::SetErrorMessage(i, m) => {
self.states[i].error = Some(String::new()); self.states[i].error = Some(m);
} }
Message::DeleteColumn(idx) => { Message::DeleteColumn(idx) => {
self.cols -= 1; self.cols -= 1;
@ -263,18 +202,18 @@ impl State {
// 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.states[i].selected_file = Some(format!("{bible}"));
self.states[i].bible_selected = Some(bible); self.states[i].bible_selected = Some(bible);
// automatically focus the search bar when done here self.states[i].error = None;
// return text_input::focus("bible-search");
} }
Err(err) => { Err(err) => {
eprintln!("{}", err); eprintln!("{}", err);
return self.update(Message::ErrorSelectingBible(i)); return self.update(Message::SetErrorMessage(i, err.to_string()));
}, },
} }
}, },
Err(err) => { Err(err) => {
eprintln!("{}", err); eprintln!("{}", err);
return self.update(Message::ErrorSelectingBible(i)); return self.update(Message::SetErrorMessage(i, err.to_string()));
} }
} }
} }
@ -286,12 +225,8 @@ impl State {
} }
return self.update(Message::BibleSearchSubmit(i)); return self.update(Message::BibleSearchSubmit(i));
} }
Message::BibleSearchInput(i, query) => { Message::BibleSearchInput(i, query) => self.states[i].bible_search = query,
self.states[i].bible_search = query Message::WordSearchInput(i, query) => self.states[i].word_search = query,
},
Message::WordSearchInput(i, query) => {
self.states[i].word_search = query
},
Message::BibleSearchSubmit(i) => { Message::BibleSearchSubmit(i) => {
let mut splits = self.states[i].bible_search.split_whitespace(); let mut splits = self.states[i].bible_search.split_whitespace();
let book = splits.next(); let book = splits.next();
@ -300,7 +235,25 @@ impl State {
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.states[i].bible_selected)
{ {
self.states[i].scripture_body = bible::get(book, chap_and_ver, vec![bible]); self.states[i].scripture_body = match bible::get(book, chap_and_ver, vec![bible]) {
Ok(s) => {
self.states[i].error = None;
Some(s)
},
Err(s) => {
return self.update(Message::SetErrorMessage(i, s));
},
};
} else {
if let None = book {
return self.update(Message::SetErrorMessage(i, format!("Error selecting book {:?}", book)));
}
if let None = location {
return self.update(Message::SetErrorMessage(i, format!("Error getting chapter and/or verse {:?}", location)));
}
if let None = &self.states[i].bible_selected {
return self.update(Message::SetErrorMessage(i, format!("Error getting selected bible")));
}
} }
} }
Message::SubmitWordSearch(i) => { Message::SubmitWordSearch(i) => {
@ -336,26 +289,21 @@ impl State {
row((0..self.cols).map(|col_index| { row((0..self.cols).map(|col_index| {
column![ column![
// header // header
button("Delete Column")
.on_press_with(move || Message::DeleteColumn(col_index))
.width(Length::Fill)
.style(button::danger),
combo_box( combo_box(
&self.files, &self.files,
"Select Bible", "Select Bible",
self.states[col_index].selected_file.as_ref(), self.states[col_index].selected_file.as_ref(),
move |s| Message::BibleSelected(col_index, s) move |s| Message::BibleSelected(col_index, s)
) ),
.on_open(Message::ColSelected(col_index)),
text_input( text_input(
"Search query, ie: Gen 1:1", "Search query, ie: Gen 1:1",
&self.states[col_index].bible_search &self.states[col_index].bible_search
) )
.id(format!("verse-{}", col_index)) .id(format!("tabId-{}", (col_index*2)+0))
.on_input(move |s| Message::BibleSearchInput(col_index, s)) .on_input(move |s| Message::BibleSearchInput(col_index, s))
.on_submit(Message::BibleSearchSubmit(col_index)), .on_submit(Message::BibleSearchSubmit(col_index)),
widget::text_input("Word Search", &self.states[col_index].word_search) widget::text_input("Word Search", &self.states[col_index].word_search)
.id(format!("word-{}", col_index)) .id(format!("tabId-{}", (col_index*2)+1))
.on_input(move |s| Message::WordSearchInput(col_index, s)) .on_input(move |s| Message::WordSearchInput(col_index, s))
.on_submit(Message::SubmitWordSearch(col_index)), .on_submit(Message::SubmitWordSearch(col_index)),
row![ row![
@ -365,6 +313,11 @@ impl State {
button("Copy Scripture") button("Copy Scripture")
.on_press_with(move || Message::CopyText(col_index)) .on_press_with(move || Message::CopyText(col_index))
.style(button::primary), .style(button::primary),
button("Delete Column")
.on_press_with(move || Message::DeleteColumn(col_index))
.style(button::danger),
text(format!("{:?}", self.states[col_index].error))
.style(text::danger),
] ]
.spacing(5), .spacing(5),
row![ row![

View File

@ -102,7 +102,7 @@ pub fn search_for_word(word: &str, from_files: &Bible) -> Vec<String> {
found found
} }
pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option<String> { pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Result<String, String> {
// expecting query to be in format: // expecting query to be in format:
// Gen 1:2 // Gen 1:2
// Gen 1:2-5 // Gen 1:2-5
@ -135,11 +135,11 @@ pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option<String
for (_, i) in &res { for (_, i) in &res {
eprintln!("\t{i}"); eprintln!("\t{i}");
} }
return None; return Err(format!("Ambigious input '{book}'"));
} }
_ => { _ => {
eprintln!("'{book}' is not a book"); eprintln!("'{book}' is not a book");
return None; return Err(format!("'{book}' is not a book"));
} }
}; };
@ -162,10 +162,10 @@ pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option<String
.map(|(bible,book,chapter)| (*bible,*book,chapter.unwrap())) .map(|(bible,book,chapter)| (*bible,*book,chapter.unwrap()))
.collect::<Vec<(&Bible, &Book, &Chapter)>>() .collect::<Vec<(&Bible, &Book, &Chapter)>>()
} else { } else {
return None; return Err(format!("Chapter number could not be parsed from {chapter}"));
} }
} else { } else {
return None; return Err(format!("A chapter was not passed"));
}; };
// Get the verse in each Bible // Get the verse in each Bible
@ -190,9 +190,9 @@ pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option<String
} }
} }
} }
return Some(buf); return Ok(buf);
} }
return None; return Err(format!("Could not parse '{sn}' or '{en}' into numbers"));
} }
// only one verse // only one verse
(Some(ver_num), None) => { (Some(ver_num), None) => {
@ -206,13 +206,13 @@ pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option<String
); );
} }
} }
return Some(buf); return Ok(buf);
} }
return None; return Err(format!("Could not parse '{ver_num}' into a number"));
} }
_ => { _ => {
// couldn't parse verse // couldn't parse verse
return None; return Err(format!("Couldn't parse '{:?}' or '{:?}' into verse numbers", start, end));
} }
} }
} }
@ -226,7 +226,7 @@ pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option<String
); );
buf += &format!("{}", chapter); buf += &format!("{}", chapter);
} }
return Some(buf); return Ok(buf);
} }
} }
} }