145 lines
4.4 KiB
Rust
145 lines
4.4 KiB
Rust
/*
|
|
Program: Bibliofile
|
|
Language: Rustc 1.71.0
|
|
ide: CLion
|
|
Operating system: POP_OS
|
|
Purpose: TUI-based ereader and library manager for Linux terminal environments.
|
|
Last edited: 11/3/23
|
|
*/
|
|
|
|
mod html_module;
|
|
use cursive::views::{Dialog, TextView};
|
|
use cursive::Cursive;
|
|
use cursive::view::Scrollable;
|
|
use epub::doc::EpubDoc; //library for navigating epubs
|
|
use std::*;
|
|
use std::i32;
|
|
use std::path::Path;
|
|
|
|
|
|
//this function will determine if Bibliofile has been opened before. If it has not, it will create a library folder under /opt/bibliofile.
|
|
fn library_exists(){
|
|
|
|
|
|
//if /var/lib/bibliofile/library does not exist, create it.
|
|
let library_exist_var = Path::exists("/var/lib/bibliofile/library".as_ref());
|
|
|
|
if library_exist_var == false{
|
|
fs::create_dir_all("/var/lib/bibliofile/library").expect("Error...could not create library. If this is your first time running Bibliofile, try running with sudo.\n\
|
|
Keep in mind, this program was designed with *nix systems in mind. Running this on Windows will result in unexpected behavior, as it uses a different file system.");
|
|
}
|
|
|
|
}
|
|
|
|
//initial function. Reads the ebook passed by argument.
|
|
//TODO: add visual library to pull up ebooks.
|
|
fn main() {
|
|
//library_exists();
|
|
if env::args().len() == 1 {
|
|
println!("You need to enter a book! \n\
|
|
context: bibliofile book.epub\n\
|
|
Closing program.");
|
|
}
|
|
else {
|
|
let args: Vec<String> = env::args().collect();
|
|
let filename = &args[1];
|
|
|
|
screen_func(filename);
|
|
}
|
|
}
|
|
|
|
|
|
//this function manages the screen
|
|
fn screen_func(epub_file: &str){
|
|
|
|
let mut page_num = 1;
|
|
let mut siv = cursive::default();
|
|
let textinfo = get_init_text(epub_file, &page_num, "next");
|
|
let text = textinfo.0;
|
|
let title = textinfo.1;
|
|
page_num = textinfo.2;
|
|
|
|
|
|
// Creates the cursive root - required for every application.
|
|
let mut siv = cursive::default();
|
|
siv.add_global_callback('q', |s| s.quit());
|
|
siv.add_global_callback('d', move |s| { get_text(s, &page_num, "next"); });
|
|
|
|
// Creates a dialog with a single "Quit" button
|
|
siv.add_layer(Dialog::around(TextView::new(text))
|
|
.title(title + " page: " + &page_num.to_string())
|
|
.scrollable()
|
|
.scroll_x(true),
|
|
);
|
|
|
|
// Starts the event loop.
|
|
siv.run();
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_text(s: &mut Cursive, mut page_num: &i32, direction: &str) {
|
|
s.pop_layer();
|
|
let args: Vec<String> = env::args().collect();
|
|
let filename = &args[1];
|
|
let doc = EpubDoc::new(filename);
|
|
let mut doc = doc.unwrap();
|
|
let mut usize_num = *page_num as usize;
|
|
usize_num = usize_num + 1;
|
|
let title = doc.mdata("title");
|
|
|
|
doc.set_current_page(usize_num);
|
|
let content = doc.get_current_str();
|
|
let str_content = content.unwrap();
|
|
let text = html_module::main(str_content.0);
|
|
s.add_layer(Dialog::around(TextView::new(text))
|
|
.title(title + " page: " + &page_num.to_string())
|
|
.scrollable()
|
|
.scroll_x(true),
|
|
);
|
|
|
|
}
|
|
|
|
fn get_init_text(epub_file: &str, mut page_num: &i32, direction: &str) -> (String, String, i32){
|
|
let doc = EpubDoc::new(&epub_file);
|
|
assert!(doc.is_ok());
|
|
let mut doc = doc.unwrap();
|
|
let mut usize_num = *page_num as usize;
|
|
|
|
return if direction == "next" {
|
|
usize_num = usize_num + 1;
|
|
let title = doc.mdata("title");
|
|
|
|
doc.set_current_page(usize_num);
|
|
let content = doc.get_current_str();
|
|
let str_content = content.unwrap();
|
|
let text = html_module::main(str_content.0);
|
|
|
|
(text, title.unwrap(), usize_num as i32)
|
|
} else if direction == "last" {
|
|
usize_num = usize_num - 1;
|
|
let title = doc.mdata("title");
|
|
|
|
doc.set_current_page(usize_num);
|
|
let content = doc.get_current_str();
|
|
let str_content = content.unwrap();
|
|
let text = html_module::main(str_content.0);
|
|
|
|
(text, title.unwrap(), usize_num as i32)
|
|
} else {
|
|
let title = doc.mdata("title");
|
|
|
|
doc.set_current_page(usize_num);
|
|
let content = doc.get_current_str();
|
|
let str_content = content.unwrap();
|
|
let text = html_module::main(str_content.0);
|
|
|
|
(text, title.unwrap(), usize_num as i32)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|