revamping user interface

This commit is contained in:
Zastian Pretorius
2022-06-10 21:44:20 +01:00
parent b6b59e89be
commit 72cfafccb2

View File

@@ -7,6 +7,25 @@ use isahc::prelude::*;
use std::process::{Command, ExitStatus}; use std::process::{Command, ExitStatus};
use std::io::Result; use std::io::Result;
fn main() { fn main() {
let mut exit_status = 0;
while exit_status == 0 {
}
let ln_url = search_ln();
let chapter_url = chapter_selector(&ln_url);
let full_text = get_full_text(&chapter_url);
//write full_text to file called temp.txt
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");
//close file
file.sync_all().expect("Unable to sync file");
//open temp.txt in cat for user to read
let _com = open_bat();
}
fn search_ln()->String{
let mut _is_n = false;
while _is_n == false{
let mut search_path = String::new(); let mut search_path = String::new();
println!("What ln do you want to read?"); println!("What ln do you want to read?");
std::io::stdin().read_line(&mut search_path).expect("Failed to read line"); std::io::stdin().read_line(&mut search_path).expect("Failed to read line");
@@ -28,20 +47,34 @@ fn main() {
} }
count += 1; count += 1;
} }
println!("Which ln do you want to read?"); println!("(n)\t{}","Search another title".red());
let mut ln_number = String::new(); let mut ln_number = String::new();
std::io::stdin().read_line(&mut ln_number).expect("Failed to read line"); std::io::stdin().read_line(&mut ln_number).expect("Failed to read line");
ln_number = ln_number.trim().to_string();
if ln_number != "n" {
let ln_number = ln_number.trim().to_string(); let ln_number = ln_number.trim().to_string();
let ln_number = ln_number.parse::<usize>().unwrap(); let ln_number = ln_number.parse::<usize>().unwrap();
let ln_url = &ln_urls[ln_number]; let ln_url = &ln_urls[ln_number];
let ln_url = ln_url.trim().to_string(); let ln_url = ln_url.trim().to_string();
_is_n = true;
return ln_url;
}
print!("\x1B[2J\x1B[1;1H");
}
return "".to_string();
}
fn chapter_selector(ln_url: &String)->String{
let exit = false;
let mut selected_chapter = 1;
while exit == false{
let ln_html = get_html(&ln_url); let ln_html = get_html(&ln_url);
let ln_id = get_ln_id(&ln_html); let ln_id = get_ln_id(&ln_html);
let ln_last_page = get_ln_last_page(&ln_html); let ln_last_page = get_ln_last_page(&ln_html);
let ln_page_html = page_selector(&ln_last_page, &ln_id); let ln_page_html = page_selector(&ln_id,selected_chapter);
let ln_chapters = get_ln_chapters(&ln_page_html); let ln_chapters = get_ln_chapters(&ln_page_html);
let ln_chapters_urls = get_ln_chapters_urls(&ln_page_html); let ln_chapters_urls = get_ln_chapters_urls(&ln_page_html);
count = 0; let mut count = 0;
for chaprer in ln_chapters { for chaprer in ln_chapters {
if count % 2 == 0 { if count % 2 == 0 {
println!("({})\t{}",count, format!("{}", chaprer.blue())); println!("({})\t{}",count, format!("{}", chaprer.blue()));
@@ -50,27 +83,39 @@ fn main() {
} }
count += 1; count += 1;
} }
println!("(n)\t{}","Go to next page".red());
println!("(b)\t{}","Go to previous page".red());
println!("Which chapter do you want to read?"); println!("Which chapter do you want to read?");
let mut chaprer_number = String::new(); let mut chapter_number = String::new();
std::io::stdin().read_line(&mut chaprer_number).expect("Failed to read line"); std::io::stdin().read_line(&mut chapter_number).expect("Failed to read line");
let chaprer_number = chaprer_number.trim().to_string(); chapter_number = chapter_number.trim().to_string();
if chapter_number == "n" && selected_chapter < ln_last_page.parse::<u32>().unwrap() {
selected_chapter += 1;
print!("\x1B[2J\x1B[1;1H");
}
else if chapter_number == "b" && selected_chapter > 1{
selected_chapter -= 1;
print!("\x1B[2J\x1B[1;1H");
}
else{
let chaprer_number = chapter_number.trim().to_string();
let chaprer_number = chaprer_number.parse::<usize>().unwrap(); let chaprer_number = chaprer_number.parse::<usize>().unwrap();
let chaprer_url = &ln_chapters_urls[chaprer_number]; let chaprer_url = &ln_chapters_urls[chaprer_number];
let chaprer_url = chaprer_url.trim().to_string(); let chaprer_url = chaprer_url.trim().to_string();
let ln_text = get_ln_text(&chaprer_url); return chaprer_url;
}
}
return "".to_string();
}
fn get_full_text(chapter_url: &String)->String{
let ln_text = get_ln_text(&chapter_url);
let mut full_text: String = String::new(); let mut full_text: String = String::new();
for line in ln_text { for line in ln_text {
let text = format!("{}\n\n", line); let text = format!("{}\n\n", line);
full_text.push_str(&text); full_text.push_str(&text);
} }
//write full_text to file called temp.txt full_text
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");
//close file
file.sync_all().expect("Unable to sync file");
//open temp.txt in cat for user to read
let _com = open_bat();
} }
pub fn open_bat() -> Result<ExitStatus> { pub fn open_bat() -> Result<ExitStatus> {
@@ -236,19 +281,9 @@ fn get_ln_next_page(ln_id: &str, page: &str) -> String {
html html
} }
fn page_selector(max_page: &str, ln_id: &str) -> String { fn page_selector(ln_id: &str,selected_page: u32) -> String {
println!("Please select a page go to");
println!("1..{}", max_page); get_ln_next_page(&ln_id, &selected_page.to_string())
let mut page_select = String::new();
std::io::stdin().read_line(&mut page_select).unwrap();
let page_select: u32 = page_select.trim().parse().unwrap();
while page_select > max_page.parse::<u32>().unwrap() {
println!("Please select a page go to");
println!("1..{}", max_page);
let mut page_select = String::new();
std::io::stdin().read_line(&mut page_select).unwrap();
}
get_ln_next_page(&ln_id, &page_select.to_string())
} }
fn get_ln_text(chapter_url: &str) -> Vec<String> { fn get_ln_text(chapter_url: &str) -> Vec<String> {