use anyhow::Result;
use clap::Parser;
use crate::browser::Browser;
use crate::cli::{Backend, Cli};
use crate::colors::{AnsiColor, AnsiStyle};
use crate::response::ResultFormat;
use crate::user_agents::get;
use urlencoding::encode;
pub async fn run_cli_entry(args: Vec<String>) -> Result<()> {
let args = Cli::parse_from(args);
let error_style = AnsiStyle {
bold: true,
color: Some(AnsiColor::Red),
};
if args.query.is_none() {
#[cfg(feature = "tui")]
return crate::tui::run_tui().await;
#[cfg(not(feature = "tui"))]
{
eprintln!(
"{}Error: --query (-q) is required when the TUI feature is disabled.{}",
error_style.escape_code(),
AnsiStyle::reset_code()
);
std::process::exit(1);
}
}
let query = args.query.unwrap();
let mut builder = Browser::builder();
if !args.user_agent.is_empty() {
match get(&args.user_agent) {
Some(agent) => builder = builder.user_agent(agent),
None => {
eprintln!(
"{}Error: Invalid user agent selected!{}",
error_style.escape_code(),
AnsiStyle::reset_code()
);
std::process::exit(1);
}
}
}
if args.cookie {
builder = builder.cookie_store(true);
}
if !args.proxy.is_empty() {
builder = builder.proxy(&args.proxy);
}
let browser = builder.build()?;
let usr_agent: &'static str = get(&args.user_agent).unwrap_or("");
let result_format = if args.format {
ResultFormat::Detailed
} else {
ResultFormat::List
};
let limit = Some(args.limit);
match args.backend {
Backend::Auto => {
if !args.operators.is_empty() {
browser
.search_operators(
&encode(&query),
&encode(&args.operators),
args.safe,
result_format,
limit,
None,
)
.await?;
} else {
browser
.search(&encode(&query), args.safe, result_format, limit, None)
.await?;
}
}
Backend::Lite => {
let results = browser
.lite_search(&query, "wt-wt", limit, usr_agent)
.await?;
for r in results {
println!("{}\n{}\n{}", r.title, r.url, r.snippet);
}
}
Backend::Images => {
let results = browser
.images(&query, "wt-wt", args.safe, limit, usr_agent)
.await?;
for r in results {
println!("{}\n{}\n{}", r.title, r.url, r.image);
}
}
Backend::News => {
let results = browser
.news(&query, "wt-wt", args.safe, limit, usr_agent)
.await?;
for r in results {
println!("{}\n{}\n{}", r.date, r.title, r.url);
}
}
}
Ok(())
}