use std::fmt::Display;
pub use rascii_art_img::RenderOptions as RasciiOptions;
pub use rascii_art_img::{
charsets::{Charset, from_enum, to_charset_enum},
convert_string_to_str_vec,
};
use crate::error::ImgiiError;
const DEFAULT_CHAR_FONT_SIZE: u32 = 16;
#[derive(Debug, Clone)]
pub struct ImgiiOptions<'a> {
font: Vec<u8>,
font_name: String,
font_size: u32,
background: bool,
rascii_options: RasciiOptions<'a>,
}
impl<'a> ImgiiOptions<'a> {
#[must_use]
fn new(
font: Vec<u8>,
font_name: String,
font_size: u32,
background: bool,
rascii_options: RasciiOptions<'a>,
) -> Self {
Self {
font,
font_name,
font_size,
background,
rascii_options,
}
}
#[must_use]
pub fn font(&self) -> &Vec<u8> {
&self.font
}
#[must_use]
pub fn font_name(&self) -> &str {
&self.font_name
}
#[must_use]
pub fn font_size(&self) -> u32 {
self.font_size
}
#[must_use]
pub fn background(&self) -> bool {
self.background
}
#[must_use]
pub fn rascii_options(&self) -> &RasciiOptions<'a> {
&self.rascii_options
}
}
impl<'a> Display for ImgiiOptions<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{font.len()={}; font_name={}; font_size={}, background={}; rascii_options={:?}}}",
self.font.len(),
self.font_name,
self.font_size,
self.background,
self.rascii_options
)
}
}
#[derive(Debug, Clone)]
pub struct ImgiiOptionsBuilder<'a> {
font: Option<Vec<u8>>,
font_name: Option<String>,
font_size: u32,
background: bool,
rascii_options: RasciiOptions<'a>,
}
impl<'a> Default for ImgiiOptionsBuilder<'a> {
fn default() -> Self {
Self {
font: None,
font_name: None,
font_size: DEFAULT_CHAR_FONT_SIZE,
background: false,
rascii_options: RasciiOptions::default()
.colored(true)
.escape_each_colored_char(true),
}
}
}
impl<'a> ImgiiOptionsBuilder<'a> {
pub fn new() -> Self {
Self::default()
}
pub fn font(mut self, font: Vec<u8>) -> Self {
self.font = Some(font);
self
}
pub fn font_name(mut self, font_name: String) -> Self {
self.font_name = Some(font_name);
self
}
pub fn font_size(mut self, font_size: u32) -> Self {
self.font_size = font_size;
self
}
pub fn background(mut self, background: bool) -> Self {
self.background = background;
self
}
pub fn build(&self) -> Result<ImgiiOptions<'a>, ImgiiError> {
let (Some(font), Some(font_name)) = (self.font.clone(), self.font_name.clone()) else {
return Err(ImgiiError::InvalidArgument);
};
Ok(ImgiiOptions::new(
font,
font_name,
self.font_size,
self.background,
self.rascii_options.clone(),
))
}
pub fn width(mut self, width: u32) -> Self {
self.rascii_options.width = Some(width);
self
}
pub fn height(mut self, height: u32) -> Self {
self.rascii_options.height = Some(height);
self
}
pub fn invert(mut self, invert: bool) -> Self {
self.rascii_options.invert = invert;
self
}
pub fn charset(mut self, charset: &'a [&'a str]) -> Self {
self.rascii_options.charset = charset;
self
}
pub fn char_override(mut self, char_override: Vec<String>) -> Self {
self.rascii_options.char_override = Some(char_override);
self
}
}