bibliofile/src/main.rs

132 lines
3.7 KiB
Rust
Raw Normal View History

/*
Program: Bibliofile
2023-07-24 23:40:33 +00:00
Language: Rustc 1.71.0
ide: CLion
Operating system: POP_OS
Purpose: TUI-based ereader and library manager for Linux terminal environments.
Last edited: 10/14/23
*/
mod html_module;
use cursive::views::{Dialog, TextView};
use cursive::backends::termion::termion;
use cursive::event;
use epub::doc::EpubDoc; //library for navigating epubs
use std::*;
use std::i32;
use std::io::stdin;
use std::path::Path;
use termion::event::{Event, Key};
use termion::input::TermRead;
use termion::scroll;
2023-10-14 23:23:13 +00:00
//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 textinfo = get_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();
// Creates a dialog with a single "Quit" button
siv.add_layer(Dialog::around(TextView::new(text))
.title(title)
.button("Quit", |s| s.quit()));
// Starts the event loop.
siv.run();
let c = stdin();
for c in stdin().events() {
let evt = c.unwrap();
match evt {
Event::Key(Key::Up) => scroll::Up(1),
_ => {}
}
}
}
fn get_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)
}
}
2023-07-24 23:36:08 +00:00