refactor: better file hierarchy

This commit is contained in:
justchokingaround
2022-07-18 04:05:27 +02:00
13 changed files with 449 additions and 126 deletions

31
src/ln/ln.rs Normal file
View File

@@ -0,0 +1,31 @@
use std::fs::File;
use std::io::Write;
use crate::{search_ln,chapter_selector,get_full_text,open_bat};
pub fn ln_read(){
let ln_url = search_ln();
let mut selected_page = 1;
loop {
//make empty tuple called chapter_url with (String, u32, u32)
let chapter_url = chapter_selector(&ln_url, selected_page);
selected_page = chapter_url.1;
let full_text = get_full_text(&chapter_url.0);
if cfg!(target_os = "windows"){
use dirs::home_dir;
let mut home = format!("{:?}",home_dir()).replace("\\\\","/");
home.drain(0..6);
home.drain(home.len()-2..home.len());
let mut file = File::create(format!("{}/AppData/Roaming/log_e",home)).expect("Unable to create file");
file.write_all(full_text.as_bytes())
.expect("Unable to write to file");
file.sync_all().expect("Unable to sync file");
}else{
let mut file = File::create("/tmp/log_e").expect("Unable to create file");
file.write_all(full_text.as_bytes())
.expect("Unable to write to file");
file.sync_all().expect("Unable to sync file");
};
//open temp.txt in cat for user to read
let _com = open_bat();
print!("\x1B[2J\x1B[1;1H");
}
}

View File

@@ -11,8 +11,7 @@ use crate::{
};
pub fn chapter_selector(ln_url: &str, mut selected_page: u32) -> (String, u32) {
let exit = false;
while exit == false {
loop {
let ln_html = ln::scraper::get_html(ln_url);
let ln_id = get_ln_id(&ln_html);
let ln_last_page = get_ln_last_page(&ln_html);
@@ -22,15 +21,16 @@ pub fn chapter_selector(ln_url: &str, mut selected_page: u32) -> (String, u32) {
let mut count = 0;
ln_chapters.into_iter().for_each(|chaprer| {
if count % 2 == 0 {
println!("({})\t{}", count, format_args!("{}", chaprer.blue()));
println!("({})\t{}", count.to_string().blue(), format_args!("{}", chaprer.blue()));
} else {
println!("({})\t{}", count, format_args!("{}", chaprer.yellow()));
println!("({})\t{}", count.to_string().yellow(), format_args!("{}", chaprer.yellow()));
}
count += 1;
});
println!("(n)\t{}", "Go to next page".red());
println!("(b)\t{}", "Go to previous page".red());
println!("(q)\t{}", "go back to main menu".red());
println!("{}\t{}","n:".green(), "Go to next page".green());
println!("{}\t{}","b:".yellow(), "Go to previous page".yellow());
println!("{}\t{}","s:".green(), "Search another title".green());
println!("{}\t{}","q:".red(), "quit".red());
let chapter_number = string_input("Which chapter do you want to read? ");
if chapter_number == "n" && selected_page < ln_last_page.parse::<u32>().unwrap() {
selected_page += 1;
@@ -39,14 +39,23 @@ pub fn chapter_selector(ln_url: &str, mut selected_page: u32) -> (String, u32) {
selected_page -= 1;
print!("\x1B[2J\x1B[1;1H");
} else if chapter_number == "q" {
print!("\x1B[2J\x1B[1;1H");
std::process::exit(0);
} else if chapter_number == "s" {
main();
} else {
}
else {
let chapter_number = chapter_number.trim().to_string();
let chapter_number = chapter_number.parse::<usize>().unwrap();
let chapter_url = &ln_chapters_urls[chapter_number];
let mut _chapter_number_int = 0;
if chapter_number.parse::<u32>().is_ok() {
_chapter_number_int = chapter_number.parse::<u32>().unwrap();
} else {
println!("{}", "Invalid option".red());
continue;
}
let chapter_url = &ln_chapters_urls[_chapter_number_int as usize];
let chapter_url = chapter_url.trim().to_string();
return (chapter_url, selected_page);
}
}
("".to_string(), 1)
}

View File

@@ -2,3 +2,4 @@ pub mod menu;
pub mod open_text;
pub mod scraper;
pub mod search;
pub mod ln;

View File

@@ -1,11 +1,39 @@
use std::io::Result;
use std::process::{Command, ExitStatus};
use std::process::{Command, ExitStatus, Stdio};
#[allow(unused_assignments)]
pub fn open_bat() -> Result<ExitStatus> {
let termsize::Size {rows: _, cols} = termsize::get().unwrap();
let mut path = String::new();
if cfg!(target_os = "windows"){
use dirs::home_dir;
let mut home = format!("{:?}",home_dir()).replace("\\\\","/");
home.drain(0..6);
home.drain(home.len()-2..home.len());
path = format!("{}/AppData/Roaming/log_e",home).to_string();
}
else{
path = "/tmp/log_e".to_string();
}
let soft_wrap = match Command::new("fold")
.arg("-s")
.arg("-w")
.arg((cols - 9).to_string())
.arg(path)
.stdout(Stdio::piped())
.spawn()
{
Err(why) => panic!("couldn't spawn wc: {}", why),
Ok(soft_wrap) => soft_wrap,
};
Command::new("bat")
.arg("--paging")
.arg("always")
.arg("/tmp/log_e")
.arg("-l")
.arg("markdown")
.stdin(soft_wrap.stdout.unwrap())
.spawn()?
.wait()
}

View File

@@ -23,19 +23,15 @@ pub fn search_ln() -> String {
let mut count = 0;
ln_titles.into_iter().for_each(|ln| {
if count % 2 == 0 {
println!("({})\t{}", count, format_args!("{}", ln.blue()));
println!("({})\t{}", count.to_string().blue(), format_args!("{}", ln.blue()));
} else {
println!("({})\t{}", count, format_args!("{}", ln.yellow()));
println!("({})\t{}", count.to_string().yellow(), format_args!("{}", ln.yellow()));
}
count += 1;
});
println!("(n)\t{}", "Search another title".red());
let mut ln_number = String::new();
std::io::stdin()
.read_line(&mut ln_number)
.expect("Failed to read line");
ln_number = ln_number.trim().to_string();
if ln_number != "n" {
println!("{}\t{}","s:".green(), "Search another title".green());
let ln_number = string_input("Enter an option: ");
if ln_number != "s" && ln_number.parse::<usize>().is_ok() {
let ln_number = ln_number.trim().to_string();
let ln_number = ln_number.parse::<usize>().unwrap();
let ln_url = &ln_urls[ln_number];
@@ -43,6 +39,8 @@ pub fn search_ln() -> String {
_is_n = true;
print!("\x1B[2J\x1B[1;1H");
return ln_url;
} else {
print!("invalid input");
}
print!("\x1B[2J\x1B[1;1H");
}