ruby-progressbar

repository·master·Indexed 23 days ago

https://github.com/jfelchner/ruby-progressbar

A text-based progress bar library for Ruby used to visualize long-running tasks in the terminal. It provides tools for managing progress state, customizing visual appearance with configurable marks and formats, and calculating metrics such as rate of change and estimated time remaining (ETA). The library supports TTY and non-TTY output detection and allows for custom output streams.

Tokens
3.1K
Snippets
2
Records
21
Agent score
81%

What's inside ruby-progressbar

  1. How ProgressBar detects the output type

    master

    The ProgressBar::Output.detect method automatically determines the most appropriate output handler based on the provided options and the environment:

    1. Custom Class: If :output is a class that inherits from ProgressBar::Output, it instantiates that class.
    2. TTY (Interactive Terminal): If the output stream (or $stdout by default) is a TTY, it uses Outputs::Tty to enable interactive features like clearing lines.
    3. Non-TTY: If the stream is not a TTY (e.g., a redirected file or a pipe), it uses Outputs::NonTty to ensure progress doesn't clutter logs with escape sequences or attempt interactive line clearing.
  2. Configure progress bar smoothing (Deprecation Warning)

    master

    The smoothing and running_average_rate options are deprecated and will be removed in version 2.0. To ensure future compatibility, migrate these options into a projector configuration block.

    Deprecated way:

    ProgressBar.new(smoothing: 0.5)

    New way:

    ProgressBar.new(projector: { type: 'smoothing', strength: 0.5 })
  3. Handle InvalidProgressError

    master

    The ProgressBar::InvalidProgressError is raised when you attempt to set the progress state to an impossible value. This includes:

    • Setting progress to a value greater than total.
    • Setting total to a value less than the current progress.
    • (In v2.0.0+) Attempting to increment when progress is already at total or decrement when progress is at 0.
  4. Calculate completion percentage

    master

    The ProgressBar::Progress object provides methods to calculate how much of the task is complete:

    • percentage_completed: Returns an integer representing the percentage (e.g., 50). It uses integer math to avoid float conversion issues.
    • percentage_completed_with_precision: Returns a formatted string with two decimal places (e.g., " 50.00").
    • none?: Returns true if the current progress is zero.
    • finished?: Returns true if the current progress equals the total.
  5. Render the progress bar with different formats

    master

    The to_s method on a ProgressBar::Components::Bar instance allows you to render the bar using different visual formats.

    Supported :format options:

    • :standard: Returns a string consisting of the completed progress marks followed by the remainder marks (e.g., ==== ).
    • :integrated_percentage: Returns a string where the percentage is centered within the completed progress section (e.g., == 50% == ).

    Note: If the progress is 'unknown', the bar will automatically use the 'Unknown Progress Animation' (UPA) regardless of the format requested.

  6. Use the Rate component to calculate progress speed

    master

    The ProgressBar::Components::Rate component calculates the rate of progress (e.g., items per second). It requires a timer object (to track elapsed time) and a progress object (to track the absolute progress value). You can provide a rate_scale lambda to transform the raw rate (e.g., for scaling units).

    To use it, initialize the component with the necessary objects and call rate_of_change or rate_of_change_with_precision.

  7. Manage progress state with ProgressBar::Progress

    master

    The ProgressBar::Progress class handles the internal state of a progress bar, including the current progress, the total value, and the starting position. You can use it to manually control how much work has been completed.

    Key behaviors:

    • Incrementing/Decrementing: Use increment and decrement to move the progress by 1 unit. Note that incrementing beyond the total or decrementing below 0 will trigger a warning (and will raise a ProgressBar::InvalidProgressError in v2.0.0).
    • Setting Progress: You can set the progress directly using progress=, but it cannot exceed the total.
    • Setting Total: You can set the total using total=, but it cannot be less than the current progress.
    • Completion: Calling finish sets the progress to the total value (unless the progress is currently unknown).
    • Resetting: Calling reset returns the progress to the starting_position.
  8. Use SmoothedAverage to smooth progress values

    master

    The ProgressBar::Projectors::SmoothedAverage class is used to calculate a smoothed average of progress values, which helps prevent jittery progress bar updates when progress increments are irregular. It uses an exponential moving average formula to blend the current absolute progress with the previous projection based on a strength factor.

    Configuration

    • strength: A float representing the smoothing rate. A higher value gives more weight to the previous projection (more smoothing), while a lower value reacts faster to new progress values. The DEFAULT_STRENGTH is 0.1.
    • samples: An internal array used to track progress state.

    Lifecycle

    • start(options = {}): Initializes the projection. You can pass :at to specify the starting position (defaults to 0).
    • progress=(new_progress): Updates the current progress and recalculates the smoothed projection.
    • reset: Resets the projection to the initial starting position.
  9. Use specialized bar rendering methods

    master

    The ProgressBar::Components::Bar class provides several convenience methods for rendering specific bar states:

    • bar(length): Sets the bar length and returns the standard complete string.
    • complete_bar(length): Sets the bar length and returns the bar in :standard format.
    • complete_bar_with_percentage(length): Sets the bar length and returns the bar in :integrated_percentage format.
    • incomplete_space(length): Sets the bar length and returns the incomplete portion of the bar (or the unknown animation if progress is unknown).
    • bar_with_percentage(length): Sets the bar length and returns the integrated_percentage_complete_string.