From 07301930560a5f162998e651392ac0bc7c2b5f5f Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 20 Jun 2024 15:10:19 -0400 Subject: [PATCH] Rewrite cursup parsing without regexes --- cursive-core/Cargo.toml | 5 - cursive-core/src/printer.rs | 7 + cursive-core/src/theme/style.rs | 46 ++-- cursive-core/src/utils/markup/cursup.rs | 265 ++++++++++++++++++------ cursive/Cargo.toml | 1 - 5 files changed, 248 insertions(+), 76 deletions(-) diff --git a/cursive-core/Cargo.toml b/cursive-core/Cargo.toml index 48fcf655..0ce23831 100644 --- a/cursive-core/Cargo.toml +++ b/cursive-core/Cargo.toml @@ -60,10 +60,6 @@ version = "0.8" default-features = false version = "0.4" -[dependencies.regex] -optional = true -version = "1" - [dependencies.pulldown-cmark] default-features = false optional = true @@ -73,7 +69,6 @@ version = "0.11" default = [] doc-cfg = [] builder = ["inventory", "cursive-macros/builder"] -cursup = ["regex"] markdown = ["pulldown-cmark"] ansi = ["ansi-parser"] unstable_scroll = [] # Deprecated feature, remove in next version diff --git a/cursive-core/src/printer.rs b/cursive-core/src/printer.rs index 4a49d0b5..ccb7b3b7 100644 --- a/cursive-core/src/printer.rs +++ b/cursive-core/src/printer.rs @@ -300,6 +300,13 @@ impl<'a, 'b> Printer<'a, 'b> { } } + /// Fills a rectangle using the given character. + pub fn print_rect(&self, rect: Rect, c: &str) { + for y in rect.top()..=rect.bottom() { + self.print_hline((rect.left(), y), rect.width(), c); + } + } + /// Prints a horizontal line using the given character. pub fn print_hline>(&self, start: T, width: usize, c: &str) { let start = start.into(); diff --git a/cursive-core/src/theme/style.rs b/cursive-core/src/theme/style.rs index 2c6dc52f..d62b93a9 100644 --- a/cursive-core/src/theme/style.rs +++ b/cursive-core/src/theme/style.rs @@ -165,19 +165,29 @@ impl Style { } } +fn parse_single_style(s: &str) -> Result { + if let Some(s) = s.strip_prefix("back.") { + if let Ok(back) = s.parse::() { + return Ok(ColorStyle::back(back).into()); + } + } + + if let Ok(front) = s.parse::() { + return Ok(front.into()); + } + + if let Ok(effect) = s.parse::() { + return Ok(effect.into()); + } + + Err(super::NoSuchColor) +} + impl FromStr for Style { type Err = super::NoSuchColor; fn from_str(s: &str) -> Result { - if let Ok(front) = s.parse::() { - return Ok(front.into()); - } - - if let Ok(effect) = s.parse::() { - return Ok(effect.into()); - } - - Err(super::NoSuchColor) + s.split('+').map(parse_single_style).collect() } } @@ -374,23 +384,35 @@ impl From for StyleType { /// Will use the last non-`None` color, and will combine all effects. impl<'a> FromIterator<&'a Style> for Style { fn from_iter>(iter: I) -> Style { + combine_styles(iter) + } +} + +impl AsRef