Compare commits

...

2 Commits

Author SHA1 Message Date
Oliver Atkinson
2874617e4c gui is working 2025-03-26 15:06:06 -06:00
Oliver Atkinson
09acb5af50 working 2025-03-26 13:46:51 -06:00
8 changed files with 4280 additions and 36 deletions

45
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'bible'",
"cargo": {
"args": [
"build",
"--bin=bible",
"--package=bible"
],
"filter": {
"name": "bible",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'bible'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=bible",
"--package=bible"
],
"filter": {
"name": "bible",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

4002
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
iced = "0.13.1"
inline_colorization = "0.1.6"
quick-xml = { version="0.37.2", features=["serialize"] }
serde = { version = "1.0.219", features = ["derive"] }

View File

@ -8,3 +8,6 @@
[HewbrewLexicon](https://github.com/openscriptures/HebrewLexicon/tree/master)
[27, 91, 49, 109, 91, 69, 110, 103, 108, 105, 115, 104, 32, 78, 65, 83, 66, 93, 32, 27, 91, 52, 109, 71, 101, 110, 101, 115, 105, 115, 32, 49, 58, 49, 27, 91, 48, 109, 58, 32, 73, 110, 32, 116, 104, 101, 32, 98, 101, 103, 105, 110, 110, 105, 110, 103, 32, 71, 111, 100, 32, 99, 114, 101, 97, 116, 101, 100, 32, 116, 104, 101, 32, 104, 101, 97, 118, 101, 110, 115, 32, 97, 110, 100, 32, 116, 104, 101, 32, 101, 97, 114, 116, 104, 46, 10]
[??, [, 1, m, [, E, n,,,]

View File

@ -1,19 +1,210 @@
use std::env;
#![feature(iter_map_windows)]
use iced::{
Element,
widget::{self, column, combo_box},
};
use std::{env, 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::<Vec<String>>();
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) {
if let Some(f) = bible::get(
query,
second_query,
vec![format!("{BIBLE_DIRECTORY}/EnglishNASBBible.xml").into()],
) {
println!("{}", f);
}
return Ok(())
}
}
iced::run("Bible Search", State::update, State::view)
}
#[derive(Debug, Clone)]
enum Message {
InputChange(String),
BibleSelected(String),
SubmitQuery,
}
struct State {
files: combo_box::State<String>,
file_selected: Option<String>,
query: String,
body: Option<String>,
}
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::<Vec<PathBuf>>();
paths
} else {
todo!()
};
let files = files
.iter()
.filter_map(|f| f.to_str())
.map(|f| f.to_owned())
.collect::<Vec<String>>();
Self {
files: combo_box::State::new(files),
file_selected: None,
query: String::new(),
body: None,
}
}
}
impl State {
fn update(&mut self, msg: Message) {
match msg {
Message::BibleSelected(file) => {
self.file_selected = Some(file);
}
Message::InputChange(query) => {
// update self's state
self.query = query;
}
Message::SubmitQuery => {
let mut splits = self.query.split_whitespace();
let book = splits.next();
let location = splits.next();
if let (Some(book), Some(chap_and_ver), Some(file)) =
(book, location, &self.file_selected)
{
self.body = bible::get(book, chap_and_ver, vec![file.into()]);
}
}
}
}
fn view(&self) -> Element<Message> {
let msg = if let Some(body) = self.body.clone() {
parse(&body)
} else {
"None".to_string()
};
column![
widget::combo_box(
&self.files,
"Select Bible",
self.file_selected.as_ref(),
Message::BibleSelected
),
widget::text_input("Search query, ie: Gen 1:1", &self.query)
.on_input(Message::InputChange)
.on_submit(Message::SubmitQuery),
widget::text(msg),
]
.into()
}
}
// parse out terminal style codes
fn parse(input: &str) -> String {
let magic_start_indices = input
.bytes()
.into_iter()
.enumerate()
.map_windows(|[(ai, a), (bi, b)]| {
// 27 91 is the start sequence for a color/style
if *a == 27 && *b == 91 {
Some(*ai)
} else {
None
}
})
.filter_map(|f| f)
.collect::<Vec<usize>>();
let mut to_remove = Vec::new();
for start in magic_start_indices {
let range = &input[start..];
// "m" is the end of sequence
if let Some(end) = range.find("m") {
let end = start + end;
let code = &input[start..=end];
match code {
"\x1B[1m" => {} //style_bold
"\x1B[4m" => {} //style_underline
"\x1B[0m" => {} //style_reset
"\x1B[30m" => {} //color_black
"\x1B[31m" => {} //color_red
"\x1B[32m" => {} //color_green
"\x1B[33m" => {} //color_yellow
"\x1B[34m" => {} //color_blue
"\x1B[35m" => {} //color_magenta
"\x1B[36m" => {} //color_cyan
"\x1B[37m" => {} //color_white
"\x1B[90m" => {} //color_bright_black
"\x1B[91m" => {} //color_bright_red
"\x1B[92m" => {} //color_bright_green
"\x1B[93m" => {} //color_bright_yellow
"\x1B[94m" => {} //color_bright_blue
"\x1B[95m" => {} //color_bright_magenta
"\x1B[96m" => {} //color_bright_cyan
"\x1B[97m" => {} //color_bright_white
"\x1B[39m" => {} //color_reset
"\x1B[40m" => {} //bg_black
"\x1B[41m" => {} //bg_red
"\x1B[42m" => {} //bg_green
"\x1B[43m" => {} //bg_yellow
"\x1B[44m" => {} //bg_blue
"\x1B[45m" => {} //bg_magenta
"\x1B[46m" => {} //bg_cyan
"\x1B[47m" => {} //bg_white
"\x1B[100m" => {} //bg_bright_black
"\x1B[101m" => {} //bg_bright_red
"\x1B[102m" => {} //bg_bright_green
"\x1B[103m" => {} //bg_bright_yellow
"\x1B[104m" => {} //bg_bright_blue
"\x1B[105m" => {} //bg_bright_magenta
"\x1B[106m" => {} //bg_bright_cyan
"\x1B[107m" => {} //bg_bright_white
"\x1B[49m" => {} //bg_reset
_ => {}
}
to_remove.push((start, end));
}
}
to_remove.reverse();
let mut modified = input.to_owned();
for (s,e) in to_remove {
let front = modified[..s].to_string();
let back = modified[e+1..].to_string();
modified = front + &back;
}
modified
}

View File

@ -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(book: &str, chap_and_ver: &str, from_files: Vec<PathBuf>) -> Option<String> {
// 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 = chap_and_ver.split(':');
let chapter = splits.next();
let verse = splits.next();
@ -95,26 +89,26 @@ pub fn get(query: &str, loc: &str) {
let res = BOOKS_IN_ORDER
.iter()
.enumerate()
.filter(|(_, book)| book.to_lowercase().contains(&query.to_lowercase()))
.filter(|(_, lbook)| lbook.to_lowercase().contains(&book.trim().to_lowercase()))
.collect::<Vec<(usize, &&str)>>();
let (book_idx, book_name) = match res.len() {
1 => res[0],
2.. => {
eprintln!("Err: Ambigious input '{query}', could be any of:");
eprintln!("Err: Ambigious input '{book}', could be any of:");
for (_, i) in &res {
eprintln!("\t{i}");
}
return;
return None;
}
_ => {
eprintln!("'{query}' is not a book");
return;
eprintln!("'{book}' is not a book");
return None;
}
};
// Load Bibles into memory
let bibles = BIBLES
let bibles = from_files
.iter()
.filter_map(|path| fs::read_to_string(path).ok())
.filter_map(|contents| from_str::<Bible>(&contents).ok())
@ -138,10 +132,10 @@ pub fn get(query: &str, loc: &str) {
.map(|(bible,book,chapter)| (*bible,*book,chapter.unwrap()))
.collect::<Vec<(&Bible, &Book, &Chapter)>>()
} else {
return;
return None;
}
} else {
return;
return None;
};
// Get the verse in each Bible
@ -155,46 +149,54 @@ pub fn get(query: &str, loc: &str) {
// range of verses
(Some(sn), Some(en)) => {
if let (Ok(start), Ok(end)) = (sn.parse::<usize>(), en.parse::<usize>()) {
let mut buf = String::new();
for num in start..=end {
for (bible, _book, chapter) in &chapters {
if let Some(verse) = chapter.get_verse_by_index(num) {
println!(
"{style_bold}[{}] {style_underline}{book_name} {}:{}{style_reset}: {verse}",
buf += &format!(
"{style_bold}[{}] {style_underline}{book_name} {}:{}{style_reset}: {verse}\n",
bible.translation_name, chapter.number, verse.number
);
}
}
}
return Some(buf);
}
return None;
}
// only one verse
(Some(ver_num), None) => {
if let Ok(num) = ver_num.parse::<usize>() {
let mut buf = String::new();
for (bible, _book, chapter) in chapters {
if let Some(verse) = chapter.get_verse_by_index(num) {
println!(
"{style_bold}[{}] {style_underline}{book_name} {}:{}{style_reset}: {verse}",
buf += &format!(
"{style_bold}[{}] {style_underline}{book_name} {}:{}{style_reset}: {verse}\n",
bible.translation_name, chapter.number, verse.number
);
}
}
return Some(buf);
}
return None;
}
_ => {
// couldn't parse verse
return;
return None;
}
}
}
None => {
// only chapter
for (bible, _book, chapter) in chapters {
println!(
"{style_bold}[{}] {style_underline}{book_name} {}{style_reset}:",
let mut buf = String::new();
for (bible, _, chapter) in chapters {
buf += &format!(
"{style_bold}[{}] {style_underline}{book_name} {}{style_reset}:\n",
bible.translation_name, chapter.number
);
print!("{}", chapter);
buf += &format!("{}", chapter);
}
return Some(buf);
}
}
}

2
src/texts/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod bible;
pub mod strong;