improved search and focus

This commit is contained in:
2025-03-26 23:35:07 -06:00
parent 2874617e4c
commit 3e7509a9c7
2 changed files with 86 additions and 45 deletions

View File

@@ -1,7 +1,6 @@
use inline_colorization::*;
use std::{fmt::Display, fs, path::PathBuf};
use std::fmt::Display;
use quick_xml::de::from_str;
use serde::Deserialize;
pub const BOOKS_IN_ORDER: [&str; 66] = [
@@ -73,7 +72,11 @@ pub const BOOKS_IN_ORDER: [&str; 66] = [
"Revelation",
];
pub fn get(book: &str, chap_and_ver: &str, from_files: Vec<PathBuf>) -> Option<String> {
pub fn search_for_word(word: &str, from_files: &Bible) -> Vec<String> {
todo!()
}
pub fn get(book: &str, chap_and_ver: &str, bibles: Vec<&Bible>) -> Option<String> {
// expecting query to be in format:
// Gen 1:2
// Gen 1:2-5
@@ -89,7 +92,7 @@ pub fn get(book: &str, chap_and_ver: &str, from_files: Vec<PathBuf>) -> Option<S
let res = BOOKS_IN_ORDER
.iter()
.enumerate()
.filter(|(_, lbook)| lbook.to_lowercase().contains(&book.trim().to_lowercase()))
.filter(|(_, lbook)| lbook.replace(" ", "").to_lowercase().starts_with(&book.trim().to_lowercase()))
.collect::<Vec<(usize, &&str)>>();
let (book_idx, book_name) = match res.len() {
@@ -107,19 +110,13 @@ pub fn get(book: &str, chap_and_ver: &str, from_files: Vec<PathBuf>) -> Option<S
}
};
// Load Bibles into memory
let bibles = from_files
.iter()
.filter_map(|path| fs::read_to_string(path).ok())
.filter_map(|contents| from_str::<Bible>(&contents).ok())
.collect::<Vec<Bible>>();
// Select the book in each Bible
let books = bibles
.iter()
// Book are 1 indexed in the xml spec
.map(|bible| (bible, bible.get_book_by_index(book_idx + 1)))
.filter(|(_,e)| e.is_some())
.map(|(bible,book)| (bible,book.unwrap()))
.map(|(bible,book)| (*bible,book.unwrap()))
.collect::<Vec<(&Bible,&Book)>>();
// Select the chapter in each Bible
@@ -237,12 +234,17 @@ impl Chapter {
}
#[derive(Deserialize)]
struct Bible {
pub struct Bible {
#[serde(rename = "@translation")]
translation_name: String,
#[serde(rename = "testament")]
testaments: Vec<Testament>,
}
impl Display for Bible {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}",self.translation_name)
}
}
#[derive(Deserialize)]
struct Testament {