Skip to content

Commit

Permalink
Rewrite cursup parsing without regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Jun 20, 2024
1 parent 52d3dbd commit 0730193
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 76 deletions.
5 changes: 0 additions & 5 deletions cursive-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions cursive-core/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Into<Vec2>>(&self, start: T, width: usize, c: &str) {
let start = start.into();
Expand Down
46 changes: 34 additions & 12 deletions cursive-core/src/theme/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,29 @@ impl Style {
}
}

fn parse_single_style(s: &str) -> Result<Style, super::NoSuchColor> {
if let Some(s) = s.strip_prefix("back.") {
if let Ok(back) = s.parse::<ColorType>() {
return Ok(ColorStyle::back(back).into());
}
}

if let Ok(front) = s.parse::<ColorType>() {
return Ok(front.into());
}

if let Ok(effect) = s.parse::<Effect>() {
return Ok(effect.into());
}

Err(super::NoSuchColor)
}

impl FromStr for Style {
type Err = super::NoSuchColor;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(front) = s.parse::<ColorType>() {
return Ok(front.into());
}

if let Ok(effect) = s.parse::<Effect>() {
return Ok(effect.into());
}

Err(super::NoSuchColor)
s.split('+').map(parse_single_style).collect()
}
}

Expand Down Expand Up @@ -374,23 +384,35 @@ impl From<PaletteStyle> for StyleType {
/// Will use the last non-`None` color, and will combine all effects.
impl<'a> FromIterator<&'a Style> for Style {
fn from_iter<I: IntoIterator<Item = &'a Style>>(iter: I) -> Style {
combine_styles(iter)
}
}

impl AsRef<Style> for Style {
fn as_ref(&self) -> &Style {
self
}
}

fn combine_styles<S: AsRef<Style>>(styles: impl IntoIterator<Item=S>) -> Style {
let mut color = ColorStyle::inherit_parent();
let mut effects = Effects::empty();

for style in iter {
for style in styles {
let style = style.as_ref();
color = ColorStyle::merge(color, style.color);
effects = Effects::merge(effects, style.effects);
}

Style { effects, color }
}
}

/// Creates a new `Style` by merging all given styles.
///
/// Will use the last non-`None` color, and will combine all effects.
impl<T: Into<Style>> FromIterator<T> for Style {
// TODO: Find some common implementation for both?
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Style {
iter.into_iter().map(Into::into).collect()
combine_styles(iter.into_iter().map(Into::into))
}
}
Loading

0 comments on commit 0730193

Please sign in to comment.