Skip to content

Commit bf84c62

Browse files
authored
feat(core): Add a has_modifier() method to Style (#2267)
Resolves #2264
1 parent 3bdb9fe commit bf84c62

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

ratatui-core/src/style.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,22 @@ impl Style {
433433
self
434434
}
435435

436+
/// Returns `true` if the style has the given modifier set.
437+
///
438+
/// ## Examples
439+
///
440+
/// ```rust
441+
/// use ratatui_core::style::{Modifier, Style};
442+
///
443+
/// let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC);
444+
/// assert!(style.has_modifier(Modifier::BOLD));
445+
/// assert!(style.has_modifier(Modifier::ITALIC));
446+
/// assert!(!style.has_modifier(Modifier::UNDERLINED));
447+
/// ```
448+
pub const fn has_modifier(self, modifier: Modifier) -> bool {
449+
self.add_modifier.contains(modifier) && !self.sub_modifier.contains(modifier)
450+
}
451+
436452
/// Results in a combined style that is equivalent to applying the two individual styles to
437453
/// a style one after the other.
438454
///
@@ -812,6 +828,28 @@ mod tests {
812828
assert_eq!(ALL, ALL_SHORT);
813829
}
814830

831+
#[test]
832+
fn has_modifier_checks() {
833+
// basic presence
834+
let style = Style::new().add_modifier(Modifier::BOLD | Modifier::ITALIC);
835+
assert!(style.has_modifier(Modifier::BOLD));
836+
assert!(style.has_modifier(Modifier::ITALIC));
837+
assert!(!style.has_modifier(Modifier::UNDERLINED));
838+
839+
// removal prevents the modifier from being reported as present
840+
let style = Style::new()
841+
.add_modifier(Modifier::BOLD | Modifier::ITALIC)
842+
.remove_modifier(Modifier::ITALIC);
843+
assert!(style.has_modifier(Modifier::BOLD));
844+
assert!(!style.has_modifier(Modifier::ITALIC));
845+
846+
// patching with a style that removes a modifier clears it
847+
let style = Style::new().add_modifier(Modifier::BOLD | Modifier::ITALIC);
848+
let patched = style.patch(Style::new().remove_modifier(Modifier::ITALIC));
849+
assert!(patched.has_modifier(Modifier::BOLD));
850+
assert!(!patched.has_modifier(Modifier::ITALIC));
851+
}
852+
815853
#[rstest]
816854
#[case(Style::new().black(), Color::Black)]
817855
#[case(Style::new().red(), Color::Red)]

0 commit comments

Comments
 (0)