|
| 1 | +//! # onefetch-ascii |
| 2 | +//! |
| 3 | +//! Provides the ascii template interface for [onefetch](https://github.com/o2sh/onefetch). |
| 4 | +//! |
| 5 | +//! ```rust,no_run |
| 6 | +//! use onefetch_ascii::AsciiArt; |
| 7 | +//! use owo_colors::{DynColors, AnsiColors}; |
| 8 | +//! |
| 9 | +//! const ASCII: &str = r#" |
| 10 | +//! {2} .:--::////::--.` |
| 11 | +//! {1} `/yNMMNho{2}////////////:. |
| 12 | +//! {1} `+NMMMMMMMMmy{2}/////////////:` |
| 13 | +//! {0} `-:::{1}ohNMMMMMMMNy{2}/////////////:` |
| 14 | +//! {0} .::::::::{1}odMMMMMMMNy{2}/////////////- |
| 15 | +//! {0} -:::::::::::{1}/hMMMMMMMmo{2}////////////- |
| 16 | +//! {0} .::::::::::::::{1}oMMMMMMMMh{2}////////////- |
| 17 | +//! {0}`:::::::::::::{1}/dMMMMMMMMMMNo{2}///////////` |
| 18 | +//! {0}-::::::::::::{1}sMMMMMMmMMMMMMMy{2}//////////- |
| 19 | +//! {0}-::::::::::{1}/dMMMMMMs{0}:{1}+NMMMMMMd{2}/////////: |
| 20 | +//! {0}-:::::::::{1}+NMMMMMm/{0}:::{1}/dMMMMMMm+{2}///////: |
| 21 | +//! {0}-::::::::{1}sMMMMMMh{0}:::::::{1}dMMMMMMm+{2}//////- |
| 22 | +//! {0}`:::::::{1}sMMMMMMy{0}:::::::::{1}dMMMMMMm+{2}/////` |
| 23 | +//! {0} .:::::{1}sMMMMMMs{0}:::::::::::{1}mMMMMMMd{2}////- |
| 24 | +//! {0} -:::{1}sMMMMMMy{0}::::::::::::{1}/NMMMMMMh{2}//- |
| 25 | +//! {0} .:{1}+MMMMMMd{0}::::::::::::::{1}oMMMMMMMo{2}- |
| 26 | +//! {1} `yMMMMMN/{0}:::::::::::::::{1}hMMMMMh. |
| 27 | +//! {1} -yMMMo{0}::::::::::::::::{1}/MMMy- |
| 28 | +//! {1} `/s{0}::::::::::::::::::{1}o/` |
| 29 | +//! {0} ``.---::::---..` |
| 30 | +//! "#; |
| 31 | +//! |
| 32 | +//! let colors = vec![ |
| 33 | +//! DynColors::Ansi(AnsiColors::Blue), |
| 34 | +//! DynColors::Ansi(AnsiColors::Default), |
| 35 | +//! DynColors::Ansi(AnsiColors::BrightBlue) |
| 36 | +//! ]; |
| 37 | +//! |
| 38 | +//! let art = AsciiArt::new(ASCII, colors.as_slice(), true); |
| 39 | +//! |
| 40 | +//! for line in art { |
| 41 | +//! println!("{line}") |
| 42 | +//! } |
| 43 | +//! ``` |
| 44 | +//! |
| 45 | +
|
1 | 46 | use owo_colors::{AnsiColors, DynColors, OwoColorize, Style};
|
2 | 47 | use std::fmt::Write;
|
3 | 48 |
|
| 49 | +/// Renders an ascii template with the given colors truncated to the correct width. |
4 | 50 | pub struct AsciiArt<'a> {
|
5 | 51 | content: Box<dyn 'a + Iterator<Item = &'a str>>,
|
6 | 52 | colors: &'a [DynColors],
|
@@ -211,9 +257,10 @@ fn add_styled_segment(base: &mut String, segment: &str, color: DynColors, bold:
|
211 | 257 | type ParseResult<'a, R> = Option<(&'a str, R)>;
|
212 | 258 |
|
213 | 259 | fn token<R>(s: &str, predicate: impl FnOnce(char) -> Option<R>) -> ParseResult<R> {
|
214 |
| - let token = s.chars().next()?; |
| 260 | + let mut chars = s.chars(); |
| 261 | + let token = chars.next()?; |
215 | 262 | let result = predicate(token)?;
|
216 |
| - Some((s.get(1..).unwrap(), result)) |
| 263 | + Some((chars.as_str(), result)) |
217 | 264 | }
|
218 | 265 |
|
219 | 266 | // Parsers
|
@@ -313,6 +360,12 @@ mod test {
|
313 | 360 | "\u{1b}[39;1m \u{1b}[0m"
|
314 | 361 | );
|
315 | 362 |
|
| 363 | + // https://github.com/o2sh/onefetch/issues/935 |
| 364 | + assert_eq!( |
| 365 | + Tokens("███").render(Vec::new().as_slice(), 0, 3, true), |
| 366 | + "\u{1b}[39;1m███\u{1b}[0m" |
| 367 | + ); |
| 368 | + |
316 | 369 | assert_eq!(
|
317 | 370 | Tokens(" {1} {5} {9} a").render(&colors_shim, 4, 10, true),
|
318 | 371 | "\u{1b}[39;1m\u{1b}[0m\u{1b}[39;1m\u{1b}[0m\u{1b}[39;1m \u{1b}[0m\u{1b}[39;1m a\u{1b}[0m "
|
|
0 commit comments