|
|
|
@ -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 => "",
|
|
|
|
|
_ => " "
|
|
|
|
|
}
|
|
|
|
|