Class: Charming::Presentation::UI::Style

Inherits:
Object
  • Object
show all
Defined in:
lib/charming/presentation/ui/style.rb

Overview

Style is an immutable builder for terminal text styling. Every method returns a new Style instance with the requested attribute added, so styles can be safely chained and shared across views. ‘render(value)` applies the accumulated style to a string.

Constant Summary collapse

ATTRIBUTES =
ANSICodes::ATTRIBUTES
COLORS =
ANSICodes::COLORS

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Style

Initializes a new style with an optional options hash. Recognized keys: ‘:attributes` (array of attribute symbols), `:padding` ([top, right, bottom, left]), `:align` (`:left`/`:right`/`:center`), and any of `:foreground`, `:background`, `:border`, `:border_sides`, `:border_foreground`, `:width`, `:height`.



18
19
20
21
22
23
24
# File 'lib/charming/presentation/ui/style.rb', line 18

def initialize(options = {})
  @options = {
    attributes: [],
    padding: [0, 0, 0, 0],
    align: :left
  }.merge(options)
end

Instance Method Details

#align(value) ⇒ Object

Returns a new Style with horizontal alignment set (‘:left`, `:right`, or `:center`).



71
72
73
# File 'lib/charming/presentation/ui/style.rb', line 71

def align(value)
  with(align: value)
end

#background(color) ⇒ Object Also known as: bg

Returns a new Style with the background color set.



34
35
36
# File 'lib/charming/presentation/ui/style.rb', line 34

def background(color)
  with(background: color)
end

#border(style = :normal, sides: nil, foreground: nil) ⇒ Object

Returns a new Style with the border set. style is a border name (e.g., :normal, :rounded). sides optionally restricts the border to specific sides. foreground sets the border color.



56
57
58
# File 'lib/charming/presentation/ui/style.rb', line 56

def border(style = :normal, sides: nil, foreground: nil)
  with(border: style, border_sides: sides, border_foreground: foreground)
end

#foreground(color) ⇒ Object Also known as: fg

Returns a new Style with the foreground color set. color is a color name (“:red”), 256-color index (integer), or hex string (“#rrggbb”).



28
29
30
# File 'lib/charming/presentation/ui/style.rb', line 28

def foreground(color)
  with(foreground: color)
end

#height(value) ⇒ Object

Returns a new Style that fixes the rendered height to value (in rows).



66
67
68
# File 'lib/charming/presentation/ui/style.rb', line 66

def height(value)
  with(height: value)
end

#padding(*values) ⇒ Object

Returns a new Style with the padding set. Accepts 1, 2, or 4 values following CSS-style shorthand: 1 → all sides, 2 → [vertical, horizontal], 4 → [top, right, bottom, left].



49
50
51
# File 'lib/charming/presentation/ui/style.rb', line 49

def padding(*values)
  with(padding: expand_box_values(values))
end

#render(value) ⇒ Object

Applies the configured style to value and returns the styled string. Steps:

  1. wrap to ‘:width`, 2. align horizontally, 3. expand to `:height`, 4. apply padding,

  2. paint border, 6. emit ANSI attribute/foreground/background escapes.



78
79
80
81
82
83
# File 'lib/charming/presentation/ui/style.rb', line 78

def render(value)
  lines = apply_dimensions(value.to_s.lines(chomp: true))
  lines = apply_padding(lines)
  lines = apply_border(lines)
  apply_ansi(lines.join("\n"))
end

#width(value) ⇒ Object

Returns a new Style that fixes the rendered width to value (in display columns).



61
62
63
# File 'lib/charming/presentation/ui/style.rb', line 61

def width(value)
  with(width: value)
end