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
Operating system : Fedora 38 / WSL
Purpose : TUI - based ereader and library manager for Linux terminal environments .
2023-07-27 23:23:41 +00:00
Last edited : 7 / 27 / 23
2023-07-20 15:28:40 +00:00
* /
2023-07-27 23:23:41 +00:00
mod html_module ;
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 ::* ;
use std ::path ::Path ;
2023-07-20 15:28:40 +00:00
use std ::process ::exit ;
2023-09-02 20:21:36 +00:00
use tuikit ::term ::{ Term } ;
2023-07-24 23:36:08 +00:00
use tuikit ::prelude ::* ;
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 {
println! ( " you need to enter a book. Closing program. " ) ;
}
else {
let args : Vec < String > = env ::args ( ) . collect ( ) ;
let filename = & args [ 1 ] ;
epub_func ( filename ) ;
}
}
//parses epub files
fn epub_func ( epub_file : & str ) {
let doc = EpubDoc ::new ( & epub_file ) ;
assert! ( doc . is_ok ( ) ) ;
let mut doc = doc . unwrap ( ) ;
2023-09-02 20:21:36 +00:00
let term : Term < ( ) > = Term ::new ( ) . unwrap ( ) ;
2023-07-24 23:36:08 +00:00
2023-09-02 20:21:36 +00:00
let _ = term . print ( 1 , 0 , " Use the left and right button to turn the page. \n Up and down scrolls. Press escape or q to quit. " ) ;
2023-07-24 23:36:08 +00:00
2023-07-27 23:56:14 +00:00
let mut page_num = 1 ;
2023-07-27 23:23:41 +00:00
2023-07-24 23:36:08 +00:00
2023-07-24 23:40:33 +00:00
//If letter q pressed, closes program.
while let Ok ( ev ) = term . poll_event ( ) {
2023-09-02 20:21:36 +00:00
let ( width , height ) = term . term_size ( ) . unwrap ( ) ;
2023-07-27 23:56:14 +00:00
2023-07-24 23:40:33 +00:00
match ev {
Event ::Key ( Key ::ESC ) | Event ::Key ( Key ::Char ( 'q' ) ) = > break ,
2023-07-27 23:56:14 +00:00
Event ::Key ( Key ::Right ) = > page_num = page_num + 1 ,
Event ::Key ( Key ::Left ) = > page_num = page_num - 1 ,
2023-07-24 23:40:33 +00:00
_ = > { }
}
2023-09-02 20:36:26 +00:00
//This is a really hacky way of doing this.
//TODO: Remember to make a more elegant way of handling stack underflow.
if ( page_num < = 0 ) {
doc . set_current_page ( 1 ) ;
page_num = 1 ;
let content = doc . get_current_str ( ) ;
let str_content = content . unwrap ( ) ;
let text = html_module ::main ( str_content . 0 ) ;
let _ = term . set_cursor ( 1 , 1 ) ;
let _ = term . print ( 2 , 2 , & text ) ;
let _ = term . present ( ) ;
}
else {
doc . set_current_page ( page_num ) ;
2023-07-27 23:56:14 +00:00
2023-09-02 20:36:26 +00:00
let content = doc . get_current_str ( ) ;
let str_content = content . unwrap ( ) ;
let text = html_module ::main ( str_content . 0 ) ;
let _ = term . set_cursor ( 1 , 1 ) ;
2023-07-27 23:56:14 +00:00
2023-09-02 20:36:26 +00:00
let _ = term . print ( 2 , 2 , & text ) ;
let _ = term . present ( ) ;
}
2023-07-24 23:40:33 +00:00
}
2023-09-02 20:21:36 +00:00
}