use std::path::PathBuf;
pub fn init() {
#[cfg(windows)]
iniciar_windows();
}
#[cfg(windows)]
fn iniciar_windows() {
use windows_sys::Win32::System::Console::{
GetConsoleMode, GetStdHandle, SetConsoleCP, SetConsoleMode, SetConsoleOutputCP,
ENABLE_VIRTUAL_TERMINAL_PROCESSING, STD_OUTPUT_HANDLE,
};
let resultado_output = unsafe { SetConsoleOutputCP(65001) };
if resultado_output == 0 {
tracing::warn!("Failed to configure UTF-8 output codepage (65001) on Windows console.");
}
let resultado_input = unsafe { SetConsoleCP(65001) };
if resultado_input == 0 {
tracing::warn!("Failed to configure UTF-8 input codepage (65001) on Windows console.");
}
if resultado_output != 0 || resultado_input != 0 {
tracing::debug!("UTF-8 codepage (65001) configured on Windows console.");
}
let handle = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
if handle != 0 && handle != usize::MAX {
let mut mode: u32 = 0;
if unsafe { GetConsoleMode(handle as isize, &mut mode) } != 0 {
let novo = mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if unsafe { SetConsoleMode(handle as isize, novo) } == 0 {
tracing::debug!("ANSI VTP not available on this Windows console.");
}
}
}
}
pub fn stdout_is_tty() -> bool {
use std::io::IsTerminal;
std::io::stdout().is_terminal()
}
pub fn config_directory() -> Option<PathBuf> {
if let Some(home) = std::env::var_os("DUCKDUCKGO_SEARCH_CLI_HOME") {
let p = PathBuf::from(home);
if p.to_string_lossy().contains("..") {
tracing::warn!("DUCKDUCKGO_SEARCH_CLI_HOME contains '..', ignoring");
} else {
return Some(p);
}
}
dirs::config_dir().map(|base| base.join("duckduckgo-search-cli"))
}
pub fn should_disable_color(flag_no_color: bool) -> bool {
flag_no_color
|| std::env::var_os("NO_COLOR").is_some()
|| std::env::var("CLICOLOR_FORCE").ok().as_deref() == Some("0")
}
pub fn selectors_toml_path() -> Option<PathBuf> {
config_directory().map(|base| base.join("selectors.toml"))
}
pub fn user_agents_toml_path() -> Option<PathBuf> {
config_directory().map(|base| base.join("user-agents.toml"))
}
pub fn platform_name() -> &'static str {
if cfg!(target_os = "linux") {
"linux"
} else if cfg!(target_os = "macos") {
"macos"
} else if cfg!(target_os = "windows") {
"windows"
} else {
"outro"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn platform_name_returns_known_value() {
let nome = platform_name();
assert!(matches!(nome, "linux" | "macos" | "windows" | "outro"));
}
#[test]
fn init_should_not_panic() {
init();
}
#[test]
fn config_directory_not_empty_on_systems_with_home() {
let _ = config_directory();
}
#[test]
fn toml_paths_end_with_expected_names() {
if let Some(selectors) = selectors_toml_path() {
assert!(selectors.ends_with("selectors.toml"));
}
if let Some(uas) = user_agents_toml_path() {
assert!(uas.ends_with("user-agents.toml"));
}
}
}