Class: RubyTUI::Widgets::Block

Inherits:
Object
  • Object
show all
Includes:
RubyTUI::Widget
Defined in:
lib/rubytui/widgets/block.rb

Overview

Block is the most fundamental widget: a rectangular area with optional borders, title, and padding. Other widgets are typically rendered inside a Block's inner area.

Examples:

Bordered block with a centered title

block = RubyTUI::Widgets::Block.new(title: " Status ", title_alignment: :center)
frame.render_widget(block, area)
content_area = block.inner(area)

Constant Summary collapse

NONE =

No borders.

:none
ALL =

All four borders.

:all
TOP =

Top border.

:top
BOTTOM =

Bottom border.

:bottom
LEFT =

Left border.

:left
RIGHT =

Right border.

:right

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title: nil, title_alignment: :left, title_style: nil, borders: ALL, border_set: Symbols::ROUNDED, border_style: Style::DEFAULT, style: Style::DEFAULT, padding: nil) ⇒ Block

Returns a new instance of Block.

Parameters:

  • title (String, nil) (defaults to: nil) —

    title drawn on the top border, truncated to fit between the corners; not drawn when the top border is disabled

  • title_alignment (Symbol) (defaults to: :left) —

    :left (default, one cell in from the left corner), :center, or :right

  • title_style (Style, nil) (defaults to: nil) —

    title style (default: nil, uses border_style)

  • borders (Symbol, Array<Symbol>, Boolean, nil) (defaults to: ALL) —

    sides to draw: :all or true (default: ALL), :none, false or nil for none, a single side symbol (+:top+, :bottom, :left, :right), or an Array of side symbols (the Array is frozen in place); any other value draws all sides

  • border_set (Symbols::BorderSet) (defaults to: Symbols::ROUNDED) —

    border characters (default: Symbols::ROUNDED)

  • border_style (Style) (defaults to: Style::DEFAULT) —

    style of the border characters (default: Style::DEFAULT)

  • style (Style) (defaults to: Style::DEFAULT) —

    style patched onto every cell of the area when it is not Style::DEFAULT (default: Style::DEFAULT)

  • padding (Integer, Array<Integer>, nil) (defaults to: nil) —

    space inside the borders: an Integer for all sides, or an Array of 1 (all sides), 2 ([vertical, horizontal]) or 4 ([top, right, bottom, left]) values; nil or any other shape means no padding (default: nil)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubytui/widgets/block.rb', line 58

def initialize(
  title: nil,
  title_alignment: :left,
  title_style: nil,
  borders: ALL,
  border_set: Symbols::ROUNDED,
  border_style: Style::DEFAULT,
  style: Style::DEFAULT,
  padding: nil  # [top, right, bottom, left] or single integer
)
  @title = title
  @title_alignment = title_alignment
  @title_style = title_style || border_style
  @borders = normalize_borders(borders)
  @border_set = border_set
  @border_style = border_style
  @style = style
  @padding = normalize_padding(padding)
end

Instance Attribute Details

#border_set ⇒ Object (readonly)

Returns the value of attribute border_set.



37
38
39
# File 'lib/rubytui/widgets/block.rb', line 37

def border_set
  @border_set
end

#border_style ⇒ Object (readonly)

Returns the value of attribute border_style.



37
38
39
# File 'lib/rubytui/widgets/block.rb', line 37

def border_style
  @border_style
end

#borders ⇒ Object (readonly)

Returns the value of attribute borders.



37
38
39
# File 'lib/rubytui/widgets/block.rb', line 37

def borders
  @borders
end

#padding ⇒ Object (readonly)

Returns the value of attribute padding.



37
38
39
# File 'lib/rubytui/widgets/block.rb', line 37

def padding
  @padding
end

#style ⇒ Object (readonly)

Returns the value of attribute style.



37
38
39
# File 'lib/rubytui/widgets/block.rb', line 37

def style
  @style
end

#title ⇒ Object (readonly)

Returns the value of attribute title.



37
38
39
# File 'lib/rubytui/widgets/block.rb', line 37

def title
  @title
end

#title_alignment ⇒ Object (readonly)

Returns the value of attribute title_alignment.



37
38
39
# File 'lib/rubytui/widgets/block.rb', line 37

def title_alignment
  @title_alignment
end

Instance Method Details

#inner(area) ⇒ Rect

Calculate the inner area (area minus borders and padding)

Parameters:

  • area (Rect) —

    the outer area of the block

Returns:

  • (Rect) —

    the area inside the enabled borders and padding (width and height clamped to >= 0), or Rect::ZERO if area is empty



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rubytui/widgets/block.rb', line 83

def inner(area)
  return Rect::ZERO if area.empty?

  x = area.x
  y = area.y
  w = area.width
  h = area.height

  if @borders.include?(:left)
    x += 1
    w -= 1
  end
  if @borders.include?(:right)
    w -= 1
  end
  if @borders.include?(:top)
    y += 1
    h -= 1
  end
  if @borders.include?(:bottom)
    h -= 1
  end

  # Apply padding
  x += @padding[3]  # left
  y += @padding[0]  # top
  w -= @padding[1] + @padding[3]  # right + left
  h -= @padding[0] + @padding[2]  # top + bottom

  Rect.new(x: x, y: y, width: [w, 0].max, height: [h, 0].max)
end

#render(area, buf) ⇒ void

This method returns an undefined value.

Render the block's background, borders and title.

The background style is patched onto every cell of area unless it is Style::DEFAULT. Child content is not rendered; draw it into #inner.

Parameters:

  • area (Rect) —

    the rectangular area to render into

  • buf (Buffer) —

    the buffer to write cells into



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rubytui/widgets/block.rb', line 123

def render(area, buf)
  return if area.empty?

  # Fill background
  buf.set_style(area, @style) if @style != Style::DEFAULT

  # Draw borders
  render_borders(area, buf)

  # Draw title
  render_title(area, buf) if @title
end