Skip to content

Commit

Permalink
docs(Gauge): add documentation to Gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin271 committed Sep 18, 2023
1 parent 0c7d547 commit ade6470
Showing 1 changed file with 70 additions and 5 deletions.
75 changes: 70 additions & 5 deletions src/widgets/gauge.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![deny(missing_docs)]
use crate::{
buffer::Buffer,
layout::Rect,
Expand All @@ -9,14 +10,19 @@ use crate::{

/// A widget to display a task progress.
///
/// # Examples:
/// # Example
///
/// ```
/// use ratatui::{prelude::*, widgets::*};
///
/// Gauge::default()
/// .block(Block::default().borders(Borders::ALL).title("Progress"))
/// .gauge_style(Style::default().fg(Color::White).bg(Color::Black).add_modifier(Modifier::ITALIC))
/// .gauge_style(
/// Style::default()
/// .fg(Color::White)
/// .bg(Color::Black)
/// .add_modifier(Modifier::ITALIC),
/// )
/// .percent(20);
/// ```
#[derive(Debug, Clone, PartialEq)]
Expand All @@ -43,11 +49,21 @@ impl<'a> Default for Gauge<'a> {
}

impl<'a> Gauge<'a> {
/// Surrounds the `Gauge` with a [`Block`].
pub fn block(mut self, block: Block<'a>) -> Gauge<'a> {
self.block = Some(block);
self
}

/// Sets the bar progression from a percentage.
///
/// # Panics
///
/// This method panics if `percent` is **not** between 0 and 100 inclusively.
///
/// # See also
///
/// See [`Gauge::ratio`] to set from a float.
pub fn percent(mut self, percent: u16) -> Gauge<'a> {
assert!(
percent <= 100,
Expand All @@ -57,7 +73,18 @@ impl<'a> Gauge<'a> {
self
}

/// Sets ratio ([0.0, 1.0]) directly.
/// Sets the bar progression from a ratio (float).
///
/// `ratio` is the ratio between filled bar over empty bar (i.e. `3/4` completion is `0.75`).
/// This is more easily seen as a floating point percentage (e.g. 42% = `0.42`).
///
/// # Panics
///
/// This method panics if `ratio` is **not** between 0 and 1 inclusively.
///
/// # See also
///
/// See [`Gauge::percent`] to set from a percentage.
pub fn ratio(mut self, ratio: f64) -> Gauge<'a> {
assert!(
(0.0..=1.0).contains(&ratio),
Expand All @@ -67,6 +94,7 @@ impl<'a> Gauge<'a> {
self
}

/// Sets the label to display in the center of the bar.
pub fn label<T>(mut self, label: T) -> Gauge<'a>
where
T: Into<Span<'a>>,
Expand All @@ -75,16 +103,24 @@ impl<'a> Gauge<'a> {
self
}

/// Sets the widget style.
///
/// This will style everything except the bar itself, so basically the block (if any) and
/// background.
pub fn style(mut self, style: Style) -> Gauge<'a> {
self.style = style;
self
}

/// Sets the style of the bar.
pub fn gauge_style(mut self, style: Style) -> Gauge<'a> {
self.gauge_style = style;
self
}

/// Sets whether to use unicode characters to display the progress bar.
///
/// This enables the use of [unicode block characters](https://en.wikipedia.org/wiki/Block_Elements).
pub fn use_unicode(mut self, unicode: bool) -> Gauge<'a> {
self.use_unicode = unicode;
self
Expand Down Expand Up @@ -165,7 +201,7 @@ fn get_unicode_block<'a>(frac: f64) -> &'a str {
}
}

/// A compact widget to display a task progress over a single line.
/// A compact widget to display a task progress over a single thin line.
///
/// # Examples:
///
Expand All @@ -174,7 +210,12 @@ fn get_unicode_block<'a>(frac: f64) -> &'a str {
///
/// LineGauge::default()
/// .block(Block::default().borders(Borders::ALL).title("Progress"))
/// .gauge_style(Style::default().fg(Color::White).bg(Color::Black).add_modifier(Modifier::BOLD))
/// .gauge_style(
/// Style::default()
/// .fg(Color::White)
/// .bg(Color::Black)
/// .add_modifier(Modifier::BOLD),
/// )
/// .line_set(symbols::line::THICK)
/// .ratio(0.4);
/// ```
Expand All @@ -189,11 +230,20 @@ pub struct LineGauge<'a> {
}

impl<'a> LineGauge<'a> {
/// Surrounds the `LineGauge` with a [`Block`].
pub fn block(mut self, block: Block<'a>) -> Self {
self.block = Some(block);
self
}

/// Sets the bar progression from a ratio (float).
///
/// `ratio` is the ratio between filled bar over empty bar (i.e. `3/4` completion is `0.75`).
/// This is more easily seen as a floating point percentage (e.g. 42% = `0.42`).
///
/// # Panics
///
/// This method panics if `ratio` is **not** between 0 and 1 inclusively.
pub fn ratio(mut self, ratio: f64) -> Self {
assert!(
(0.0..=1.0).contains(&ratio),
Expand All @@ -203,11 +253,21 @@ impl<'a> LineGauge<'a> {
self
}

/// Sets the characters to use for the line.
///
/// # See also
///
/// See [`symbols::line::Set`] for more information. Predefined sets are also available, see
/// [`NORMAL`](symbols::line::NORMAL), [`DOUBLE`](symbols::line::DOUBLE) and
/// [`THICK`](symbols::line::THICK).
pub fn line_set(mut self, set: symbols::line::Set) -> Self {
self.line_set = set;
self
}

/// Sets the label to display.
///
/// With `LineGauge`, labels are only on the left, see [`Gauge`] for a centered label.
pub fn label<T>(mut self, label: T) -> Self
where
T: Into<Line<'a>>,
Expand All @@ -216,11 +276,16 @@ impl<'a> LineGauge<'a> {
self
}

/// Sets the widget style.
///
/// This will style everything except the bar itself, so basically the block (if any) and
/// background.
pub fn style(mut self, style: Style) -> Self {
self.style = style;
self
}

/// Sets the style of the bar.
pub fn gauge_style(mut self, style: Style) -> Self {
self.gauge_style = style;
self
Expand Down

0 comments on commit ade6470

Please sign in to comment.