1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
//! The contents of this module is all about transforming letters on the output vector
use crate::color::{color2hex, get_foreground_color};
use crate::config::{Align, Colors, Env, Options};
use crate::debug::{d, Dt};
/// Generate the letter space by taking into account the letter_spacing options
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::Options;
/// use cfonts::chars::get_letter_space;
///
/// let options = Options::default();
/// let font_letter_space = vec![
/// String::from("~"),
/// String::from("~"),
/// String::from("~"),
/// ];
///
/// assert_eq!(
/// get_letter_space(&font_letter_space, 1, &options),
/// vec![
/// String::from("~"),
/// String::from("~"),
/// String::from("~"),
/// ]
/// );
/// assert_eq!(
/// get_letter_space(&font_letter_space, 2, &options),
/// vec![
/// String::from("~~"),
/// String::from("~~"),
/// String::from("~~"),
/// ]
/// );
/// assert_eq!(
/// get_letter_space(&font_letter_space, 10, &options),
/// vec![
/// String::from("~~~~~~~~~~"),
/// String::from("~~~~~~~~~~"),
/// String::from("~~~~~~~~~~"),
/// ]
/// );
/// ```
pub fn get_letter_space(letter_space: &[String], letter_spacing: u16, options: &Options) -> Vec<String> {
d("chars::get_letter_space()", 5, Dt::Head, options, &mut std::io::stdout());
d(
&format!("chars::get_letter_space()\nletter_space:{:?}\noptions.letter_spacing:{:?}", letter_space, letter_spacing),
5,
Dt::Log,
options,
&mut std::io::stdout(),
);
let mut output = Vec::new();
for letter_space_line in letter_space {
let space = match letter_space_line.len() {
0 => String::from(" ").repeat(letter_spacing as usize),
_ => letter_space_line.repeat(letter_spacing as usize),
};
output.push(space);
}
d(&format!("chars::get_letter_space() -> {:?}", output), 5, Dt::Log, options, &mut std::io::stdout());
output
}
/// Adding a new line to a given output vector
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::Options;
/// use cfonts::chars::add_line;
///
/// let options = Options::default();
/// let mut output = vec![
/// String::from("1"),
/// String::from("1"),
/// String::from("2"),
/// String::from("2"),
/// ];
/// add_line(&mut output, 2, &options);
///
/// assert_eq!(
/// output,
/// vec![
/// String::from("1"),
/// String::from("1"),
/// String::from("2"),
/// String::from("2"),
/// String::new(),
/// String::new(),
/// ]
/// );
/// ```
pub fn add_line(output: &mut Vec<String>, font_lines: usize, options: &Options) {
d("chars::add_line()", 5, Dt::Head, options, &mut std::io::stdout());
d(
&format!("chars::add_line()\noutput:{:?}\nfont_lines:{:?}", output, font_lines),
5,
Dt::Log,
options,
&mut std::io::stdout(),
);
for _ in 0..font_lines {
output.push(String::new());
}
d(&format!("chars::add_line() -> {:?}", output), 5, Dt::Log, options, &mut std::io::stdout());
}
/// Adding a letter to a given output vector
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::Options;
/// use cfonts::chars::add_letter;
///
/// let options = Options::default();
/// let mut output = vec![
/// String::from("1"),
/// String::from("1"),
/// String::from("2"),
/// String::from("2"),
/// ];
/// let mut letter = vec![
/// String::from("letter"),
/// String::from("letter"),
/// ];
/// add_letter(&mut output, &letter, &options);
///
/// assert_eq!(
/// output,
/// vec![
/// String::from("1"),
/// String::from("1"),
/// String::from("2letter"),
/// String::from("2letter"),
/// ]
/// );
/// ```
pub fn add_letter(output: &mut [String], letter: &[String], options: &Options) {
d("chars::add_letter()", 5, Dt::Head, options, &mut std::io::stdout());
d(
&format!("chars::add_letter()\noutput:{:?}\nletter:{:?}", output, letter),
5,
Dt::Log,
options,
&mut std::io::stdout(),
);
for (i, line) in letter.iter().enumerate() {
let index = output.len() - (letter.len() - i);
output[index] += line;
}
d(&format!("chars::add_letter() -> {:?}", output), 5, Dt::Log, options, &mut std::io::stdout());
}
/// Adding line height to a given output vector
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::Options;
/// use cfonts::chars::add_line_height;
///
/// let options = Options::default();
/// let mut output = vec![
/// String::from("1"),
/// String::from("1"),
/// String::from("1"),
/// ];
/// add_line_height(&mut output, 3, &options);
///
/// assert_eq!(
/// output,
/// vec![
/// String::from("1"),
/// String::from("1"),
/// String::from("1"),
/// String::new(),
/// String::new(),
/// String::new(),
/// ]
/// );
/// ```
pub fn add_line_height(output: &mut Vec<String>, line_height: u16, options: &Options) {
d("chars::add_line_height()", 5, Dt::Head, options, &mut std::io::stdout());
d(
&format!("chars::add_line_height()\noutput:{:?}\nline_height:{:?}", output, line_height),
5,
Dt::Log,
options,
&mut std::io::stdout(),
);
for _ in 0..line_height {
output.push(String::new());
}
d(&format!("chars::add_line_height() -> {:?}", output), 5, Dt::Log, options, &mut std::io::stdout());
}
/// Get the longest line length of a vectors last n items
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::Options;
/// use cfonts::chars::get_longest_line_len;
///
/// let options = Options::default();
/// let output = vec![
/// String::from("1"),
/// String::from("1"),
/// String::from("1"),
/// String::from("222"),
/// String::from("22"),
/// String::from("22"),
/// ];
///
/// assert_eq!(get_longest_line_len(&output, 3, &options), 3);
/// ```
pub fn get_longest_line_len(output: &[String], font_lines: usize, options: &Options) -> usize {
d("chars::get_longest_line_len()", 5, Dt::Head, options, &mut std::io::stdout());
d(
&format!("chars::get_longest_line_len()\noutput:{:?}\nfont_lines:{:?}", output, font_lines),
5,
Dt::Log,
options,
&mut std::io::stdout(),
);
let size = output.iter().rev().take(font_lines).fold(0, |acc, item| {
if item.chars().count() > acc {
item.chars().count()
} else {
acc
}
});
d(&format!("chars::get_longest_line_len() -> {:?}", size), 5, Dt::Log, options, &mut std::io::stdout());
size
}
/// Get the longest line length of a vectors last n items
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::Options;
/// use cfonts::chars::get_first_char_position;
///
/// let options = Options::default();
/// assert_eq!(get_first_char_position(&[String::from("x")], &options), 0);
/// assert_eq!(get_first_char_position(&[String::from(" x")], &options), 2);
/// assert_eq!(get_first_char_position(&[String::from(" x")], &options), 4);
///
/// assert_eq!(
/// get_first_char_position(&[
/// String::from(" x"),
/// String::from(" x"),
/// String::from(" x"),
/// ], &options),
/// 1
/// );
/// assert_eq!(
/// get_first_char_position(&[
/// String::from(" x"),
/// String::from(""),
/// String::from(" x"),
/// ], &options),
/// 3
/// );
/// assert_eq!(
/// get_first_char_position(&[
/// String::from(" x"),
/// String::from(" x x"),
/// String::from(" x x"),
/// ], &options),
/// 1
/// );
/// ```
pub fn get_first_char_position(output: &[String], options: &Options) -> usize {
d("chars::get_first_char_position()", 5, Dt::Head, options, &mut std::io::stdout());
d(&format!("chars::get_first_char_position()\noutput:{:?}", output), 5, Dt::Log, options, &mut std::io::stdout());
let closest_line = output.iter().fold(&output[0], |prev_line, line| {
if !line.is_empty() && line.len() - line.trim_start().len() < prev_line.len() - prev_line.trim_start().len() {
line
} else {
prev_line
}
});
let pos = closest_line.len() - closest_line.trim_start().len();
d(&format!("chars::get_first_char_position() -> {:?}", pos), 5, Dt::Log, options, &mut std::io::stdout());
pos
}
/// Get the length of a letter contained in a vector
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::Options;
/// use cfonts::chars::get_letter_length;
///
/// let options = Options::default();
/// let letter = [
/// String::from("<c1>███</c1><c2>╗</c2><c1> ███</c1><c2>╗</c2>"),
/// String::from("<c1>████</c1><c2>╗</c2><c1> ████</c1><c2>║</c2>"),
/// String::from("<c1>██</c1><c2>╔</c2><c1>████</c1><c2>╔</c2><c1>██</c1><c2>║</c2>"),
/// String::from("<c1>██</c1><c2>║╚</c2><c1>██</c1><c2>╔╝</c2><c1>██</c1><c2>║</c2>"),
/// String::from("<c1>██</c1><c2>║ ╚═╝</c2><c1> ██</c1><c2>║</c2>"),
/// String::from("<c2>╚═╝ ╚═╝</c2>"),
/// ];
///
/// assert_eq!(get_letter_length(&letter, 2, &options), 11);
/// ```
pub fn get_letter_length(letter: &[String], font_color_count: usize, options: &Options) -> usize {
d("chars::get_letter_length()", 5, Dt::Head, options, &mut std::io::stdout());
d(
&format!("chars::get_letter_length()\nchar:{:?}\nfont_color_count:{:?}", letter, font_color_count),
5,
Dt::Log,
options,
&mut std::io::stdout(),
);
let size = letter.iter().fold(0, |acc, item| {
let mut stripped_item = item.clone();
// first we remove color annotations
// but fonts that support only a single color don't have color annotations
if font_color_count > 1 {
for i in 1..=font_color_count {
let open = format!("<c{}>", i);
let close = format!("</c{}>", i);
stripped_item = stripped_item.replace(&open, "").replace(&close, "");
}
}
if stripped_item.chars().count() > acc {
stripped_item.chars().count()
} else {
acc
}
});
d(&format!("chars::get_letter_length() -> {:?}", size), 5, Dt::Log, options, &mut std::io::stdout());
size
}
/// Add color meta info to a letter in form of ansi escape codes or HTML depending on the env set in [`Options`]
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::{Options, Colors};
/// use cfonts::chars::paint_letter;
///
/// let mut options = Options::default();
/// options.colors = vec![Colors::Red, Colors::Green];
/// let mut letter = vec![
/// String::from("<c1>red</c1>"),
/// String::from("<c1>red</c1><c2>green</c2><c1>red</c1>"),
/// String::from("no color"),
/// ];
///
/// assert_eq!(paint_letter(&letter, 2, &options),
/// vec![
/// String::from("\x1b[31mred\x1b[39m"),
/// String::from("\x1b[31mred\x1b[39m\x1b[32mgreen\x1b[39m\x1b[31mred\x1b[39m"),
/// String::from("no color"),
/// ]
/// );
/// ```
pub fn paint_letter(letter: &[String], font_color_count: usize, options: &Options) -> Vec<String> {
d("chars::paint_letter()", 5, Dt::Head, options, &mut std::io::stdout());
d(
&format!(
"chars::paint_letter()\nletter:{:?}\ncolors:{:?}\nfont_color_count:{:?}",
letter, options.colors, font_color_count
),
5,
Dt::Log,
options,
&mut std::io::stdout(),
);
let painted_letter = letter
.iter()
.map(|line| {
let mut new_line = line.clone();
if line.is_empty() {
new_line
} else {
let colors = if options.colors.len() > font_color_count {
&options.colors[0..font_color_count]
} else {
&options.colors
};
for i in 0..=options.colors.len() {
let color_name = match !options.gradient.is_empty() {
true => &Colors::System,
false => colors.get(i).unwrap_or(&Colors::System),
};
let (color_start, color_end) = match options.env {
Env::Cli => {
let (color_start, color_end) = match color_name {
Colors::System => (String::from(""), String::from("")),
color => get_foreground_color(color),
};
(color_start, color_end)
}
Env::Browser => {
let hex = color2hex(color_name, options);
if hex == *"transparent" {
(String::from(""), String::from(""))
} else {
(format!("<span style=\"color:{}\">", hex), String::from("</span>"))
}
}
};
if font_color_count == 1 {
new_line = format!("{}{}{}", color_start, new_line, color_end);
} else {
let open = format!("<c{}>", i + 1);
let close = format!("</c{}>", i + 1);
new_line = new_line.replace(&open, &color_start).replace(&close, &color_end);
}
}
for i in colors.len()..=font_color_count {
let open = format!("<c{}>", i);
let close = format!("</c{}>", i);
new_line = new_line.replace(&open, "").replace(&close, "");
}
new_line
}
})
.collect();
d(&format!("chars::paint_letter() -> {:?}", painted_letter), 5, Dt::Log, options, &mut std::io::stdout());
painted_letter
}
/// Align the last n line of an output vector to x total width taking into account the max amount of letters allowed
///
/// ```rust
/// extern crate cfonts;
///
/// use cfonts::{Options, Align};
/// use cfonts::chars::align_last_line;
///
/// let mut options = Options::default();
/// options.align = Align::Right;
/// let mut input = vec![
/// String::from(" line 1"),
/// String::from(" line 2"),
/// String::from(" line 3"),
/// String::from("line 4"),
/// String::from("line 5"),
/// ];
/// align_last_line(&mut input, 2, 6, 10, &options);
///
/// assert_eq!(input, vec![
/// String::from(" line 1"),
/// String::from(" line 2"),
/// String::from(" line 3"),
/// String::from(" line 4"),
/// String::from(" line 5"),
/// ]);
/// ```
pub fn align_last_line(
output: &mut [String],
font_lines: usize,
line_length: usize,
max_length: usize,
options: &Options,
) {
d("chars::align_last_line()", 5, Dt::Head, options, &mut std::io::stdout());
d(
&format!(
"chars::align_last_line()\noutput:{:?}\nfont_lines:{:?}\nline_length:{:?}\nmax_length:{:?}",
output, font_lines, line_length, max_length
),
5,
Dt::Log,
options,
&mut std::io::stdout(),
);
if options.env == Env::Cli {
let space = match options.align {
Align::Right => format!("{0:>width$}", "", width = (max_length - line_length)),
Align::Center => {
format!("{0:>width$}", "", width = (((max_length as f64 - line_length as f64) / 2.0).round() as usize))
}
_ => String::from(""),
};
let start = output.len() - font_lines;
for line in output.iter_mut().skip(start) {
line.insert_str(0, &space);
}
}
d(&format!("chars::align_last_line() -> {:?}", output), 5, Dt::Log, options, &mut std::io::stdout());
}