Class: Vedeu::Cursors::Cursor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Model, Toggleable
Defined in:
lib/vedeu/cursors/cursor.rb,
lib/vedeu/cursors/repository.rb

Overview

Repository

Instance Attribute Summary collapse

Attributes included from Toggleable

#visible

Attributes included from Model

#repository

Instance Method Summary collapse

Methods included from Toggleable

included

Methods included from Model

#deputy, #dsl_class, included, #store

Methods included from Vedeu::Common

#demodulize, #present?, #snake_case

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Cursors::Cursor

Returns a new instance of Vedeu::Cursors::Cursor.

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):

  • name (String)

    The name of the interface this cursor belongs to.

  • ox (Fixnum)

    The offset x coordinate.

  • oy (Fixnum)

    The offset y coordinate.

  • repository (Vedeu::Repository)
  • visible (Boolean)

    The visibility of the cursor.

  • x (Fixnum)

    The terminal x coordinate for the cursor.

  • y (Fixnum)

    The terminal y coordinate for the cursor.



64
65
66
67
68
69
70
# File 'lib/vedeu/cursors/cursor.rb', line 64

def initialize(attributes = {})
  @attributes = defaults.merge!(attributes)

  @attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#attributesHash (readonly)

Returns:

  • (Hash)


22
23
24
# File 'lib/vedeu/cursors/cursor.rb', line 22

def attributes
  @attributes
end

#nameString (readonly)

Returns:

  • (String)


26
27
28
# File 'lib/vedeu/cursors/cursor.rb', line 26

def name
  @name
end

#oxFixnum

Returns:

  • (Fixnum)


189
190
191
192
# File 'lib/vedeu/cursors/cursor.rb', line 189

def ox
  @ox = 0 if @ox < 0
  @ox
end

#oyFixnum

Returns:

  • (Fixnum)


195
196
197
198
# File 'lib/vedeu/cursors/cursor.rb', line 195

def oy
  @oy = 0 if @oy < 0
  @oy
end

#stateBoolean|Symbol (readonly)

Returns:

  • (Boolean|Symbol)


38
39
40
# File 'lib/vedeu/cursors/cursor.rb', line 38

def state
  @state
end

#xFixnum

Returns The column/character coordinate.

Returns:

  • (Fixnum)

    The column/character coordinate.



237
238
239
240
241
242
243
244
# File 'lib/vedeu/cursors/cursor.rb', line 237

def x
  @x = bx  if @x < bx
  @x = bxn if @x > bxn

  @attributes[:x] = @x

  @x
end

#yFixnum

Returns The row/line coordinate.

Returns:

  • (Fixnum)

    The row/line coordinate.



247
248
249
250
251
252
253
254
# File 'lib/vedeu/cursors/cursor.rb', line 247

def y
  @y = by  if @y < by
  @y = byn if @y > byn

  @attributes[:y] = @y

  @y
end

Instance Method Details

#borderObject (private)

See Also:

  • Borders::Repository#by_name


259
260
261
# File 'lib/vedeu/cursors/cursor.rb', line 259

def border
  @border ||= Vedeu.borders.by_name(name)
end

#coordinateVedeu::Geometry::Coordinate (private)



264
265
266
# File 'lib/vedeu/cursors/cursor.rb', line 264

def coordinate
  @coordinate ||= Vedeu::Geometry::Coordinate.new(name, oy, ox)
end

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


271
272
273
274
275
276
277
278
279
280
281
# File 'lib/vedeu/cursors/cursor.rb', line 271

def defaults
  {
    name:       '',
    ox:         0,
    oy:         0,
    repository: Vedeu.cursors,
    visible:    false,
    x:          1,
    y:          1,
  }
end

#hideVedeu::Models::Escape

Hide a named cursor, or without a name, the cursor of the currently focussed interface.

Examples:

Vedeu.hide_cursor(name)

Returns:



182
183
184
185
186
# File 'lib/vedeu/cursors/cursor.rb', line 182

def hide
  super

  render
end

#move_downVedeu::Cursors::Cursor

Moves the cursor down by one row.



75
76
77
78
79
80
81
82
83
84
# File 'lib/vedeu/cursors/cursor.rb', line 75

def move_down
  @oy += 1

  @attributes = attributes.merge!(x:  x,
                                  y:  coordinate.y_position,
                                  ox: ox,
                                  oy: oy)

  Vedeu::Cursors::Cursor.new(@attributes).store
end

#move_leftVedeu::Cursors::Cursor

Moves the cursor left by one column.



89
90
91
92
93
94
95
96
97
98
# File 'lib/vedeu/cursors/cursor.rb', line 89

def move_left
  @ox -= 1

  @attributes = attributes.merge!(x:  coordinate.x_position,
                                  y:  y,
                                  ox: ox,
                                  oy: oy)

  Vedeu::Cursors::Cursor.new(@attributes).store
end

#move_originVedeu::Cursors::Cursor

Moves the cursor to the top left of the named interface.



103
104
105
106
107
# File 'lib/vedeu/cursors/cursor.rb', line 103

def move_origin
  @attributes = attributes.merge!(x: bx, y: by, ox: 0, oy: 0)

  Vedeu::Cursors::Cursor.new(@attributes).store
end

#move_rightVedeu::Cursors::Cursor

Moves the cursor right by one column.



112
113
114
115
116
117
118
119
120
121
# File 'lib/vedeu/cursors/cursor.rb', line 112

def move_right
  @ox += 1

  @attributes = attributes.merge!(x:  coordinate.x_position,
                                  y:  y,
                                  ox: ox,
                                  oy: oy)

  Vedeu::Cursors::Cursor.new(@attributes).store
end

#move_upVedeu::Cursors::Cursor

Moves the cursor up by one row.



126
127
128
129
130
131
132
133
134
135
# File 'lib/vedeu/cursors/cursor.rb', line 126

def move_up
  @oy -= 1

  @attributes = attributes.merge!(x:  x,
                                  y:  coordinate.y_position,
                                  ox: ox,
                                  oy: oy)

  Vedeu::Cursors::Cursor.new(@attributes).store
end

#positionVedeu::Geometry::Position

Return the position of this cursor.



203
204
205
# File 'lib/vedeu/cursors/cursor.rb', line 203

def position
  @position = Vedeu::Geometry::Position[y, x]
end

#renderArray<Vedeu::Models::Escape>

Renders the cursor.

Returns:



140
141
142
# File 'lib/vedeu/cursors/cursor.rb', line 140

def render
  Vedeu::Output::Output.render(visibility)
end

#reposition(new_y, new_x) ⇒ Vedeu::Cursors::Cursor

Arbitrarily move the cursor to a given position.

Parameters:

  • new_y (Fixnum)

    The row/line position.

  • new_x (Fixnum)

    The column/character position.

Returns:



149
150
151
152
153
154
155
156
# File 'lib/vedeu/cursors/cursor.rb', line 149

def reposition(new_y, new_x)
  @attributes = attributes.merge!(x:  coordinate.x_position,
                                  y:  coordinate.y_position,
                                  ox: new_x,
                                  oy: new_y)

  Vedeu::Cursors::Cursor.new(@attributes).store
end

#showVedeu::Models::Escape

Show a named cursor, or without a name, the cursor of the currently focussed interface.

Examples:

Vedeu.show_cursor(name)

Returns:



214
215
216
217
218
# File 'lib/vedeu/cursors/cursor.rb', line 214

def show
  super

  render
end

#to_sString Also known as: to_str

Returns an escape sequence to position the cursor and set its visibility. When passed a block, will position the cursor, yield and return the original position.

Returns:

  • (String)


163
164
165
166
167
168
169
170
171
# File 'lib/vedeu/cursors/cursor.rb', line 163

def to_s
  if block_given?
    "#{position}#{yield}#{visibility}"

  else
    "#{visibility}"

  end
end

#toggleVedeu::Models::Escape

Toggle the visibility of the cursor with the given name.

Examples:

Vedeu.toggle_cursor(name)

Returns:



226
227
228
229
230
231
232
233
234
# File 'lib/vedeu/cursors/cursor.rb', line 226

def toggle
  if visible?
    hide

  else
    show

  end
end

#visibilityString (private)

Returns the escape sequence for setting the visibility of the cursor.

Returns:

  • (String)


287
288
289
290
291
# File 'lib/vedeu/cursors/cursor.rb', line 287

def visibility
  value = visible? ? Vedeu::Esc.show_cursor : Vedeu::Esc.hide_cursor

  Vedeu::Models::Escape.new(position: position, value: value)
end