tui: Constify functions, shrink Margin representation

This commit is contained in:
Blaž Hrastnik 2024-05-21 04:34:36 +09:00
parent e94735bbd3
commit dfcd814389
14 changed files with 102 additions and 112 deletions

View File

@ -532,8 +532,8 @@ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
surface.clear_with(doc_area, background); surface.clear_with(doc_area, background);
if cx.editor.popup_border() { if cx.editor.popup_border() {
use tui::widgets::{Block, Borders, Widget}; use tui::widgets::{Block, Widget};
Widget::render(Block::default().borders(Borders::ALL), doc_area, surface); Widget::render(Block::bordered(), doc_area, surface);
} }
markdown_doc.render(doc_area, surface, cx); markdown_doc.render(doc_area, surface, cx);

View File

@ -3,7 +3,7 @@
use helix_view::info::Info; use helix_view::info::Info;
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
use tui::text::Text; use tui::text::Text;
use tui::widgets::{Block, Borders, Paragraph, Widget}; use tui::widgets::{Block, Paragraph, Widget};
impl Component for Info { impl Component for Info {
fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) { fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
@ -23,13 +23,12 @@ fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
)); ));
surface.clear_with(area, popup_style); surface.clear_with(area, popup_style);
let block = Block::default() let block = Block::bordered()
.title(self.title.as_str()) .title(self.title.as_str())
.borders(Borders::ALL)
.border_style(popup_style); .border_style(popup_style);
let margin = Margin::horizontal(1); let margin = Margin::horizontal(1);
let inner = block.inner(area).inner(&margin); let inner = block.inner(area).inner(margin);
block.render(area, surface); block.render(area, surface);
Paragraph::new(&Text::from(self.text.as_str())) Paragraph::new(&Text::from(self.text.as_str()))

View File

@ -126,7 +126,7 @@ fn render(&mut self, area: Rect, surface: &mut Buffer, cx: &mut Context) {
let (_, sig_text_height) = crate::ui::text::required_size(&sig_text, area.width); let (_, sig_text_height) = crate::ui::text::required_size(&sig_text, area.width);
let sig_text_area = area.clip_top(1).with_height(sig_text_height); let sig_text_area = area.clip_top(1).with_height(sig_text_height);
let sig_text_area = sig_text_area.inner(&margin).intersection(surface.area); let sig_text_area = sig_text_area.inner(margin).intersection(surface.area);
let sig_text_para = Paragraph::new(&sig_text).wrap(Wrap { trim: false }); let sig_text_para = Paragraph::new(&sig_text).wrap(Wrap { trim: false });
sig_text_para.render(sig_text_area, surface); sig_text_para.render(sig_text_area, surface);
@ -153,7 +153,7 @@ fn render(&mut self, area: Rect, surface: &mut Buffer, cx: &mut Context) {
let sig_doc_para = Paragraph::new(&sig_doc) let sig_doc_para = Paragraph::new(&sig_doc)
.wrap(Wrap { trim: false }) .wrap(Wrap { trim: false })
.scroll((cx.scroll.unwrap_or_default() as u16, 0)); .scroll((cx.scroll.unwrap_or_default() as u16, 0));
sig_doc_para.render(sig_doc_area.inner(&margin), surface); sig_doc_para.render(sig_doc_area.inner(margin), surface);
} }
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {

View File

@ -351,7 +351,7 @@ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
.scroll((cx.scroll.unwrap_or_default() as u16, 0)); .scroll((cx.scroll.unwrap_or_default() as u16, 0));
let margin = Margin::all(1); let margin = Margin::all(1);
par.render(area.inner(&margin), surface); par.render(area.inner(margin), surface);
} }
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {

View File

@ -17,7 +17,7 @@
buffer::Buffer as Surface, buffer::Buffer as Surface,
layout::Constraint, layout::Constraint,
text::{Span, Spans}, text::{Span, Spans},
widgets::{Block, BorderType, Borders, Cell, Table}, widgets::{Block, BorderType, Cell, Table},
}; };
use tui::widgets::Widget; use tui::widgets::Widget;
@ -539,13 +539,12 @@ fn render_picker(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context)
let background = cx.editor.theme.get("ui.background"); let background = cx.editor.theme.get("ui.background");
surface.clear_with(area, background); surface.clear_with(area, background);
// don't like this but the lifetime sucks const BLOCK: Block<'_> = Block::bordered();
let block = Block::default().borders(Borders::ALL);
// calculate the inner area inside the box // calculate the inner area inside the box
let inner = block.inner(area); let inner = BLOCK.inner(area);
block.render(area, surface); BLOCK.render(area, surface);
// -- Render the input bar: // -- Render the input bar:
@ -690,15 +689,14 @@ fn render_preview(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context
let text = cx.editor.theme.get("ui.text"); let text = cx.editor.theme.get("ui.text");
surface.clear_with(area, background); surface.clear_with(area, background);
// don't like this but the lifetime sucks const BLOCK: Block<'_> = Block::bordered();
let block = Block::default().borders(Borders::ALL);
// calculate the inner area inside the box // calculate the inner area inside the box
let inner = block.inner(area); let inner = BLOCK.inner(area);
// 1 column gap on either side // 1 column gap on either side
let margin = Margin::horizontal(1); let margin = Margin::horizontal(1);
let inner = inner.inner(&margin); let inner = inner.inner(margin);
block.render(area, surface); BLOCK.render(area, surface);
if let Some((path, range)) = self.current_file(cx.editor) { if let Some((path, range)) = self.current_file(cx.editor) {
let preview = self.get_preview(path, cx.editor); let preview = self.get_preview(path, cx.editor);
@ -921,7 +919,7 @@ fn handle_event(&mut self, event: &Event, ctx: &mut Context) -> EventResult {
} }
fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) { fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
let block = Block::default().borders(Borders::ALL); let block = Block::bordered();
// calculate the inner area inside the box // calculate the inner area inside the box
let inner = block.inner(area); let inner = block.inner(area);

View File

@ -5,7 +5,7 @@
}; };
use tui::{ use tui::{
buffer::Buffer as Surface, buffer::Buffer as Surface,
widgets::{Block, Borders, Widget}, widgets::{Block, Widget},
}; };
use helix_core::Position; use helix_core::Position;
@ -323,8 +323,8 @@ fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
let mut inner = area; let mut inner = area;
if render_borders { if render_borders {
inner = area.inner(&Margin::all(1)); inner = area.inner(Margin::all(1));
Widget::render(Block::default().borders(Borders::ALL), area, surface); Widget::render(Block::bordered(), area, surface);
} }
let border = usize::from(render_borders); let border = usize::from(render_borders);

View File

@ -7,7 +7,7 @@
use std::sync::Arc; use std::sync::Arc;
use std::{borrow::Cow, ops::RangeFrom}; use std::{borrow::Cow, ops::RangeFrom};
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
use tui::widgets::{Block, Borders, Widget}; use tui::widgets::{Block, Widget};
use helix_core::{ use helix_core::{
unicode::segmentation::GraphemeCursor, unicode::width::UnicodeWidthStr, Position, unicode::segmentation::GraphemeCursor, unicode::width::UnicodeWidthStr, Position,
@ -457,12 +457,11 @@ pub fn render_prompt(&self, area: Rect, surface: &mut Surface, cx: &mut Context)
let background = theme.get("ui.help"); let background = theme.get("ui.help");
surface.clear_with(area, background); surface.clear_with(area, background);
let block = Block::default() let block = Block::bordered()
// .title(self.title.as_str()) // .title(self.title.as_str())
.borders(Borders::ALL)
.border_style(background); .border_style(background);
let inner = block.inner(area).inner(&Margin::horizontal(1)); let inner = block.inner(area).inner(Margin::horizontal(1));
block.render(area, surface); block.render(area, surface);
text.render(inner, surface, cx); text.render(inner, surface, cx);

View File

@ -83,24 +83,22 @@ pub fn constraints<C>(mut self, constraints: C) -> Layout
self self
} }
pub fn margin(mut self, margin: u16) -> Layout { pub const fn margin(mut self, margin: u16) -> Layout {
self.margin = Margin::all(margin); self.margin = Margin::all(margin);
self self
} }
pub fn horizontal_margin(mut self, horizontal: u16) -> Layout { pub const fn horizontal_margin(mut self, horizontal: u16) -> Layout {
self.margin.left = horizontal; self.margin.horizontal = horizontal;
self.margin.right = horizontal;
self self
} }
pub fn vertical_margin(mut self, vertical: u16) -> Layout { pub const fn vertical_margin(mut self, vertical: u16) -> Layout {
self.margin.top = vertical; self.margin.vertical = vertical;
self.margin.bottom = vertical;
self self
} }
pub fn direction(mut self, direction: Direction) -> Layout { pub const fn direction(mut self, direction: Direction) -> Layout {
self.direction = direction; self.direction = direction;
self self
} }
@ -191,7 +189,7 @@ fn split(area: Rect, layout: &Layout) -> Vec<Rect> {
.map(|_| Rect::default()) .map(|_| Rect::default())
.collect::<Vec<Rect>>(); .collect::<Vec<Rect>>();
let dest_area = area.inner(&layout.margin); let dest_area = area.inner(layout.margin);
for (i, e) in elements.iter().enumerate() { for (i, e) in elements.iter().enumerate() {
vars.insert(e.x, (i, 0)); vars.insert(e.x, (i, 0));
vars.insert(e.y, (i, 1)); vars.insert(e.y, (i, 1));

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
buffer::Buffer, buffer::Buffer,
symbols::line, symbols::line,
text::{Span, Spans}, text::Spans,
widgets::{Borders, Widget}, widgets::{Borders, Widget},
}; };
use helix_view::graphics::{Rect, Style}; use helix_view::graphics::{Rect, Style};
@ -58,6 +58,22 @@ pub struct Block<'a> {
} }
impl<'a> Block<'a> { impl<'a> Block<'a> {
pub const fn new() -> Self {
Self {
title: None,
borders: Borders::empty(),
border_style: Style::new(),
border_type: BorderType::Plain,
style: Style::new(),
}
}
pub const fn bordered() -> Self {
let mut block = Self::new();
block.borders = Borders::ALL;
block
}
pub fn title<T>(mut self, title: T) -> Block<'a> pub fn title<T>(mut self, title: T) -> Block<'a>
where where
T: Into<Spans<'a>>, T: Into<Spans<'a>>,
@ -66,34 +82,22 @@ pub fn title<T>(mut self, title: T) -> Block<'a>
self self
} }
#[deprecated( pub const fn border_style(mut self, style: Style) -> Block<'a> {
since = "0.10.0",
note = "You should use styling capabilities of `text::Spans` given as argument of the `title` method to apply styling to the title."
)]
pub fn title_style(mut self, style: Style) -> Block<'a> {
if let Some(t) = self.title {
let title = String::from(&t);
self.title = Some(Spans::from(Span::styled(title, style)));
}
self
}
pub fn border_style(mut self, style: Style) -> Block<'a> {
self.border_style = style; self.border_style = style;
self self
} }
pub fn style(mut self, style: Style) -> Block<'a> { pub const fn style(mut self, style: Style) -> Block<'a> {
self.style = style; self.style = style;
self self
} }
pub fn borders(mut self, flag: Borders) -> Block<'a> { pub const fn borders(mut self, flag: Borders) -> Block<'a> {
self.borders = flag; self.borders = flag;
self self
} }
pub fn border_type(mut self, border_type: BorderType) -> Block<'a> { pub const fn border_type(mut self, border_type: BorderType) -> Block<'a> {
self.border_type = border_type; self.border_type = border_type;
self self
} }
@ -413,9 +417,7 @@ fn inner_takes_into_account_the_borders() {
// All borders // All borders
assert_eq!( assert_eq!(
Block::default() Block::bordered().inner(Rect::default()),
.borders(Borders::ALL)
.inner(Rect::default()),
Rect { Rect {
x: 0, x: 0,
y: 0, y: 0,
@ -425,7 +427,7 @@ fn inner_takes_into_account_the_borders() {
"all borders, width=0, height=0" "all borders, width=0, height=0"
); );
assert_eq!( assert_eq!(
Block::default().borders(Borders::ALL).inner(Rect { Block::bordered().inner(Rect {
x: 0, x: 0,
y: 0, y: 0,
width: 1, width: 1,
@ -440,7 +442,7 @@ fn inner_takes_into_account_the_borders() {
"all borders, width=1, height=1" "all borders, width=1, height=1"
); );
assert_eq!( assert_eq!(
Block::default().borders(Borders::ALL).inner(Rect { Block::bordered().inner(Rect {
x: 0, x: 0,
y: 0, y: 0,
width: 2, width: 2,
@ -455,7 +457,7 @@ fn inner_takes_into_account_the_borders() {
"all borders, width=2, height=2" "all borders, width=2, height=2"
); );
assert_eq!( assert_eq!(
Block::default().borders(Borders::ALL).inner(Rect { Block::bordered().inner(Rect {
x: 0, x: 0,
y: 0, y: 0,
width: 3, width: 3,

View File

@ -72,7 +72,7 @@ pub fn height(&self) -> usize {
/// # use helix_tui::style::{Style, Color, Modifier}; /// # use helix_tui::style::{Style, Color, Modifier};
/// let items = [ListItem::new("Item 1"), ListItem::new("Item 2"), ListItem::new("Item 3")]; /// let items = [ListItem::new("Item 1"), ListItem::new("Item 2"), ListItem::new("Item 3")];
/// List::new(items) /// List::new(items)
/// .block(Block::default().title("List").borders(Borders::ALL)) /// .block(Block::bordered().title("List"))
/// .style(Style::default().fg(Color::White)) /// .style(Style::default().fg(Color::White))
/// .highlight_style(Style::default().add_modifier(Modifier::ITALIC)) /// .highlight_style(Style::default().add_modifier(Modifier::ITALIC))
/// .highlight_symbol(">>"); /// .highlight_symbol(">>");

View File

@ -37,7 +37,7 @@ fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment)
/// Spans::from(Span::styled("Second line", Style::default().fg(Color::Red))), /// Spans::from(Span::styled("Second line", Style::default().fg(Color::Red))),
/// ]); /// ]);
/// Paragraph::new(&text) /// Paragraph::new(&text)
/// .block(Block::default().title("Paragraph").borders(Borders::ALL)) /// .block(Block::bordered().title("Paragraph"))
/// .style(Style::default().fg(Color::White).bg(Color::Black)) /// .style(Style::default().fg(Color::White).bg(Color::Black))
/// .alignment(Alignment::Center) /// .alignment(Alignment::Center)
/// .wrap(Wrap { trim: true }); /// .wrap(Wrap { trim: true });

View File

@ -23,7 +23,7 @@
// let size = f.size(); // let size = f.size();
// let text = Text::from(SAMPLE_STRING); // let text = Text::from(SAMPLE_STRING);
// let paragraph = Paragraph::new(&text) // let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .alignment(alignment) // .alignment(alignment)
// .wrap(Wrap { trim: true }); // .wrap(Wrap { trim: true });
// f.render_widget(paragraph, size); // f.render_widget(paragraph, size);
@ -90,7 +90,7 @@
// let size = f.size(); // let size = f.size();
// let text = Text::from(s); // let text = Text::from(s);
// let paragraph = Paragraph::new(&text) // let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .wrap(Wrap { trim: true }); // .wrap(Wrap { trim: true });
// f.render_widget(paragraph, size); // f.render_widget(paragraph, size);
// }) // })
@ -122,7 +122,7 @@
// let size = f.size(); // let size = f.size();
// let text = Text::from(s); // let text = Text::from(s);
// let paragraph = Paragraph::new(&text) // let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .wrap(Wrap { trim: true }); // .wrap(Wrap { trim: true });
// f.render_widget(paragraph, size); // f.render_widget(paragraph, size);
// }) // })
@ -156,7 +156,7 @@
// .draw(|f| { // .draw(|f| {
// let size = f.size(); // let size = f.size();
// let text = Text::from(line); // let text = Text::from(line);
// let paragraph = Paragraph::new(&text).block(Block::default().borders(Borders::ALL)); // let paragraph = Paragraph::new(&text).block(Block::bordered());
// f.render_widget(paragraph, size); // f.render_widget(paragraph, size);
// }) // })
// .unwrap(); // .unwrap();
@ -175,7 +175,7 @@
// "段落现在可以水平滚动了!\nParagraph can scroll horizontally!\nShort line", // "段落现在可以水平滚动了!\nParagraph can scroll horizontally!\nShort line",
// ); // );
// let paragraph = Paragraph::new(&text) // let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .alignment(alignment) // .alignment(alignment)
// .scroll(scroll); // .scroll(scroll);
// f.render_widget(paragraph, size); // f.render_widget(paragraph, size);

View File

@ -24,7 +24,7 @@
// Row::new(vec!["Row41", "Row42", "Row43"]), // Row::new(vec!["Row41", "Row42", "Row43"]),
// ]) // ])
// .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1)) // .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1))
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .widths(&[ // .widths(&[
// Constraint::Length(5), // Constraint::Length(5),
// Constraint::Length(5), // Constraint::Length(5),
@ -122,7 +122,7 @@
// Row::new(vec!["Row41", "Row42", "Row43"]), // Row::new(vec!["Row41", "Row42", "Row43"]),
// ]) // ])
// .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1)) // .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1))
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .widths(widths); // .widths(widths);
// f.render_widget(table, size); // f.render_widget(table, size);
// }) // })
@ -210,7 +210,7 @@
// Row::new(vec!["Row41", "Row42", "Row43"]), // Row::new(vec!["Row41", "Row42", "Row43"]),
// ]) // ])
// .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1)) // .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1))
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .widths(widths) // .widths(widths)
// .column_spacing(0); // .column_spacing(0);
// f.render_widget(table, size); // f.render_widget(table, size);
@ -316,7 +316,7 @@
// Row::new(vec!["Row41", "Row42", "Row43"]), // Row::new(vec!["Row41", "Row42", "Row43"]),
// ]) // ])
// .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1)) // .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1))
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .widths(widths); // .widths(widths);
// f.render_widget(table, size); // f.render_widget(table, size);
// }) // })
@ -425,7 +425,7 @@
// Row::new(vec!["Row41", "Row42", "Row43"]), // Row::new(vec!["Row41", "Row42", "Row43"]),
// ]) // ])
// .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1)) // .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1))
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .widths(widths) // .widths(widths)
// .column_spacing(0); // .column_spacing(0);
// f.render_widget(table, size); // f.render_widget(table, size);
@ -530,7 +530,7 @@
// Row::new(vec!["Row41", "Row42", "Row43"]).height(2), // Row::new(vec!["Row41", "Row42", "Row43"]).height(2),
// ]) // ])
// .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1)) // .header(Row::new(vec!["Head1", "Head2", "Head3"]).bottom_margin(1))
// .block(Block::default().borders(Borders::ALL)) // .block(Block::bordered())
// .highlight_symbol(">> ") // .highlight_symbol(">> ")
// .widths(&[ // .widths(&[
// Constraint::Length(5), // Constraint::Length(5),

View File

@ -25,62 +25,52 @@ fn default() -> Self {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Margin { pub struct Margin {
pub left: u16, pub horizontal: u16,
pub right: u16, pub vertical: u16,
pub top: u16,
pub bottom: u16,
} }
impl Margin { impl Margin {
pub fn none() -> Self { pub fn none() -> Self {
Self { Self {
left: 0, horizontal: 0,
right: 0, vertical: 0,
top: 0,
bottom: 0,
} }
} }
/// Set uniform margin for all sides. /// Set uniform margin for all sides.
pub fn all(value: u16) -> Self { pub const fn all(value: u16) -> Self {
Self { Self {
left: value, horizontal: value,
right: value, vertical: value,
top: value,
bottom: value,
} }
} }
/// Set the margin of left and right sides to specified value. /// Set the margin of left and right sides to specified value.
pub fn horizontal(value: u16) -> Self { pub const fn horizontal(value: u16) -> Self {
Self { Self {
left: value, horizontal: value,
right: value, vertical: 0,
top: 0,
bottom: 0,
} }
} }
/// Set the margin of top and bottom sides to specified value. /// Set the margin of top and bottom sides to specified value.
pub fn vertical(value: u16) -> Self { pub const fn vertical(value: u16) -> Self {
Self { Self {
left: 0, horizontal: 0,
right: 0, vertical: value,
top: value,
bottom: value,
} }
} }
/// Get the total width of the margin (left + right) /// Get the total width of the margin (left + right)
pub fn width(&self) -> u16 { pub const fn width(&self) -> u16 {
self.left + self.right self.horizontal * 2
} }
/// Get the total height of the margin (top + bottom) /// Get the total height of the margin (top + bottom)
pub fn height(&self) -> u16 { pub const fn height(&self) -> u16 {
self.top + self.bottom self.vertical * 2
} }
} }
@ -181,13 +171,13 @@ pub fn with_width(self, width: u16) -> Rect {
Self::new(self.x, self.y, width, self.height) Self::new(self.x, self.y, width, self.height)
} }
pub fn inner(self, margin: &Margin) -> Rect { pub fn inner(self, margin: Margin) -> Rect {
if self.width < margin.width() || self.height < margin.height() { if self.width < margin.width() || self.height < margin.height() {
Rect::default() Rect::default()
} else { } else {
Rect { Rect {
x: self.x + margin.left, x: self.x + margin.horizontal,
y: self.y + margin.top, y: self.y + margin.vertical,
width: self.width - margin.width(), width: self.width - margin.width(),
height: self.height - margin.height(), height: self.height - margin.height(),
} }
@ -459,7 +449,13 @@ pub struct Style {
} }
impl Default for Style { impl Default for Style {
fn default() -> Style { fn default() -> Self {
Self::new()
}
}
impl Style {
pub const fn new() -> Self {
Style { Style {
fg: None, fg: None,
bg: None, bg: None,
@ -469,12 +465,10 @@ fn default() -> Style {
sub_modifier: Modifier::empty(), sub_modifier: Modifier::empty(),
} }
} }
}
impl Style {
/// Returns a `Style` resetting all properties. /// Returns a `Style` resetting all properties.
pub fn reset() -> Style { pub const fn reset() -> Self {
Style { Self {
fg: Some(Color::Reset), fg: Some(Color::Reset),
bg: Some(Color::Reset), bg: Some(Color::Reset),
underline_color: None, underline_color: None,
@ -494,7 +488,7 @@ pub fn reset() -> Style {
/// let diff = Style::default().fg(Color::Red); /// let diff = Style::default().fg(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().fg(Color::Red)); /// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
/// ``` /// ```
pub fn fg(mut self, color: Color) -> Style { pub const fn fg(mut self, color: Color) -> Style {
self.fg = Some(color); self.fg = Some(color);
self self
} }
@ -509,7 +503,7 @@ pub fn fg(mut self, color: Color) -> Style {
/// let diff = Style::default().bg(Color::Red); /// let diff = Style::default().bg(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().bg(Color::Red)); /// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
/// ``` /// ```
pub fn bg(mut self, color: Color) -> Style { pub const fn bg(mut self, color: Color) -> Style {
self.bg = Some(color); self.bg = Some(color);
self self
} }
@ -524,7 +518,7 @@ pub fn bg(mut self, color: Color) -> Style {
/// let diff = Style::default().underline_color(Color::Red); /// let diff = Style::default().underline_color(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().underline_color(Color::Red)); /// assert_eq!(style.patch(diff), Style::default().underline_color(Color::Red));
/// ``` /// ```
pub fn underline_color(mut self, color: Color) -> Style { pub const fn underline_color(mut self, color: Color) -> Style {
self.underline_color = Some(color); self.underline_color = Some(color);
self self
} }
@ -539,7 +533,7 @@ pub fn underline_color(mut self, color: Color) -> Style {
/// let diff = Style::default().underline_style(UnderlineStyle::Curl); /// let diff = Style::default().underline_style(UnderlineStyle::Curl);
/// assert_eq!(style.patch(diff), Style::default().underline_style(UnderlineStyle::Curl)); /// assert_eq!(style.patch(diff), Style::default().underline_style(UnderlineStyle::Curl));
/// ``` /// ```
pub fn underline_style(mut self, style: UnderlineStyle) -> Style { pub const fn underline_style(mut self, style: UnderlineStyle) -> Style {
self.underline_style = Some(style); self.underline_style = Some(style);
self self
} }