terminal_tools_plus_plus 0.2.1

A collection of enhanced utilities for terminal manipulation and CLI development in Rust.
Documentation
use std::io::{self, Write};

/// A simple terminal progress bar.
pub struct ProgressBar {
    total: u32,
    width: usize,
}

impl ProgressBar {
    /// Creates a new progress bar with a total value.
    pub fn new(total: u32) -> Self {
        Self { total, width: 20 }
    }

    /// Sets the width of the progress bar.
    pub fn set_width(mut self, width: usize) -> Self {
        self.width = width;
        self
    }

    /// Draws the progress bar based on the current value.
    pub fn draw(&self, current: u32) {
        let total = self.total.max(1);
        let ratio = current.min(total) as f32 / total as f32;

        let filled = (ratio * self.width as f32).round() as usize;
        let empty = self.width - filled;

        print!(
            "\r[{}{}] {:.0}%",
            "".repeat(filled),
            "".repeat(empty),
            ratio * 100.0
        );

        let _ = io::stdout().flush();
    }

    /// Finishes the progress bar and moves to a new line.
    pub fn finish(&self) {
        println!();
    }
}