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. displayed in inverse order (right to left)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/markdown_exec.rb', line 145 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" = AnsiString.new('¤' * 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 |