use std::sync::OnceLock;
use crate::color_enabled;
#[cfg(feature = "inline")]
use farben_core::inline;
use farben_core::{ansi, errors, parser, registry};
pub fn try_color(input: impl Into<String>) -> Result<String, errors::LexError> {
let input = input.into();
#[cfg(feature = "inline")]
let input = inline::preprocess(&input);
let mut res = parser::render_str(&input)?;
if color_enabled() {
res.push_str("\x1b[0m");
}
Ok(res)
}
#[cfg(not(feature = "compile"))]
pub fn color(input: impl Into<String>) -> String {
color_runtime(input, false)
}
#[cfg(not(feature = "compile"))]
pub fn colorb(input: impl Into<String>) -> String {
color_runtime(input, true)
}
static CODE_STYLE_INIT: OnceLock<()> = OnceLock::new();
pub fn color_runtime(input: impl Into<String>, bleed: bool) -> String {
CODE_STYLE_INIT.get_or_init(|| {
if registry::search_registry("code").is_err() {
let _ = registry::insert_style(
"code",
ansi::Style::parse("[bg:ansi(238) bright-white]").unwrap(),
);
}
});
let input = input.into();
#[cfg(feature = "inline")]
let input = inline::preprocess(&input);
let mut res = parser::render_str(&input).unwrap_or_else(|e| {
panic!(
"{}",
errors::LexErrorDisplay {
error: &e,
input: &input
}
);
});
if !bleed {
if color_enabled() {
res.push_str("\x1b[0m");
}
farben_core::clear_active_stack();
}
res
}
#[cfg(feature = "markdown")]
pub fn markdown(input: impl Into<String>) -> String {
farben_md::renderer::render(&farben_md::lexer::tokenize(&input.into()))
}