class RatatuiRuby::Layout::Constraint
Defines the sizing rule for a layout section.
Flexible layouts need rules. You can’t just place widgets at absolute coordinates; they must adapt to changing terminal sizes.
This class defines the rules of engagement. It tells the layout engine exactly how much space a section requires relative to others.
Mix and match fixed lengths, percentages, ratios, and minimums. Build layouts that breathe.
Examples
Layout::Constraint.length(5) # Exactly 5 cells Layout::Constraint.percentage(50) # Half the available space Layout::Constraint.min(10) # At least 10 cells, maybe more Layout::Constraint.fill(1) # Fill remaining space (weight 1)
Attributes
The type of constraint.
:length, :percentage, :min, :max, :fill, or :ratio.
The numeric value (or array for ratio) associated with the rule.
Public Class Methods
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 130 def self.fill(v = 1) new(type: :fill, value: Integer(v)) end
Fills remaining space proportionally.
Layout::Constraint.fill(1) # Equal share Layout::Constraint.fill(2) # Double share
Fill constraints distribute any space left after satisfying strict rules. They behave like flex-grow. A fill(2) takes twice as much space as a fill(1).
- v
-
Proportional weight (Integer, default: 1).
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 261 def self.from_fills(values) values.map { |v| fill(v) } end
Converts an array of weights into an array of Fill constraints.
Fill constraints distribute remaining space by weight. Batch them here.
Example
Constraint.from_fills([1, 2, 1]) # => [Constraint.fill(1), Constraint.fill(2), Constraint.fill(1)]
- values
-
Enumerable of Integers.
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 173 def self.from_lengths(values) values.map { |v| length(v) } end
Converts an array of lengths into an array of Length constraints.
Complex layouts often use multiple fixed-size sections. Manually creating each constraint clutters the code.
This method maps over the input, returning a constraint array in one call.
Example
Constraint.from_lengths([10, 20, 10]) # => [Constraint.length(10), Constraint.length(20), Constraint.length(10)]
- values
-
Enumerable of Integers.
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 239 def self.from_maxes(values) values.map { |v| max(v) } end
Converts an array of maximums into an array of Max constraints.
Maximum constraints cap section sizes. Batch them here.
Example
Constraint.from_maxes([20, 30, 40]) # => [Constraint.max(20), Constraint.max(30), Constraint.max(40)]
- values
-
Enumerable of Integers.
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 217 def self.from_mins(values) values.map { |v| min(v) } end
Converts an array of minimums into an array of Min constraints.
Minimum constraints ensure sections never shrink below a threshold. Batch them here.
Example
Constraint.from_mins([5, 10, 5]) # => [Constraint.min(5), Constraint.min(10), Constraint.min(5)]
- values
-
Enumerable of Integers.
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 195 def self.from_percentages(values) values.map { |v| percentage(v) } end
Converts an array of percentages into an array of Percentage constraints.
Percentage-based layouts distribute space proportionally. This method batches the creation.
Example
Constraint.from_percentages([25, 50, 25]) # => [Constraint.percentage(25), Constraint.percentage(50), Constraint.percentage(25)]
- values
-
Enumerable of Integers (0-100).
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 283 def self.from_ratios(pairs) pairs.map { |n, d| ratio(n, d) } end
Converts an array of ratio pairs into an array of Ratio constraints.
Ratio constraints define exact fractions of space. Batch them here.
Example
Constraint.from_ratios([[1, 4], [2, 4], [1, 4]]) # => [Constraint.ratio(1, 4), Constraint.ratio(2, 4), Constraint.ratio(1, 4)]
- pairs
-
Enumerable of
[numerator, denominator]arrays.
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 56 def self.length(v) new(type: :length, value: Integer(v)) end
Requests a fixed size.
Layout::Constraint.length(10) # 10 characters wide/high
- v
-
Number of cells (Integer).
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 109 def self.max(v) new(type: :max, value: Integer(v)) end
Enforces a maximum size.
Layout::Constraint.max(10) # At most 10 cells
- v
-
Maximum cells (Integer).
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 92 def self.min(v) new(type: :min, value: Integer(v)) end
Enforces a minimum size.
Layout::Constraint.min(5) # At least 5 cells
This section will grow if space permits, but never shrink below v.
- v
-
Minimum cells (Integer).
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 73 def self.percentage(v) new(type: :percentage, value: Integer(v)) end
Requests a percentage of available space.
Layout::Constraint.percentage(25) # 25% of the area
- v
-
Percentage 0-100 (Integer).
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 148 def self.ratio(numerator, denominator) new(type: :ratio, value: [Integer(numerator), Integer(denominator)]) end
Requests a specific ratio of the total space.
Layout::Constraint.ratio(1, 3) # 1/3rd of the area
- numerator
-
Top part of fraction (Integer).
- denominator
-
Bottom part of fraction (Integer).
Public Instance Methods
Source
# File lib/ratatui_ruby/layout/constraint.rb, line 311 def apply(length) length = Integer(length) case type when :length value when :percentage (length * value) / 100 when :min [value, length].max when :max [value, length].min when :fill length when :ratio numerator, denominator = value denominator.zero? ? 0 : (length * numerator) / denominator else length end end
Computes the size this constraint would produce given available space.
Layout engines use constraints to compute actual dimensions. Calling apply lets you preview the result without rendering.
Example
Constraint.percentage(50).apply(100) # => 50 Constraint.length(10).apply(100) # => 10 Constraint.min(10).apply(5) # => 10 Constraint.max(10).apply(15) # => 10 Constraint.ratio(1, 4).apply(100) # => 25
- length
-
Available space (Integer).
Returns the computed size (Integer).
Ruby-idiomatic alias (TIMTOWTDI) Allows proc-like invocation: constraint.(100)