Class: Vedeu::Geometries::DSL

Inherits:
Object
  • Object
show all
Includes:
Common, DSL, DSL::Geometry, DSL::Use
Defined in:
lib/vedeu/geometries/dsl/dsl.rb

Overview

Geometry allows the configuration of the position and size of an interface. Within Vedeu, as the same for ANSI terminals, has the origin at top-left, y = 1, x = 1. The ‘y’ coordinate is deliberately first.

The Geometry DSL can be used within the Interface DSL or standalone. Here are example of declarations for a ‘geometry` block:

A standalone geometry definition:

Vedeu.geometry :some_interface do
  height 5 # Sets the height of the view to 5
  width 20 # Sets the width of the view to 20
  x 3      # Start drawing 3 spaces from left
  y 10     # Start drawing 10 spaces from top
  xn 30    # Stop drawing 30 spaces from the left
  yn 20    # Stop drawing 20 spaces from top
end

An interface including a geometry definition:

Vedeu.interface :some_interface do
  geometry do
    height 5
    width 20
    x 3
    y 10
    xn 30
    yn 20
  end
  # ... some code here
end

If a declaration is omitted for ‘height` or `width` the full remaining space available in the terminal will be used. `x` and `y` both default to 1.

You can also make a geometry declaration dependent on another view:

Vedeu.interface :other_panel do
  # ... some code here
end

Vedeu.interface :main do
  geometry do
    height 10
    y { use(:other_panel).south }
  end
  # ... some code here
end

This view will begin just below “other_panel”.

This crude ASCII diagram represents a Geometry within Vedeu, each of the labels is a value you can access or define.

     x    north    xn           # north:  y - 1
   y +--------------+           # top:    y
     |     top      |           # west:   x - 1
     |              |           # left:   x
west | left   right | east      # right:  xn
     |              |           # east:   xn + 1
     |    bottom    |           # bottom: yn
  yn +--------------+           # south:  yn + 1
          south

Instance Attribute Summary

Attributes included from DSL

#client, #model

Instance Method Summary collapse

Methods included from DSL::Use

#use

Methods included from DSL::Geometry

included

Methods included from DSL

#attributes, #initialize, #method_missing, #name

Methods included from Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Vedeu::DSL

Instance Method Details

#align(vertical: :none, horizontal: :none, width: nil, height: nil) ⇒ Vedeu::Geometries::Geometry

Align the interface/view horizontally or vertically within the terminal.

Parameters:

  • vertical (Symbol) (defaults to: :none)

    One of :bottom, :middle, :none, :top.

  • horizontal (Symbol) (defaults to: :none)

    One of :center, :centre, :left, :none, :right.

  • width (Fixnum) (defaults to: nil)

    The number of characters/columns wide; this is required when the given value for horizontal is any value other than :none.

  • height (Fixnum) (defaults to: nil)

    The number of lines/rows tall; this is required when the given value for vertical is any value other than :none.

Returns:

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.



98
99
100
101
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 98

def align(vertical: :none, horizontal: :none, width: nil, height: nil)
  horizontal_alignment(horizontal, width)
  vertical_alignment(vertical, height)
end

#align_bottom(height = nil) ⇒ Vedeu::Geometries::Geometry

Vertically align the interface/view to the bottom of the terminal.

Vedeu.geometry :some_interface do
  # `height` is a positive integer, e.g. 30
  align_bottom 30

  # this is the same as:
  # vertical_alignment(:bottom, 30)

  # or you can use: (see notes)
  # align(vertical: :bottom, height: 30)

  # ... some code
end

Parameters:

  • height (Fixnum) (defaults to: nil)

    The number of lines/rows.

Returns:



144
145
146
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 144

def align_bottom(height = nil)
  vertical_alignment(:bottom, height)
end

#align_centre(width = nil) ⇒ Vedeu::Geometries::Geometry Also known as: align_center

Horizontally align the interface/view centrally.

Vedeu.geometry :some_interface do
  # `width` is a positive integer, e.g. 30
  align_centre 30

  # this is the same as:
  # horizontal_alignment(:centre, 30)

  # or you can use: (see notes)
  # align(horizontal: :centre, width: 30)

  # ... some code
end

# Also allows `align_center` if preferred.

Parameters:

  • width (Fixnum) (defaults to: nil)

    The number of characters/columns.

Returns:



167
168
169
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 167

def align_centre(width = nil)
  horizontal_alignment(:centre, width)
end

#align_left(width = nil) ⇒ Vedeu::Geometries::Geometry

Horizontally align the interface/view to the left.

Vedeu.geometry :some_interface do
  # `width` is a positive integer, e.g. 30
  align_left 30

  # this is the same as:
  # horizontal_alignment(:left, 30)

  # or you can use: (see notes)
  # align(horizontal: :left, width: 30)

  # ... some code
end

Parameters:

  • width (Fixnum) (defaults to: nil)

    The number of characters/columns.

Returns:



189
190
191
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 189

def align_left(width = nil)
  horizontal_alignment(:left, width)
end

#align_middle(height = nil) ⇒ Vedeu::Geometries::Geometry

Vertically align the interface/view to the middle of the terminal.

Vedeu.geometry :some_interface do
  # `height` is a positive integer, e.g. 30
  align_middle 30

  # this is the same as:
  # vertical_alignment(:middle, 30)

  # or you can use: (see notes)
  # align(vertical: :middle, height: 30)

  # ... some code
end

Parameters:

  • height (Fixnum) (defaults to: nil)

    The number of lines/rows.

Returns:



211
212
213
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 211

def align_middle(height = nil)
  vertical_alignment(:middle, height)
end

#align_right(width = nil) ⇒ Vedeu::Geometries::Geometry

Align the interface/view to the right.

Vedeu.geometry :some_interface do
  # `width` is a positive integer, e.g. 30
  align_right 30

  # this is the same as:
  # horizontal_alignment(:right, 30)

  # or you can use: (see notes)
  # align(horizontal: :right, width: 30)

  # ... some code
end

Parameters:

  • width (Fixnum) (defaults to: nil)

    The number of characters/columns.

Returns:



232
233
234
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 232

def align_right(width = nil)
  horizontal_alignment(:right, width)
end

#align_top(height = nil) ⇒ Vedeu::Geometries::Geometry

Vertically align the interface/view to the top of the terminal.

Vedeu.geometry :some_interface do
  # `height` is a positive integer, e.g. 30
  align_top 30

  # this is the same as:
  # vertical_alignment(:top, 30)

  # or you can use: (see notes)
  # align(vertical: :top, height: 30)

  # ... some code
end

Parameters:

  • height (Fixnum) (defaults to: nil)

    The number of lines/rows.

Returns:



254
255
256
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 254

def align_top(height = nil)
  vertical_alignment(:top, height)
end

#columns(value) ⇒ Fixnum|Vedeu::Error::OutOfRange

Returns the width in characters for the number of columns specified.

Vedeu.geometry :main_interface do
  # ... some code
  width columns(9) # Vedeu.width # => 92 (for example)
                   # 92 / 12 = 7
                   # 7 * 9 = 63
                   # Therefore, width is 63 characters.
end

Parameters:

  • value (Fixnum)

Returns:

Raises:

  • (Vedeu::Error::OutOfRange)

    When the value given for an argument or parameter cannot be used because it is outside the allowed range.



272
273
274
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 272

def columns(value)
  Vedeu::Geometries::Grid.columns(value)
end

#height(value) ⇒ Fixnum Also known as: height=

Specify the number of characters/rows/lines tall the interface will be. This value will be ignored when ‘y` and `yn` are set.

Vedeu.geometry :some_interface do
  height 8
  # ... some code
end

Parameters:

  • value (Fixnum)

Returns:

  • (Fixnum)


286
287
288
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 286

def height(value)
  model.height = proc { value }
end

#horizontal_alignment(value = :none, width = nil) ⇒ Vedeu::Geometries::Geometry

Parameters:

  • value (Symbol) (defaults to: :none)

    One of :center, :centre, :left, :none, :right.

  • width (Fixnum) (defaults to: nil)

    The number of characters/columns.

Returns:



107
108
109
110
111
112
113
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 107

def horizontal_alignment(value = :none, width = nil)
  alignment = Vedeu::Coercers::HorizontalAlignment.validate(value)

  model.width = width if width
  model.horizontal_alignment = alignment
  model
end

#rows(value) ⇒ Fixnum

Returns the height in characters for the number of rows specified.

Vedeu.geometry :main_interface do
  # ... some code
  height rows(3)  # Vedeu.height # => 38 (for example)
                  # 38 / 12 = 3
                  # 3 * 3 = 9
                  # Therefore, height is 9 characters.
end

Parameters:

  • value (Fixnum)

Returns:

  • (Fixnum)

Raises:

  • (Vedeu::Error::OutOfRange)

    When the value given for an argument or parameter cannot be used because it is outside the allowed range.



305
306
307
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 305

def rows(value)
  Vedeu::Geometries::Grid.rows(value)
end

#vertical_alignment(value = :none, height = nil) ⇒ Vedeu::Geometries::Geometry

Parameters:

  • value (Symbol) (defaults to: :none)

    One of :bottom, :middle, :none, :top.

  • height (Fixnum) (defaults to: nil)

    The number of lines/rows.

Returns:



118
119
120
121
122
123
124
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 118

def vertical_alignment(value = :none, height = nil)
  alignment = Vedeu::Coercers::VerticalAlignment.validate(value)

  model.height = height if height
  model.vertical_alignment = alignment
  model
end

#width(value) ⇒ Fixnum Also known as: width=

Specify the number of characters/columns wide the interface will be. This value will be ignored when ‘x` and `xn` are set.

Vedeu.geometry :some_interface do
  width 25
  # ... some code
end

Parameters:

  • value (Fixnum)

    The number of characters/columns.

Returns:

  • (Fixnum)


319
320
321
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 319

def width(value)
  model.width = proc { value }
end

#x(value = 1, &block) ⇒ Fixnum Also known as: x=

Specify the starting x position (column) of the interface.

Vedeu.geometry :some_interface do
  x 7 # start on column 7.

  # start on column 8, if :other_interface changes position
  # then :some_interface will too.
  x { use(:other_interface).east }
  # ... some code
end

Parameters:

  • value (Fixnum) (defaults to: 1)
  • block (Proc)

Returns:

  • (Fixnum)


338
339
340
341
342
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 338

def x(value = 1, &block)
  return model.x = block if block_given?

  model.x = value
end

#xn(value = 1, &block) ⇒ Fixnum Also known as: xn=

Specify the ending x position (column) of the interface. This value will override ‘width`.

Vedeu.geometry :some_interface do
  xn 37 # end at column 37.

  # when :other_interface changes position,
  # :some_interface will too.
  xn  { use(:other_interface).right }
  # ... some code
end

Parameters:

  • value (Fixnum) (defaults to: 1)
  • block (Proc)

Returns:

  • (Fixnum)


360
361
362
363
364
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 360

def xn(value = 1, &block)
  return model.xn = block if block_given?

  model.xn = value
end

#y(value = 1, &block) ⇒ Fixnum Also known as: y=

Specify the starting y position (row/line) of the interface.

Vedeu.geometry :some_interface do
  y  4 # start at row 4

  # start on row/line 3, when :other_interface changes
  # position, :some_interface will too.
  y  { use(:other_interface).north }
  # ... some code
end

Parameters:

  • value (Fixnum) (defaults to: 1)
  • block (Proc)

Returns:

  • (Fixnum)


381
382
383
384
385
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 381

def y(value = 1, &block)
  return model.y = block if block_given?

  model.y = value
end

#yn(value = 1, &block) ⇒ Fixnum Also known as: yn=

Specify the ending y position (row/line) of the interface. This value will override ‘height`.

Vedeu.geometry :some_interface do
  yn 24 # end at row 24.

  # if :other_interface changes position, :some_interface
  # will too.
  yn { use(:other_interface).bottom }
  # ... some code
end

Parameters:

  • value (Fixnum) (defaults to: 1)
  • block (Proc)

Returns:

  • (Fixnum)


403
404
405
406
407
# File 'lib/vedeu/geometries/dsl/dsl.rb', line 403

def yn(value = 1, &block)
  return model.yn = block if block_given?

  model.yn = value
end