Class: RubyTUI::Layout::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytui/layout/constraint.rb

Overview

A sizing rule for one segment of a Layout split.

Create instances with the factory methods; Layout#split resolves them. Fixed constraints (Constraint.length, Constraint.percentage, Constraint.ratio) are allocated first, then the remaining space is shared among Constraint.fill, Constraint.min and Constraint.max constraints.

Examples:

Header, body and status line

layout = Layout::Layout.vertical([
  Layout::Constraint.length(3),
  Layout::Constraint.fill,
  Layout::Constraint.length(1)
])
header, body, status = layout.split(area)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, value) ⇒ Constraint

Returns a new instance of Constraint.

Parameters:

  • kind (Symbol) —

    :length, :percentage, :min, :max, :fill or :ratio

  • value (Integer, Array(Integer, Integer)) —

    size parameter; a [num, den] pair for :ratio



25
26
27
28
# File 'lib/rubytui/layout/constraint.rb', line 25

def initialize(kind, value)
  @kind = kind
  @value = value
end

Instance Attribute Details

#kind ⇒ Object (readonly)

Returns the value of attribute kind.



20
21
22
# File 'lib/rubytui/layout/constraint.rb', line 20

def kind
  @kind
end

#value ⇒ Object (readonly)

Returns the value of attribute value.



20
21
22
# File 'lib/rubytui/layout/constraint.rb', line 20

def value
  @value
end

Class Method Details

.fill(weight = 1) ⇒ Constraint

A share of the space left after fixed constraints, proportional to weight (+min+ and max constraints count as weight 1).

Parameters:

  • weight (Integer) (defaults to: 1) —

    relative weight (default: 1)

Returns:



59
60
61
62
63
64
65
# File 'lib/rubytui/layout/constraint.rb', line 59

def fill(weight = 1)  = new(:fill, weight)
# Fraction <tt>num / den</tt> of the available space, rounded; reduced if
# less space remains.
#
# @param num [Integer] numerator
# @param den [Integer] denominator
# @return [Constraint]

.length(n) ⇒ Constraint

Fixed size, reduced if less space remains.

Parameters:

  • n (Integer) —

    size in cells

Returns:



36
37
38
39
40
# File 'lib/rubytui/layout/constraint.rb', line 36

def length(n)         = new(:length, n)
# Percentage of the available space, rounded; reduced if less space remains.
#
# @param n [Numeric] percentage (0-100)
# @return [Constraint]

.max(n) ⇒ Constraint

A weight-1 share of the space left after fixed constraints, capped at n cells.

Parameters:

  • n (Integer) —

    maximum size in cells

Returns:



53
54
55
56
57
58
# File 'lib/rubytui/layout/constraint.rb', line 53

def max(n)            = new(:max, n)
# A share of the space left after fixed constraints, proportional to
# +weight+ (+min+ and +max+ constraints count as weight 1).
#
# @param weight [Integer] relative weight (default: 1)
# @return [Constraint]

.min(n) ⇒ Constraint

At least n cells when space allows, plus a weight-1 share of the space left after fixed constraints.

Parameters:

  • n (Integer) —

    minimum size in cells

Returns:



47
48
49
50
51
52
# File 'lib/rubytui/layout/constraint.rb', line 47

def min(n)            = new(:min, n)
# A weight-1 share of the space left after fixed constraints, capped
# at +n+ cells.
#
# @param n [Integer] maximum size in cells
# @return [Constraint]

.percentage(n) ⇒ Constraint

Percentage of the available space, rounded; reduced if less space remains.

Parameters:

  • n (Numeric) —

    percentage (0-100)

Returns:



41
42
43
44
45
46
# File 'lib/rubytui/layout/constraint.rb', line 41

def percentage(n)     = new(:percentage, n)
# At least +n+ cells when space allows, plus a weight-1 share of the
# space left after fixed constraints.
#
# @param n [Integer] minimum size in cells
# @return [Constraint]

.ratio(num, den) ⇒ Constraint

Fraction num / den of the available space, rounded; reduced if less space remains.

Parameters:

  • num (Integer) —

    numerator

  • den (Integer) —

    denominator

Returns:



66
# File 'lib/rubytui/layout/constraint.rb', line 66

def ratio(num, den)   = new(:ratio, [num, den].freeze)

Instance Method Details

#==(other) ⇒ Boolean

Value equality: same kind and value.

Parameters:

  • other (Object) —

    object to compare

Returns:

  • (Boolean) —

    true if other is a Constraint with equal #kind and #value



73
74
75
# File 'lib/rubytui/layout/constraint.rb', line 73

def ==(other)
  other.is_a?(Constraint) && @kind == other.kind && @value == other.value
end

#inspect ⇒ String

Returns same as #to_s.

Returns:

  • (String) —

    same as #to_s



80
# File 'lib/rubytui/layout/constraint.rb', line 80

def inspect = to_s

#to_s ⇒ String

Returns human-readable form, e.g. Constraint(length: 3).

Returns:

  • (String) —

    human-readable form, e.g. Constraint(length: 3)



78
79
# File 'lib/rubytui/layout/constraint.rb', line 78

def to_s = "Constraint(#{@kind}: #{@value})"
# @return [String] same as {#to_s}