Class: Charming::UI::Style
- Inherits:
-
Object
- Object
- Charming::UI::Style
- 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
-
#align(value) ⇒ Object
Returns a new Style with horizontal alignment set (‘:left`, `:right`, or `:center`).
-
#background(color) ⇒ Object
(also: #bg)
Returns a new Style with the background color set.
-
#border(style = :normal, sides: nil, foreground: nil) ⇒ Object
Returns a new Style with the border set.
-
#foreground(color) ⇒ Object
(also: #fg)
Returns a new Style with the foreground color set.
-
#height(value) ⇒ Object
Returns a new Style that fixes the rendered height to value (in rows).
-
#initialize(options = {}) ⇒ Style
constructor
Initializes a new style with an optional options hash.
-
#padding(*values) ⇒ Object
Returns a new Style with the padding set.
-
#render(value) ⇒ Object
Applies the configured style to value and returns the styled string.
-
#width(value) ⇒ Object
Returns a new Style that fixes the rendered width to value (in display columns).
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`.
17 18 19 20 21 22 23 |
# File 'lib/charming/presentation/ui/style.rb', line 17 def initialize( = {}) @options = { attributes: [], padding: [0, 0, 0, 0], align: :left }.merge() end |
Instance Method Details
#align(value) ⇒ Object
Returns a new Style with horizontal alignment set (‘:left`, `:right`, or `:center`).
70 71 72 |
# File 'lib/charming/presentation/ui/style.rb', line 70 def align(value) with(align: value) end |
#background(color) ⇒ Object Also known as: bg
Returns a new Style with the background color set.
33 34 35 |
# File 'lib/charming/presentation/ui/style.rb', line 33 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.
55 56 57 |
# File 'lib/charming/presentation/ui/style.rb', line 55 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”).
27 28 29 |
# File 'lib/charming/presentation/ui/style.rb', line 27 def foreground(color) with(foreground: color) end |
#height(value) ⇒ Object
Returns a new Style that fixes the rendered height to value (in rows).
65 66 67 |
# File 'lib/charming/presentation/ui/style.rb', line 65 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].
48 49 50 |
# File 'lib/charming/presentation/ui/style.rb', line 48 def padding(*values) with(padding: (values)) end |
#render(value) ⇒ Object
Applies the configured style to value and returns the styled string. Steps:
-
wrap to ‘:width`, 2. align horizontally, 3. expand to `:height`, 4. apply padding,
-
paint border, 6. emit ANSI attribute/foreground/background escapes.
77 78 79 80 81 82 |
# File 'lib/charming/presentation/ui/style.rb', line 77 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).
60 61 62 |
# File 'lib/charming/presentation/ui/style.rb', line 60 def width(value) with(width: value) end |