mirror of
https://github.com/o2sh/onefetch
synced 2024-06-28 13:44:36 +00:00
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
This commit is contained in:
parent
17165192bf
commit
5cc8ea62b8
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -7,3 +7,4 @@
|
|||
.DS_Store
|
||||
result
|
||||
**/generated-*
|
||||
**/.idea
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
# ascii
|
||||
|
||||
[![crates.io](https://img.shields.io/crates/v/onefetch-ascii)](https://crates.io/crates/onefetch-ascii)
|
||||
[![docs.rs](https://img.shields.io/docsrs/onefetch-ascii)](https://docs.rs/onefetch-ascii)
|
||||
|
||||
Provides the primary interface to display ascii art to the terminal.
|
||||
|
||||
More info [here](https://github.com/o2sh/onefetch/wiki/ascii-art).
|
||||
|
|
|
@ -1,6 +1,52 @@
|
|||
//! # onefetch-ascii
|
||||
//!
|
||||
//! Provides the ascii template interface for [onefetch](https://github.com/o2sh/onefetch).
|
||||
//!
|
||||
//! ```rust,no_run
|
||||
//! use onefetch_ascii::AsciiArt;
|
||||
//! use owo_colors::{DynColors, AnsiColors};
|
||||
//!
|
||||
//! const ASCII: &str = r#"
|
||||
//! {2} .:--::////::--.`
|
||||
//! {1} `/yNMMNho{2}////////////:.
|
||||
//! {1} `+NMMMMMMMMmy{2}/////////////:`
|
||||
//! {0} `-:::{1}ohNMMMMMMMNy{2}/////////////:`
|
||||
//! {0} .::::::::{1}odMMMMMMMNy{2}/////////////-
|
||||
//! {0} -:::::::::::{1}/hMMMMMMMmo{2}////////////-
|
||||
//! {0} .::::::::::::::{1}oMMMMMMMMh{2}////////////-
|
||||
//! {0}`:::::::::::::{1}/dMMMMMMMMMMNo{2}///////////`
|
||||
//! {0}-::::::::::::{1}sMMMMMMmMMMMMMMy{2}//////////-
|
||||
//! {0}-::::::::::{1}/dMMMMMMs{0}:{1}+NMMMMMMd{2}/////////:
|
||||
//! {0}-:::::::::{1}+NMMMMMm/{0}:::{1}/dMMMMMMm+{2}///////:
|
||||
//! {0}-::::::::{1}sMMMMMMh{0}:::::::{1}dMMMMMMm+{2}//////-
|
||||
//! {0}`:::::::{1}sMMMMMMy{0}:::::::::{1}dMMMMMMm+{2}/////`
|
||||
//! {0} .:::::{1}sMMMMMMs{0}:::::::::::{1}mMMMMMMd{2}////-
|
||||
//! {0} -:::{1}sMMMMMMy{0}::::::::::::{1}/NMMMMMMh{2}//-
|
||||
//! {0} .:{1}+MMMMMMd{0}::::::::::::::{1}oMMMMMMMo{2}-
|
||||
//! {1} `yMMMMMN/{0}:::::::::::::::{1}hMMMMMh.
|
||||
//! {1} -yMMMo{0}::::::::::::::::{1}/MMMy-
|
||||
//! {1} `/s{0}::::::::::::::::::{1}o/`
|
||||
//! {0} ``.---::::---..`
|
||||
//! "#;
|
||||
//!
|
||||
//! let colors = vec![
|
||||
//! DynColors::Ansi(AnsiColors::Blue),
|
||||
//! DynColors::Ansi(AnsiColors::Default),
|
||||
//! DynColors::Ansi(AnsiColors::BrightBlue)
|
||||
//! ];
|
||||
//!
|
||||
//! let art = AsciiArt::new(ASCII, colors.as_slice(), true);
|
||||
//!
|
||||
//! for line in art {
|
||||
//! println!("{line}")
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
|
||||
use owo_colors::{AnsiColors, DynColors, OwoColorize, Style};
|
||||
use std::fmt::Write;
|
||||
|
||||
/// Renders an ascii template with the given colors truncated to the correct width.
|
||||
pub struct AsciiArt<'a> {
|
||||
content: Box<dyn 'a + Iterator<Item = &'a str>>,
|
||||
colors: &'a [DynColors],
|
||||
|
@ -211,9 +257,10 @@ fn add_styled_segment(base: &mut String, segment: &str, color: DynColors, bold:
|
|||
type ParseResult<'a, R> = Option<(&'a str, R)>;
|
||||
|
||||
fn token<R>(s: &str, predicate: impl FnOnce(char) -> Option<R>) -> ParseResult<R> {
|
||||
let token = s.chars().next()?;
|
||||
let mut chars = s.chars();
|
||||
let token = chars.next()?;
|
||||
let result = predicate(token)?;
|
||||
Some((s.get(1..).unwrap(), result))
|
||||
Some((chars.as_str(), result))
|
||||
}
|
||||
|
||||
// Parsers
|
||||
|
@ -313,6 +360,12 @@ mod test {
|
|||
"\u{1b}[39;1m \u{1b}[0m"
|
||||
);
|
||||
|
||||
// https://github.com/o2sh/onefetch/issues/935
|
||||
assert_eq!(
|
||||
Tokens("███").render(Vec::new().as_slice(), 0, 3, true),
|
||||
"\u{1b}[39;1m███\u{1b}[0m"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Tokens(" {1} {5} {9} a").render(&colors_shim, 4, 10, true),
|
||||
"\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 "
|
||||
|
|
Loading…
Reference in New Issue
Block a user