Skip to content

Commit 5cc8ea6

Browse files
authored
feat(ascii): parse multi-byte unicode chars correctly + docs (#936)
* fix: parse multi-byte unicode chars correctly * tests(ascii): add unicode block assertation to render * docs(ascii): add AsciiArt usage example * docs(ascii): fix ascii doc onefetch link * docs(ascii): add badges to readme * docs(ascii): add no_run to example
1 parent 1716519 commit 5cc8ea6

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
.DS_Store
88
result
99
**/generated-*
10+
**/.idea

ascii/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ascii
22

3+
[![crates.io](https://img.shields.io/crates/v/onefetch-ascii)](https://crates.io/crates/onefetch-ascii)
4+
[![docs.rs](https://img.shields.io/docsrs/onefetch-ascii)](https://docs.rs/onefetch-ascii)
5+
36
Provides the primary interface to display ascii art to the terminal.
47

58
More info [here](https://github.com/o2sh/onefetch/wiki/ascii-art).

ascii/src/lib.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,52 @@
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+
146
use owo_colors::{AnsiColors, DynColors, OwoColorize, Style};
247
use std::fmt::Write;
348

49+
/// Renders an ascii template with the given colors truncated to the correct width.
450
pub struct AsciiArt<'a> {
551
content: Box<dyn 'a + Iterator<Item = &'a str>>,
652
colors: &'a [DynColors],
@@ -211,9 +257,10 @@ fn add_styled_segment(base: &mut String, segment: &str, color: DynColors, bold:
211257
type ParseResult<'a, R> = Option<(&'a str, R)>;
212258

213259
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()?;
215262
let result = predicate(token)?;
216-
Some((s.get(1..).unwrap(), result))
263+
Some((chars.as_str(), result))
217264
}
218265

219266
// Parsers
@@ -313,6 +360,12 @@ mod test {
313360
"\u{1b}[39;1m \u{1b}[0m"
314361
);
315362

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+
316369
assert_eq!(
317370
Tokens(" {1} {5} {9} a").render(&colors_shim, 4, 10, true),
318371
"\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

Comments
 (0)