Skip to content

Commit fc73e94

Browse files
committed
feat: Add support for styled Title
1 parent 873e2f4 commit fc73e94

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

src/level.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a> Level<'a> {
6262
/// <div class="warning">
6363
///
6464
/// Text passed to this function is considered "untrusted input", as such
65-
/// all text is passed through a normalization function. Pre-styled text is
65+
/// all text is passed through a normalization function. Styled text is
6666
/// not allowed to be passed to this function.
6767
///
6868
/// </div>
@@ -80,14 +80,37 @@ impl<'a> Level<'a> {
8080
level: self,
8181
id: None,
8282
text: text.into(),
83+
allows_styling: false,
84+
}
85+
}
86+
87+
/// Creates a [`Title`] that is allowed to be styled, for more info see the
88+
/// warning below.
89+
///
90+
/// See [`Group::with_title`][crate::Group::with_title]
91+
///
92+
/// <div class="warning">
93+
///
94+
/// Text passed to this function is allowed to be styled, as such all
95+
/// text is considered "trusted input" and has no normalizations applied to
96+
/// it. [`normalize_untrusted_str`](crate::normalize_untrusted_str) can be
97+
/// used to normalize untrusted text before it is passed to this function.
98+
///
99+
/// </div>
100+
pub fn secondary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a> {
101+
Title {
102+
level: self,
103+
id: None,
104+
text: text.into(),
105+
allows_styling: true,
83106
}
84107
}
85108

86109
/// A text [`Element`][crate::Element] in a [`Group`][crate::Group]
87110
///
88111
/// <div class="warning">
89112
///
90-
/// Text passed to this function is allowed to be pre-styled, as such all
113+
/// Text passed to this function is allowed to be styled, as such all
91114
/// text is considered "trusted input" and has no normalizations applied to
92115
/// it. [`normalize_untrusted_str`](crate::normalize_untrusted_str) can be
93116
/// used to normalize untrusted text before it is passed to this function.

src/renderer/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ impl Renderer {
596596
label_width
597597
});
598598

599-
let (title_str, style) = if title.is_pre_styled() {
599+
let (title_str, style) = if title.allows_styling() {
600600
(title.text().to_owned(), ElementStyle::NoStyle)
601601
} else {
602602
(normalize_whitespace(title.text()), title_element_style)
@@ -2609,7 +2609,7 @@ trait MessageOrTitle {
26092609
fn level(&self) -> &Level<'_>;
26102610
fn id(&self) -> Option<&Id<'_>>;
26112611
fn text(&self) -> &str;
2612-
fn is_pre_styled(&self) -> bool;
2612+
fn allows_styling(&self) -> bool;
26132613
}
26142614

26152615
impl MessageOrTitle for Title<'_> {
@@ -2622,8 +2622,8 @@ impl MessageOrTitle for Title<'_> {
26222622
fn text(&self) -> &str {
26232623
self.text.as_ref()
26242624
}
2625-
fn is_pre_styled(&self) -> bool {
2626-
false
2625+
fn allows_styling(&self) -> bool {
2626+
self.allows_styling
26272627
}
26282628
}
26292629

@@ -2637,7 +2637,7 @@ impl MessageOrTitle for Message<'_> {
26372637
fn text(&self) -> &str {
26382638
self.text.as_ref()
26392639
}
2640-
fn is_pre_styled(&self) -> bool {
2640+
fn allows_styling(&self) -> bool {
26412641
true
26422642
}
26432643
}

src/snippet.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub struct Title<'a> {
138138
pub(crate) level: Level<'a>,
139139
pub(crate) id: Option<Id<'a>>,
140140
pub(crate) text: Cow<'a, str>,
141+
pub(crate) allows_styling: bool,
141142
}
142143

143144
impl<'a> Title<'a> {

tests/color/styled_title.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use c::cnb_runtime;
3030
.annotation(AnnotationKind::Primary.span(65..86).label(label_1))
3131
.annotation(AnnotationKind::Context.span(53..64).label(label_2)),
3232
),
33-
Group::with_title(Level::HELP.primary_title(title_2)).element(
33+
Group::with_title(Level::HELP.secondary_title(title_2)).element(
3434
Snippet::source(source)
3535
.path("src/main.rs")
3636
.annotation(AnnotationKind::Primary.span(4..5).label(label_3))

tests/color/styled_title.term.svg

Lines changed: 3 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)