Class: Vedeu::Geometry::DSL

Inherits:
Object
  • Object
show all
Includes:
DSL, DSL::Use
Defined in:
lib/vedeu/geometry/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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Use

#use

Methods included from DSL

#attributes, #method_missing

Constructor Details

#initialize(model, client = nil) ⇒ Vedeu::Geometry::DSL

Returns an instance of Vedeu::Geometry::DSL.



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

def initialize(model, client = nil)
  @model  = model
  @client = client
end

Dynamic Method Handling

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

Class Method Details

.geometry(name, &block) ⇒ Vedeu::Geometry::Geometry

Specify the geometry of an interface or view with a simple DSL.

Vedeu.geometry 'some_interface' do
  # ...
end

Raises:



87
88
89
90
91
# File 'lib/vedeu/geometry/dsl.rb', line 87

def self.geometry(name, &block)
  fail Vedeu::Error::InvalidSyntax, 'block not given' unless block_given?

  Vedeu::Geometry::Geometry.build(name: name, &block).store
end

Instance Method Details

#centred(value = true) ⇒ Boolean Also known as: centred!, centred=

Instructs Vedeu to calculate x and y geometry automatically based on the centre character of the terminal, the width and the height.

Vedeu.geometry 'some_interface' do
  centred false # or...

  centred true  # or...
  centred!      # or...
  # ... some code
end


117
118
119
120
121
# File 'lib/vedeu/geometry/dsl.rb', line 117

def centred(value = true)
  boolean = value ? true : false

  model.centred = boolean
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

Raises:



139
140
141
# File 'lib/vedeu/geometry/dsl.rb', line 139

def columns(value)
  Vedeu::Geometry::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


153
154
155
# File 'lib/vedeu/geometry/dsl.rb', line 153

def height(value)
  model.height = proc { value }
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

Raises:



172
173
174
# File 'lib/vedeu/geometry/dsl.rb', line 172

def rows(value)
  Vedeu::Geometry::Grid.rows(value)
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


186
187
188
# File 'lib/vedeu/geometry/dsl.rb', line 186

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.

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


204
205
206
207
208
# File 'lib/vedeu/geometry/dsl.rb', line 204

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.

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


225
226
227
228
229
# File 'lib/vedeu/geometry/dsl.rb', line 225

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

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


245
246
247
248
249
# File 'lib/vedeu/geometry/dsl.rb', line 245

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.

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


265
266
267
268
269
# File 'lib/vedeu/geometry/dsl.rb', line 265

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

  model.yn = value
end