Skip to content

Commit

Permalink
Add smart searching behaviour to readme app
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Burns <[email protected]>
  • Loading branch information
nicoburns committed Nov 27, 2024
1 parent 3ee6377 commit 3ffc4d3
Showing 1 changed file with 64 additions and 63 deletions.
127 changes: 64 additions & 63 deletions apps/readme/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ use blitz_shell::{
};
use std::env::current_dir;
use std::fs;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::sync::Arc;
use tokio::runtime::Handle;
use url::Url;

const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0";
fn main() {
let raw_url = std::env::args().nth(1);
let raw_url = std::env::args().nth(1).unwrap_or_else(|| {
let cwd = current_dir().unwrap();
format!("{}", cwd.display())
});

// Turn on the runtime and enter it
let rt = tokio::runtime::Builder::new_multi_thread()
Expand Down Expand Up @@ -61,71 +64,69 @@ fn main() {
event_loop.run_app(&mut application).unwrap()
}

async fn fetch(raw_url: Option<String>) -> (String, String, bool) {
match raw_url {
None => {
let cwd = current_dir().ok();
let mut maybe_dir = cwd.as_deref();
while let Some(dir) = maybe_dir {
let path = dir.join("README.md");
if fs::exists(&path).unwrap() {
let base_url = format!("file://{}/", dir.display());
let contents = std::fs::read_to_string(&path).unwrap();
return (base_url, contents, true);
}

maybe_dir = dir.parent()
async fn fetch(raw_url: String) -> (String, String, bool) {
if let Ok(url) = Url::parse(&raw_url) {
match url.scheme() {
"file" => fetch_file_path(url.path()),
_ => {
let client = reqwest::Client::new();
let response = client
.get(url)
.header("User-Agent", USER_AGENT)
.send()
.await
.unwrap();
let content_type = response
.headers()
.get(HeaderName::from_static("content-type"));
let is_md = raw_url.ends_with(".md")
|| content_type.is_some_and(|ct| {
ct.to_str().is_ok_and(|ct| ct.starts_with("text/markdown"))
});
let file_content = response.text().await.unwrap();

(raw_url, file_content, is_md)
}

eprintln!("Could not find README.md file in the current directory");
std::process::exit(1);
}
Some(raw_url) => {
if let Ok(url) = Url::parse(&raw_url) {
match url.scheme() {
"file" => {
let raw_file_content = std::fs::read(url.path()).unwrap();
let file_content = String::from_utf8(raw_file_content).unwrap();
let base_url = PathBuf::from(&raw_url)
.parent()
.unwrap()
.to_string_lossy()
.to_string();
let is_md = raw_url.ends_with(".md");
(base_url, file_content, is_md)
}
_ => {
let client = reqwest::Client::new();
let response = client
.get(url)
.header("User-Agent", USER_AGENT)
.send()
.await
.unwrap();
let content_type = response
.headers()
.get(HeaderName::from_static("content-type"));
let is_md = raw_url.ends_with(".md")
|| content_type.is_some_and(|ct| {
ct.to_str().is_ok_and(|ct| ct.starts_with("text/markdown"))
});
let file_content = response.text().await.unwrap();

(raw_url, file_content, is_md)
} else if fs::exists(&raw_url).unwrap() {
fetch_file_path(&raw_url)
} else {
eprintln!("Cannot parse {} as url or find it as a file", raw_url);
std::process::exit(1);
}
}

fn fetch_file_path(raw_path: &str) -> (String, String, bool) {
let path = std::path::absolute(Path::new(&raw_path)).unwrap();

// If path is a directory, search for README.md in that directory or any parent directories
let path = if path.is_dir() {
let mut maybe_dir: Option<&Path> = Some(path.as_ref());
loop {
match maybe_dir {
Some(dir) => {
let rdme_path = dir.join("README.md");
if fs::exists(&rdme_path).unwrap() {
break rdme_path;
}
maybe_dir = dir.parent()
}
None => {
eprintln!("Could not find README.md file in the current directory");
std::process::exit(1);
}
} else if fs::exists(&raw_url).unwrap() {
let base_path = std::path::absolute(Path::new(&raw_url)).unwrap();
let base_path = base_path.parent().unwrap().to_string_lossy();
let base_url = format!("file://{}/", base_path);
let contents = std::fs::read_to_string(&raw_url).unwrap();
let is_md = raw_url.ends_with(".md");

(base_url, contents, is_md)
} else {
eprintln!("Cannot parse {} as url or find it as a file", raw_url);
std::process::exit(1);
}
}
}
} else {
path
};

let base_url_path = path.parent().unwrap().to_string_lossy();
let base_url = format!("file://{}/", base_url_path);
let is_md = path.extension() == Some("md".as_ref());

// Read file
let file_content = std::fs::read_to_string(&path).unwrap();

(base_url, file_content, is_md)
}

0 comments on commit 3ffc4d3

Please sign in to comment.