standardized block symbols, added no unicode option, added tests

pull/377/head
DashEightMate 4 years ago
parent a8c5332e1a
commit 8052bfc0db

@ -106,7 +106,8 @@ fn main() -> Result<(), Box<dyn Error>> {
.add_modifier(Modifier::ITALIC),
)
.percent(app.progress4)
.label(label);
.label(label)
.use_unicode(false);
f.render_widget(gauge, chunks[3]);
})?;

@ -4,6 +4,7 @@ use crate::{
style::{Color, Style},
text::Span,
widgets::{Block, Widget},
symbols::block,
};
/// A widget to display a task progress.
@ -23,6 +24,7 @@ pub struct Gauge<'a> {
block: Option<Block<'a>>,
ratio: f64,
label: Option<Span<'a>>,
use_block: bool,
style: Style,
gauge_style: Style,
}
@ -33,6 +35,7 @@ impl<'a> Default for Gauge<'a> {
block: None,
ratio: 0.0,
label: None,
use_block: true,
style: Style::default(),
gauge_style: Style::default(),
}
@ -81,6 +84,11 @@ impl<'a> Gauge<'a> {
self.gauge_style = style;
self
}
pub fn use_unicode(mut self, unicode: bool) -> Gauge<'a> {
self.use_block = unicode;
self
}
}
impl<'a> Widget for Gauge<'a> {
@ -101,7 +109,8 @@ impl<'a> Widget for Gauge<'a> {
let center = gauge_area.height / 2 + gauge_area.top();
let width = f64::from(gauge_area.width) * self.ratio;
let end = gauge_area.left() + (width.floor() as u16);
//go to regular rounding behavior if we're not using unicode blocks
let end = gauge_area.left() + if self.use_block {width.floor() as u16} else {width.round() as u16};
// Label
let ratio = self.ratio;
let label = self
@ -114,18 +123,23 @@ impl<'a> Widget for Gauge<'a> {
}
//set unicode block
if self.ratio < 1.0{
if self.use_block && self.ratio < 1.0{
buf.get_mut(end, y).set_symbol(get_unicode_block(width%1.0));
}
let mut color_end = end;
if y == center {
let label_width = label.width() as u16;
let middle = (gauge_area.width - label_width) / 2 + gauge_area.left();
buf.set_span(middle, y, &label, gauge_area.right() - middle);
if self.use_block && end >= middle && end < middle+label_width{
color_end = gauge_area.left() + (width.round() as u16); //set color on the label to the rounded gauge level
}
}
// Fix colors
for x in gauge_area.left()..end {
for x in gauge_area.left()..color_end {
buf.get_mut(x, y)
.set_fg(self.gauge_style.bg.unwrap_or(Color::Reset))
.set_bg(self.gauge_style.fg.unwrap_or(Color::Reset));
@ -136,13 +150,13 @@ impl<'a> Widget for Gauge<'a> {
fn get_unicode_block<'a>(frac: f64) -> &'a str {
match (frac*8.0).round() as u16{ //get how many eighths the fraction is closest to
1 => "▏",
2 => "▎",
3 => "▍",
4 => "▌",
5 => "▋",
6 => "▊",
7 => "▉",
1 => block::ONE_EIGHTH,
2 => block::ONE_QUARTER,
3 => block::THREE_EIGHTHS,
4 => block::HALF,
5 => block::FIVE_EIGHTHS,
6 => block::THREE_QUARTERS,
7 => block::SEVEN_EIGHTHS,
8 => "",
_ => " "
}

@ -28,6 +28,44 @@ fn widgets_gauge_renders() {
f.render_widget(gauge, chunks[1]);
})
.unwrap();
let expected = Buffer::with_lines(vec![
" ",
" ",
" ┌Percentage────────────────────────┐ ",
" │ ▋43% │ ",
" └──────────────────────────────────┘ ",
" ┌Ratio─────────────────────────────┐ ",
" │ ▏ 21% │ ",
" └──────────────────────────────────┘ ",
" ",
" ",
]);
terminal.backend().assert_buffer(&expected);
}
#[test]
fn widgets_gauge_renders_no_unicode(){
let backend = TestBackend::new(40, 10);
let mut terminal = Terminal::new(backend).unwrap();
terminal.draw(|f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(2)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(f.size());
let gauge = Gauge::default()
.block(Block::default().title("Percentage").borders(Borders::ALL))
.percent(43)
.use_unicode(false);
f.render_widget(gauge, chunks[0]);
let gauge = Gauge::default()
.block(Block::default().title("Ratio").borders(Borders::ALL))
.ratio(0.211_313_934_313_1)
.use_unicode(false);
f.render_widget(gauge, chunks[1]);
}).unwrap();
let expected = Buffer::with_lines(vec![
" ",
" ",

Loading…
Cancel
Save