This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
Added feature to align title of the block #462
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
57bd6af
Added ability to set title alignment, added tests, modified blocks ex…
olekslitus 4416135
Added test for inner with title in block
olekslitus b95894d
Updated block example to show center alignment
olekslitus 31b9744
Formatting fixed
olekslitus ce04c24
Updated tests to use lamdas and be more concise. Updated title alignm…
olekslitus File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use crate::{ | ||
buffer::Buffer, | ||
layout::Rect, | ||
layout::{Alignment, Rect}, | ||
style::Style, | ||
symbols::line, | ||
text::{Span, Spans}, | ||
|
@@ -45,6 +45,9 @@ impl BorderType { | |
pub struct Block<'a> { | ||
/// Optional title place on the upper left of the block | ||
title: Option<Spans<'a>>, | ||
/// Title alignment. The default is top left of the block, but one can choose to place | ||
/// title in the top middle, or top right of the block | ||
title_alignment: Alignment, | ||
/// Visible borders | ||
borders: Borders, | ||
/// Border style | ||
|
@@ -60,6 +63,7 @@ impl<'a> Default for Block<'a> { | |
fn default() -> Block<'a> { | ||
Block { | ||
title: None, | ||
title_alignment: Alignment::Left, | ||
borders: Borders::NONE, | ||
border_style: Default::default(), | ||
border_type: BorderType::Plain, | ||
|
@@ -89,6 +93,11 @@ impl<'a> Block<'a> { | |
self | ||
} | ||
|
||
pub fn title_alignment(mut self, alignment: Alignment) -> Block<'a> { | ||
self.title_alignment = alignment; | ||
self | ||
} | ||
|
||
pub fn border_style(mut self, style: Style) -> Block<'a> { | ||
self.border_style = style; | ||
self | ||
|
@@ -192,19 +201,35 @@ impl<'a> Widget for Block<'a> { | |
.set_style(self.border_style); | ||
} | ||
|
||
// Title | ||
if let Some(title) = self.title { | ||
let lx = if self.borders.intersects(Borders::LEFT) { | ||
let left_border_dx = if self.borders.intersects(Borders::LEFT) { | ||
1 | ||
} else { | ||
0 | ||
}; | ||
let rx = if self.borders.intersects(Borders::RIGHT) { | ||
|
||
let right_border_dx = if self.borders.intersects(Borders::RIGHT) { | ||
1 | ||
} else { | ||
0 | ||
}; | ||
let width = area.width.saturating_sub(lx).saturating_sub(rx); | ||
buf.set_spans(area.left() + lx, area.top(), &title, width); | ||
|
||
let title_area_width = area | ||
.width | ||
.saturating_sub(left_border_dx) | ||
.saturating_sub(right_border_dx); | ||
|
||
let title_dx = match self.title_alignment { | ||
Alignment::Left => 0, | ||
Alignment::Center => (title_area_width - title.width() as u16) / 2, | ||
Alignment::Right => (title_area_width - title.width() as u16), | ||
}; | ||
|
||
let title_x = area.left() + left_border_dx + title_dx; | ||
let title_y = area.top(); | ||
|
||
buf.set_spans(title_x, title_y, &title, title_area_width); | ||
} | ||
} | ||
} | ||
|
@@ -507,5 +532,39 @@ mod tests { | |
height: 0, | ||
}, | ||
); | ||
assert_eq!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added them to show that |
||
Block::default() | ||
.title("Test") | ||
.title_alignment(Alignment::Center) | ||
.inner(Rect { | ||
x: 0, | ||
y: 0, | ||
width: 0, | ||
height: 1, | ||
}), | ||
Rect { | ||
x: 0, | ||
y: 1, | ||
width: 0, | ||
height: 0, | ||
}, | ||
); | ||
assert_eq!( | ||
Block::default() | ||
.title("Test") | ||
.title_alignment(Alignment::Right) | ||
.inner(Rect { | ||
x: 0, | ||
y: 0, | ||
width: 0, | ||
height: 1, | ||
}), | ||
Rect { | ||
x: 0, | ||
y: 1, | ||
width: 0, | ||
height: 0, | ||
}, | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will probably panic if
title_area_width < title.width()
. You may want to usesaturating_sub
here as well unless the precondition is checked beforehand. This could use a test as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will update to use
saturating_sub
and add a test for this case. Thank you for pointing this out.