Class: MarkdownExec::Histogram
Overview
A class that generates a histogram bar in terminal using xterm-256 color codes.
Class Method Summary collapse
-
.display(integer_value, min, max, width, inverse: false) ⇒ Object
Generates and prints a histogram bar for a given value within a specified range and width, with an option for inverse display.
Class Method Details
.display(integer_value, min, max, width, inverse: false) ⇒ Object
Generates and prints a histogram bar for a given value within a specified range and width, with an option for inverse display.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/markdown_exec.rb', line 133 def self.display(integer_value, min, max, width, inverse: false) return if max <= min # Ensure the range is valid # Normalize the value within the range 0 to 1 normalized_value = [0, [(integer_value - min).to_f / (max - min), 1].min].max # Calculate how many characters should be filled filled_length = (normalized_value * width).round # # Generate the histogram bar using xterm-256 colors (color code 42 is green) # filled_bar = "\e[48;5;42m" + ' ' * filled_length + "\e[0m" = ('ยค' * filled_length).fg_rgbh_AF_AF_00 = ' ' * (width - filled_length) # Determine the order of filled and empty parts based on the inverse flag inverse ? ( + ) : ( + ) end |