docs(Sparkline): add documentation (#648)

This commit is contained in:
Valentin271 2023-11-27 00:46:20 +01:00 committed by GitHub
parent c862aa5e9e
commit fe632d70cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,4 @@
#![warn(missing_docs)]
use std::cmp::min; use std::cmp::min;
use strum::{Display, EnumString}; use strum::{Display, EnumString};
@ -12,6 +13,18 @@ use crate::{
/// Widget to render a sparkline over one or more lines. /// Widget to render a sparkline over one or more lines.
/// ///
/// You can create a `Sparkline` using [`Sparkline::default`].
///
/// `Sparkline` can be styled either using [`Sparkline::style`] or preferably using the methods
/// provided by the [`Stylize`](crate::style::Stylize) trait.
///
/// # Builder methods
///
/// - [`Sparkline::block`] wraps the sparkline in a [`Block`]
/// - [`Sparkline::data`] defines the dataset, you'll almost always want to use it
/// - [`Sparkline::max`] sets the maximum value of bars
/// - [`Sparkline::direction`] sets the render direction
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@ -21,7 +34,8 @@ use crate::{
/// .block(Block::default().title("Sparkline").borders(Borders::ALL)) /// .block(Block::default().title("Sparkline").borders(Borders::ALL))
/// .data(&[0, 2, 3, 4, 1, 4, 10]) /// .data(&[0, 2, 3, 4, 1, 4, 10])
/// .max(5) /// .max(5)
/// .style(Style::default().fg(Color::Red).bg(Color::White)); /// .direction(RenderDirection::RightToLeft)
/// .style(Style::default().red().on_white());
/// ``` /// ```
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct Sparkline<'a> { pub struct Sparkline<'a> {
@ -40,10 +54,15 @@ pub struct Sparkline<'a> {
direction: RenderDirection, direction: RenderDirection,
} }
/// Defines the direction in which sparkline will be rendered.
///
/// See [`Sparkline::direction`].
#[derive(Debug, Default, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)] #[derive(Debug, Default, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)]
pub enum RenderDirection { pub enum RenderDirection {
/// The first value is on the left, going to the right
#[default] #[default]
LeftToRight, LeftToRight,
/// The first value is on the right, going to the left
RightToLeft, RightToLeft,
} }
@ -61,31 +80,58 @@ impl<'a> Default for Sparkline<'a> {
} }
impl<'a> Sparkline<'a> { impl<'a> Sparkline<'a> {
/// Wraps the sparkline with the given `block`.
pub fn block(mut self, block: Block<'a>) -> Sparkline<'a> { pub fn block(mut self, block: Block<'a>) -> Sparkline<'a> {
self.block = Some(block); self.block = Some(block);
self self
} }
/// Sets the style of the entire widget.
///
/// The foreground corresponds to the bars while the background is everything else.
pub fn style(mut self, style: Style) -> Sparkline<'a> { pub fn style(mut self, style: Style) -> Sparkline<'a> {
self.style = style; self.style = style;
self self
} }
/// Sets the dataset for the sparkline.
///
/// # Example
///
/// ```
/// # use ratatui::{prelude::*, widgets::*};
/// # fn ui(frame: &mut Frame) {
/// # let area = Rect::default();
/// let sparkline = Sparkline::default().data(&[1, 2, 3]);
/// frame.render_widget(sparkline, area);
/// # }
/// ```
pub fn data(mut self, data: &'a [u64]) -> Sparkline<'a> { pub fn data(mut self, data: &'a [u64]) -> Sparkline<'a> {
self.data = data; self.data = data;
self self
} }
/// Sets the maximum value of bars.
///
/// Every bar will be scaled accordingly. If no max is given, this will be the max in the
/// dataset.
pub fn max(mut self, max: u64) -> Sparkline<'a> { pub fn max(mut self, max: u64) -> Sparkline<'a> {
self.max = Some(max); self.max = Some(max);
self self
} }
/// Sets the characters used to display the bars.
///
/// Can be [`symbols::bar::THREE_LEVELS`], [`symbols::bar::NINE_LEVELS`] (default) or a custom
/// [`Set`](symbols::bar::Set).
pub fn bar_set(mut self, bar_set: symbols::bar::Set) -> Sparkline<'a> { pub fn bar_set(mut self, bar_set: symbols::bar::Set) -> Sparkline<'a> {
self.bar_set = bar_set; self.bar_set = bar_set;
self self
} }
/// Sets the direction of the sparkline.
///
/// [`RenderDirection::LeftToRight`] by default.
pub fn direction(mut self, direction: RenderDirection) -> Sparkline<'a> { pub fn direction(mut self, direction: RenderDirection) -> Sparkline<'a> {
self.direction = direction; self.direction = direction;
self self