Class: Charming::Components::Sparkline

Inherits:
Charming::Component show all
Defined in:
lib/charming/presentation/components/sparkline.rb

Overview

Sparkline renders a series of numbers as a compact one-line bar graph using the eighth-block glyphs ▁▂▃▄▅▆▇█ — one cell per value, scaled between the series' min and max. Pure text, so it works on every terminal. Pass an optional style: (UI::Style) to colour it.

Constant Summary collapse

BARS =

The eight bar heights, shortest to tallest.

%w[       ].freeze

Instance Method Summary collapse

Methods inherited from Charming::Component

#captures_text?

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(values:, style: nil, theme: nil) ⇒ Sparkline

values is the numeric series. style optionally paints the result. theme is forwarded.



13
14
15
16
17
# File 'lib/charming/presentation/components/sparkline.rb', line 13

def initialize(values:, style: nil, theme: nil)
  super(theme: theme)
  @values = values
  @style = style
end

Instance Method Details

#renderObject

Renders one bar glyph per value (empty string for an empty series).



20
21
22
23
24
25
# File 'lib/charming/presentation/components/sparkline.rb', line 20

def render
  return "" if @values.empty?

  glyphs = @values.map { |value| BARS[level(value)] }.join
  @style ? @style.render(glyphs) : glyphs
end