Class: Charming::UI::Style

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

Constant Summary collapse

ATTRIBUTES =
{
  bold: 1,
  faint: 2,
  italic: 3,
  underline: 4,
  reverse: 7,
  strikethrough: 9
}.freeze
COLORS =
{
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
  bright_black: 90,
  bright_red: 91,
  bright_green: 92,
  bright_yellow: 93,
  bright_blue: 94,
  bright_magenta: 95,
  bright_cyan: 96,
  bright_white: 97
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Style

Returns a new instance of Style.



34
35
36
37
38
39
40
# File 'lib/charming/ui/style.rb', line 34

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

Instance Method Details

#align(value) ⇒ Object



74
75
76
# File 'lib/charming/ui/style.rb', line 74

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

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



47
48
49
# File 'lib/charming/ui/style.rb', line 47

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

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



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

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



42
43
44
# File 'lib/charming/ui/style.rb', line 42

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

#height(value) ⇒ Object



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

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

#padding(*values) ⇒ Object



58
59
60
# File 'lib/charming/ui/style.rb', line 58

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

#render(value) ⇒ Object



78
79
80
81
82
83
# File 'lib/charming/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



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

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