2023-07-20 15:28:40 +00:00
/*
Program : Bibliofile
2023-07-24 23:40:33 +00:00
Language : Rustc 1.7 1.0
2023-07-20 15:28:40 +00:00
ide : CLion
2023-10-15 03:06:56 +00:00
Operating system : POP_OS
2023-07-20 15:28:40 +00:00
Purpose : TUI - based ereader and library manager for Linux terminal environments .
2023-10-15 03:06:56 +00:00
Last edited : 10 / 14 / 23
2023-07-20 15:28:40 +00:00
* /
2023-07-27 23:23:41 +00:00
mod html_module ;
2023-10-15 03:06:56 +00:00
use cursive ::views ::{ Dialog , TextView } ;
2023-07-20 15:28:40 +00:00
use epub ::doc ::EpubDoc ; //library for navigating epubs
2023-07-27 23:23:41 +00:00
use std ::* ;
2023-10-15 03:06:56 +00:00
use std ::i32 ;
2023-07-27 23:23:41 +00:00
use std ::path ::Path ;
2023-10-14 23:23:13 +00:00
2023-07-20 15:28:40 +00:00
2023-07-27 23:23:41 +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 mut library_exist_var = Path ::exists ( " /var/lib/bibliofile/library " . as_ref ( ) ) ;
2023-07-27 23:56:14 +00:00
2023-07-27 23:23:41 +00:00
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. " ) ;
}
}
2023-07-20 15:28:40 +00:00
//initial function. Reads the ebook passed by argument.
//TODO: add visual library to pull up ebooks.
fn main ( ) {
2023-09-02 20:21:36 +00:00
//library_exists();
2023-07-20 15:28:40 +00:00
if env ::args ( ) . len ( ) = = 1 {
2023-10-15 03:06:56 +00:00
println! ( " You need to enter a book! \n \
context : bibliofile book . epub \ n \
Closing program . " );
2023-07-20 15:28:40 +00:00
}
else {
let args : Vec < String > = env ::args ( ) . collect ( ) ;
let filename = & args [ 1 ] ;
2023-10-15 03:06:56 +00:00
screen_func ( filename ) ;
2023-07-20 15:28:40 +00:00
}
}
2023-10-15 03:06:56 +00:00
//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 ( ) ;
}
2023-07-20 15:28:40 +00:00
2023-10-15 03:06:56 +00:00
fn get_text ( epub_file : & str , mut page_num : & i32 , direction : & str ) -> ( String , String , i32 ) {
2023-07-20 15:28:40 +00:00
let doc = EpubDoc ::new ( & epub_file ) ;
assert! ( doc . is_ok ( ) ) ;
let mut doc = doc . unwrap ( ) ;
2023-10-15 03:06:56 +00:00
let mut usize_num = * page_num as usize ;
if direction = = " next " {
usize_num = usize_num + 1 ;
let title = doc . mdata ( " title " ) ;
2023-07-20 15:28:40 +00:00
2023-10-15 03:06:56 +00:00
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 ) ;
2023-07-27 23:23:41 +00:00
2023-10-15 03:06:56 +00:00
return ( 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 ) ;
return ( 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 ) ;
return ( text , title . unwrap ( ) , usize_num as i32 ) ;
}
}
2023-07-27 23:23:41 +00:00
2023-07-24 23:36:08 +00:00
2023-09-02 20:36:26 +00:00
2023-07-27 23:56:14 +00:00