Skip to content

Commit

Permalink
documentation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Arteiii committed Apr 10, 2024
1 parent 413d0d7 commit 66923ba
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 42 deletions.
26 changes: 24 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,42 @@
use lazy_static::lazy_static;

lazy_static! {
/// supported color pallet (which colors are supported if ENABLE_COLOR)
/// supported color pallet (which colors are supported if ENABLE_COLOR)
///
/// Example:
///
/// ```
/// use zenity::color::{COLOR_PALETTE, ENABLE_COLOR, ColorPalette};
/// assert_eq!(*ENABLE_COLOR, false); // if color is supported (will be false in default tests)
/// assert_eq!(*COLOR_PALETTE, ColorPalette::None); // which colors are supported
/// ```
pub static ref COLOR_PALETTE: ColorPalette = {
CliColorConfig::get_supported_color_palette()
};

/// lazy static ENABLE color bool true if color should be enabled false otherwise
///
/// Example:
///
/// ```
/// use zenity::color::{COLOR_PALETTE, ENABLE_COLOR, ColorPalette};
/// assert_eq!(*ENABLE_COLOR, false); // if color is supported
/// ```
pub static ref ENABLE_COLOR: bool = {
let conf = CliColorConfig::default();
conf.should_enable_color()
};
}


/// represents different color palettes supported by terminals
#[allow(dead_code)]
/// Example:
///
/// ```
/// use zenity::color::{COLOR_PALETTE, ENABLE_COLOR, ColorPalette};
/// # assert_eq!(*ENABLE_COLOR, false);
/// assert_eq!(*COLOR_PALETTE, ColorPalette::None); // None for testing
/// ```
#[derive(PartialEq, Debug)]
pub enum ColorPalette {
/// color support not available (pipe or otherwise disabled)
Expand Down
2 changes: 1 addition & 1 deletion src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
///
/// a vector containing the values from each vector at the specified index position
///
pub fn balanced_iterator<T>(index: usize, vectors: &[Vec<T>]) -> Vec<Option<&T>> {
pub(crate) fn balanced_iterator<T>(index: usize, vectors: &[Vec<T>]) -> Vec<Option<&T>> {
vectors
.iter()
.map(|vec| {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
//! Your feedback is valuable and will help improve the library for everyone.
pub mod color;
mod iterators;
pub(crate) mod iterators;
pub mod progress;
pub mod spinner;
pub mod style;
Expand Down
129 changes: 122 additions & 7 deletions src/progress/frames.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
//! progressbar frames
//! Predefined Progress Frames
use std::sync::{Arc, Mutex};

/// struct storing the data needed to render a ProgressBar
#[derive(Clone)]
/// struct storing the data needed to render a ProgressFrames
///
/// Example
/// ```
/// use std::sync::{Arc, Mutex};
/// use zenity::progress::Frames;
///
/// let spinner_frames = Frames {
/// begin: vec!["["],
/// bar_complete_char: vec!["="],
/// bar_incomplete_char: vec!["-"],
/// end: vec!["]"],
/// size: Arc::new(Mutex::new(30)),
/// goal: Arc::new(Mutex::new(100)),
/// current: Arc::new(Mutex::new(0)),
/// };
/// # assert_eq!(spinner_frames.begin, vec!["["]);
/// # assert_eq!(spinner_frames.bar_complete_char, vec!["="]);
/// # assert_eq!(spinner_frames.bar_incomplete_char, vec!["-"]);
/// # assert_eq!(spinner_frames.end, vec!["]"]);
/// # assert_eq!(*spinner_frames.size.lock().unwrap(), 30);
/// # assert_eq!(*spinner_frames.goal.lock().unwrap(), 100);
/// # assert_eq!(*spinner_frames.current.lock().unwrap(), 0);
/// ```
#[derive(Clone, Debug)]
pub struct Frames {
/// begin string
pub begin: Vec<&'static str>,
Expand All @@ -30,11 +53,40 @@ pub struct Frames {
// TODO: add animations by adding +1 for each bar so you can have a wave animation and others

impl Default for Frames {
/// default implementation for `Frames`
///
/// by default, it returns a `Frames` instance generated by the `equal()` method, which creates
/// a simple frame animation with equal frames
///
/// # Example
///
/// ```
/// use zenity::progress::Frames;
///
/// let frames = Frames::default();
/// assert_eq!(frames, Frames::equal());
/// ```
fn default() -> Self {
Self::equal()
}
}


impl PartialEq for Frames {
fn eq(&self, other: &Self) -> bool {
self.begin == other.begin &&
self.bar_complete_char == other.bar_complete_char &&
self.bar_incomplete_char == other.bar_incomplete_char &&
self.end == other.end &&
*self.size.lock().unwrap() == *other.size.lock().unwrap() &&
*self.goal.lock().unwrap() == *other.goal.lock().unwrap() &&
*self.current.lock().unwrap() == *other.current.lock().unwrap()
}
}

impl Eq for Frames {}


impl Frames {
/// generates frames for
///
Expand Down Expand Up @@ -72,7 +124,6 @@ impl Frames {
current: Arc::new(Mutex::new(0)),
}
}

/// Sets the size of the progress bar.
///
/// # Arguments
Expand All @@ -81,7 +132,16 @@ impl Frames {
///
/// # Returns
///
/// A new Bar object with the modified size.
/// A new Frames object with the modified size.
///
/// # Examples
///
/// ```
/// use zenity::progress::Frames;
///
/// let bar = Frames::default().set_size(20);
/// # assert_eq!(*bar.size.lock().unwrap(), 20);
/// ```
pub fn set_size(&self, size: usize) -> Self {
*self.size.lock().unwrap() = size;

Expand All @@ -96,7 +156,16 @@ impl Frames {
///
/// # Returns
///
/// a new Bar object with the modified goal value
/// a new Frames object with the modified goal value
///
/// # Examples
///
/// ```
/// use zenity::progress::Frames;
///
/// let bar = Frames::default().set_goal(100);
/// # assert_eq!(*bar.goal.lock().unwrap(), 100);
/// ```
pub fn set_goal(&self, goal: usize) -> Self {
let mut current = self.current.lock().unwrap();
let mut goal_ref = self.goal.lock().unwrap();
Expand All @@ -115,6 +184,15 @@ impl Frames {
/// # Returns
///
/// a new object with the modified current value
///
/// # Examples
///
/// ```
/// use zenity::progress::Frames;
///
/// let bar = Frames::default().set_goal(100).inc(&10);
/// # assert_eq!(*bar.current.lock().unwrap(), 10);
/// ```
pub fn inc(&self, num: &usize) -> Self {
let mut current = self.current.lock().unwrap();
let goal = *self.goal.lock().unwrap();
Expand All @@ -125,15 +203,52 @@ impl Frames {
}

/// '=' as the complete char and '-' as the incomplete char
///
/// # Examples
///
/// ```
/// use zenity::progress::Frames;
///
/// let bar = Frames::equal();
/// # assert_eq!(bar.begin, vec!["["]);
/// # assert_eq!(bar.bar_complete_char, vec!["="]);
/// # assert_eq!(bar.bar_incomplete_char, vec!["-"]);
/// # assert_eq!(bar.end, vec!["]"]);
/// ```
pub fn equal() -> Self {
Self::new(vec!["["], vec!["="], vec!["-"], vec!["]"])
}

/// '#' as the complete char and '.' as the incomplete char
///
/// # Examples
///
/// ```
/// use zenity::progress::Frames;
///
/// let bar = Frames::hash();
/// # assert_eq!(bar.begin, vec!["["]);
/// # assert_eq!(bar.bar_complete_char, vec!["#"]);
/// # assert_eq!(bar.bar_incomplete_char, vec!["."]);
/// # assert_eq!(bar.end, vec!["]"]);
/// ```
pub fn hash() -> Self {
Self::new(vec!["["], vec!["#"], vec!["."], vec!["]"])
}
/// '#' as the complete char and '.' as the incomplete char

/// '■' as the complete char and ' ' as the incomplete char
///
/// # Examples
///
/// ```
/// use zenity::progress::Frames;
///
/// let bar = Frames::rect();
/// # assert_eq!(bar.begin, vec![" "]);
/// # assert_eq!(bar.bar_complete_char, vec!["\u{25A0}"]);
/// # assert_eq!(bar.bar_incomplete_char, vec![" "]);
/// # assert_eq!(bar.end, vec![" "]);
/// ```
pub fn rect() -> Self {
Self::new(vec![" "], vec!["\u{25A0}"], vec![" "], vec![" "])
}
Expand Down
Loading

0 comments on commit 66923ba

Please sign in to comment.