From 88c859538e2b64b498c0c7efc471b8ade2fdae14 Mon Sep 17 00:00:00 2001 From: Eberhard Kahlenberger Date: Sat, 1 Jun 2024 15:03:53 +0200 Subject: [PATCH] reasonable console output --- Cargo.toml | 7 +++++-- src/main.rs | 27 ++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8102d22..e592910 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,16 @@ [package] name = "readpage" -version = "0.1.0" +version = "0.2.0" edition = "2021" [dependencies] article_scraper = "^2" clap = { version = "^4.5", features = ["cargo", "color"] } +html-escape = "0.2.13" +html2text = "0.12.5" reqwest = {version = "^0.12", features = ["blocking", "charset"] } +termsize = "0.1.6" thiserror = "1.0.61" -url = "2.5.0" tokio = "1.38.0" +url = "2.5.0" diff --git a/src/main.rs b/src/main.rs index 160f7af..f11fb7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,10 @@ mod Error; use article_scraper::ArticleScraper; use clap::{arg, command}; +use html2text::from_read; +use html_escape::decode_html_entities; use reqwest::Client; +use termsize::Size; use tokio; use url::Url; @@ -12,7 +15,8 @@ use url::Url; use Error::AppError; #[tokio::main] -async fn main() -> Result<(), AppError> { +async fn main() -> Result<(), AppError> +{ let matches = command!() .arg(arg!([url] "The URL to a website to read out").required(true)) .get_matches(); @@ -22,8 +26,21 @@ async fn main() -> Result<(), AppError> { let parsedUrl = Url::parse(url).map_err(|e| AppError::UrlParseError(e))?; let client = Client::new(); let article = scraper.parse(&parsedUrl,false,&client,None).await.map_err(|e| AppError::ScrapeError(e.to_string()))?; - println!("{}",article.html.unwrap()); + if let Some(html) = article.html + { + println!("{}\n", url); + println!("{}", article.title.unwrap_or_else(|| "No Title".to_string())); + + let readable_text = from_read( + decode_html_entities(&html).as_bytes(), + termsize::get(). + unwrap_or(Size { cols: 100, rows: 0 }). + cols as usize); + println!("{}", readable_text); + } + else + { + println!("There is nothing readable at: {}", url); + } Ok(()) -} - - +} \ No newline at end of file