From 1fc04dfa602a07a7af5ed6624e065bdcc747cf79 Mon Sep 17 00:00:00 2001 From: tison Date: Mon, 5 Aug 2024 09:25:08 +0800 Subject: [PATCH 1/3] feat: allow construct Style constantly --- src/style.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/style.rs b/src/style.rs index 3058767..f725410 100644 --- a/src/style.rs +++ b/src/style.rs @@ -60,8 +60,20 @@ impl Style { /// let style = Style::new(); /// println!("{}", style.paint("hi")); /// ``` - pub fn new() -> Style { - Style::default() + pub const fn new() -> Style { + Style { + foreground: None, + background: None, + is_bold: false, + is_dimmed: false, + is_italic: false, + is_underline: false, + is_blink: false, + is_reverse: false, + is_hidden: false, + is_strikethrough: false, + prefix_with_reset: false, + } } /// Returns a [`Style`] with the `Style.prefix_with_reset` property set. @@ -278,19 +290,7 @@ impl Default for Style { /// assert_eq!("txt", Style::default().paint("txt").to_string()); /// ``` fn default() -> Style { - Style { - foreground: None, - background: None, - is_bold: false, - is_dimmed: false, - is_italic: false, - is_underline: false, - is_blink: false, - is_reverse: false, - is_hidden: false, - is_strikethrough: false, - prefix_with_reset: false, - } + Style::new() } } From 1b5ddeebaf999fd4583c7684d801503fb3bde267 Mon Sep 17 00:00:00 2001 From: tison Date: Mon, 5 Aug 2024 17:48:23 +0800 Subject: [PATCH 2/3] chore: more consts --- src/style.rs | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/style.rs b/src/style.rs index f725410..d4d3b7c 100644 --- a/src/style.rs +++ b/src/style.rs @@ -273,8 +273,8 @@ impl Style { /// assert_eq!(true, Style::default().is_plain()); /// assert_eq!(false, Style::default().bold().is_plain()); /// ``` - pub fn is_plain(self) -> bool { - self == Style::default() + pub const fn is_plain(self) -> bool { + self == Style::new() } } @@ -404,10 +404,10 @@ impl Color { /// let style = Color::Red.normal(); /// println!("{}", style.paint("hi")); /// ``` - pub fn normal(self) -> Style { + pub const fn normal(self) -> Style { Style { foreground: Some(self), - ..Style::default() + ..Style::new() } } @@ -422,11 +422,11 @@ impl Color { /// let style = Color::Green.bold(); /// println!("{}", style.paint("hey")); /// ``` - pub fn bold(self) -> Style { + pub const fn bold(self) -> Style { Style { foreground: Some(self), is_bold: true, - ..Style::default() + ..Style::new() } } @@ -441,11 +441,11 @@ impl Color { /// let style = Color::Yellow.dimmed(); /// println!("{}", style.paint("sup")); /// ``` - pub fn dimmed(self) -> Style { + pub const fn dimmed(self) -> Style { Style { foreground: Some(self), is_dimmed: true, - ..Style::default() + ..Style::new() } } @@ -460,11 +460,11 @@ impl Color { /// let style = Color::Blue.italic(); /// println!("{}", style.paint("greetings")); /// ``` - pub fn italic(self) -> Style { + pub const fn italic(self) -> Style { Style { foreground: Some(self), is_italic: true, - ..Style::default() + ..Style::new() } } @@ -479,11 +479,11 @@ impl Color { /// let style = Color::Purple.underline(); /// println!("{}", style.paint("salutations")); /// ``` - pub fn underline(self) -> Style { + pub const fn underline(self) -> Style { Style { foreground: Some(self), is_underline: true, - ..Style::default() + ..Style::new() } } @@ -498,11 +498,11 @@ impl Color { /// let style = Color::Cyan.blink(); /// println!("{}", style.paint("wazzup")); /// ``` - pub fn blink(self) -> Style { + pub const fn blink(self) -> Style { Style { foreground: Some(self), is_blink: true, - ..Style::default() + ..Style::new() } } @@ -517,11 +517,11 @@ impl Color { /// let style = Color::Black.reverse(); /// println!("{}", style.paint("aloha")); /// ``` - pub fn reverse(self) -> Style { + pub const fn reverse(self) -> Style { Style { foreground: Some(self), is_reverse: true, - ..Style::default() + ..Style::new() } } @@ -536,11 +536,11 @@ impl Color { /// let style = Color::White.hidden(); /// println!("{}", style.paint("ahoy")); /// ``` - pub fn hidden(self) -> Style { + pub const fn hidden(self) -> Style { Style { foreground: Some(self), is_hidden: true, - ..Style::default() + ..Style::new() } } @@ -555,11 +555,11 @@ impl Color { /// let style = Color::Fixed(244).strikethrough(); /// println!("{}", style.paint("yo")); /// ``` - pub fn strikethrough(self) -> Style { + pub const fn strikethrough(self) -> Style { Style { foreground: Some(self), is_strikethrough: true, - ..Style::default() + ..Style::new() } } @@ -574,11 +574,11 @@ impl Color { /// let style = Color::Fixed(244).reset_before_style(); /// println!("{}", style.paint("yo")); /// ``` - pub fn reset_before_style(self) -> Style { + pub const fn reset_before_style(self) -> Style { Style { foreground: Some(self), prefix_with_reset: true, - ..Style::default() + ..Style::new() } } @@ -593,11 +593,11 @@ impl Color { /// let style = Color::Rgb(31, 31, 31).on(Color::White); /// println!("{}", style.paint("eyyyy")); /// ``` - pub fn on(self, background: Color) -> Style { + pub const fn on(self, background: Color) -> Style { Style { foreground: Some(self), background: Some(background), - ..Style::default() + ..Style::new() } } } From 62b10f015b2eaa8a16e70bd0f4938db2d17a8597 Mon Sep 17 00:00:00 2001 From: tison Date: Mon, 5 Aug 2024 17:50:36 +0800 Subject: [PATCH 3/3] revert const that requires const_trait_impl unstable feature Signed-off-by: tison --- src/style.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/style.rs b/src/style.rs index d4d3b7c..3f488bd 100644 --- a/src/style.rs +++ b/src/style.rs @@ -273,7 +273,7 @@ impl Style { /// assert_eq!(true, Style::default().is_plain()); /// assert_eq!(false, Style::default().bold().is_plain()); /// ``` - pub const fn is_plain(self) -> bool { + pub fn is_plain(self) -> bool { self == Style::new() } }