@ -24,6 +24,7 @@ pub struct Gauge<'a> {
block : Option < Block < ' a > > ,
block : Option < Block < ' a > > ,
ratio : f64 ,
ratio : f64 ,
label : Option < Span < ' a > > ,
label : Option < Span < ' a > > ,
use_unicode : bool ,
style : Style ,
style : Style ,
gauge_style : Style ,
gauge_style : Style ,
}
}
@ -34,6 +35,7 @@ impl<'a> Default for Gauge<'a> {
block : None ,
block : None ,
ratio : 0.0 ,
ratio : 0.0 ,
label : None ,
label : None ,
use_unicode : true ,
style : Style ::default ( ) ,
style : Style ::default ( ) ,
gauge_style : Style ::default ( ) ,
gauge_style : Style ::default ( ) ,
}
}
@ -82,6 +84,11 @@ impl<'a> Gauge<'a> {
self . gauge_style = style ;
self . gauge_style = style ;
self
self
}
}
pub fn use_unicode ( mut self , unicode : bool ) -> Gauge < ' a > {
self . use_unicode = unicode ;
self
}
}
}
impl < ' a > Widget for Gauge < ' a > {
impl < ' a > Widget for Gauge < ' a > {
@ -101,8 +108,14 @@ impl<'a> Widget for Gauge<'a> {
}
}
let center = gauge_area . height / 2 + gauge_area . top ( ) ;
let center = gauge_area . height / 2 + gauge_area . top ( ) ;
let width = ( f64 ::from ( gauge_area . width ) * self . ratio ) . round ( ) as u16 ;
let width = f64 ::from ( gauge_area . width ) * self . ratio ;
let end = gauge_area . left ( ) + width ;
//go to regular rounding behavior if we're not using unicode blocks
let end = gauge_area . left ( )
+ if self . use_unicode {
width . floor ( ) as u16
} else {
width . round ( ) as u16
} ;
// Label
// Label
let ratio = self . ratio ;
let ratio = self . ratio ;
let label = self
let label = self
@ -114,14 +127,25 @@ impl<'a> Widget for Gauge<'a> {
buf . get_mut ( x , y ) . set_symbol ( " " ) ;
buf . get_mut ( x , y ) . set_symbol ( " " ) ;
}
}
//set unicode block
if self . use_unicode & & 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 {
if y = = center {
let label_width = label . width ( ) as u16 ;
let label_width = label . width ( ) as u16 ;
let middle = ( gauge_area . width - label_width ) / 2 + gauge_area . left ( ) ;
let middle = ( gauge_area . width - label_width ) / 2 + gauge_area . left ( ) ;
buf . set_span ( middle , y , & label , gauge_area . right ( ) - middle ) ;
buf . set_span ( middle , y , & label , gauge_area . right ( ) - middle ) ;
if self . use_unicode & & 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
// Fix colors
for x in gauge_area . left ( ) .. end {
for x in gauge_area . left ( ) .. color_ end {
buf . get_mut ( x , y )
buf . get_mut ( x , y )
. set_fg ( self . gauge_style . bg . unwrap_or ( Color ::Reset ) )
. set_fg ( self . gauge_style . bg . unwrap_or ( Color ::Reset ) )
. set_bg ( self . gauge_style . fg . unwrap_or ( Color ::Reset ) ) ;
. set_bg ( self . gauge_style . fg . unwrap_or ( Color ::Reset ) ) ;
@ -130,6 +154,20 @@ 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 = > symbols ::block ::ONE_EIGHTH ,
2 = > symbols ::block ::ONE_QUARTER ,
3 = > symbols ::block ::THREE_EIGHTHS ,
4 = > symbols ::block ::HALF ,
5 = > symbols ::block ::FIVE_EIGHTHS ,
6 = > symbols ::block ::THREE_QUARTERS ,
7 = > symbols ::block ::SEVEN_EIGHTHS ,
8 = > symbols ::block ::FULL ,
_ = > " " ,
}
}
/// A compact widget to display a task progress over a single line.
/// A compact widget to display a task progress over a single line.
///
///
/// # Examples:
/// # Examples: