Class: AnsiSys::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/ansisys.rb

Constant Summary collapse

CODE_LETTERS =

Escape sequence codes processed in this Class

%w(A B C D E F G H f)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cur_col = 1, cur_row = 1, max_col = 80, max_row = 25) ⇒ Cursor

Returns a new instance of Cursor.



142
143
144
145
146
147
# File 'lib/ansisys.rb', line 142

def initialize(cur_col = 1, cur_row = 1, max_col = 80, max_row = 25)
  @cur_col = cur_col
  @cur_row = cur_row
  @max_col = max_col
  @max_row = max_row
end

Instance Attribute Details

#cur_colObject (readonly)

current column number (1-)



137
138
139
# File 'lib/ansisys.rb', line 137

def cur_col
  @cur_col
end

#cur_rowObject (readonly)

current row number (1-)



138
139
140
# File 'lib/ansisys.rb', line 138

def cur_row
  @cur_row
end

#max_colObject

maximum column number



139
140
141
# File 'lib/ansisys.rb', line 139

def max_col
  @max_col
end

#max_rowObject

maximum row number



140
141
142
# File 'lib/ansisys.rb', line 140

def max_row
  @max_row
end

Instance Method Details

#advance!(width = 1) ⇒ Object

changes current location for a character with width to be echoed



194
195
196
197
198
199
200
201
202
# File 'lib/ansisys.rb', line 194

def advance!(width = 1)
  r = nil
  @cur_col += width
  if @cur_col > @max_col
    line_feed!
    r = "\n"
  end
  return r
end

#apply_code!(letter, *pars) ⇒ Object

applies self an escape sequence code that ends with letter as String and with some pars as Integers



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ansisys.rb', line 151

def apply_code!(letter, *pars)
  case letter
  when 'A'
    @cur_row -= pars[0] ? pars[0] : 1
    @cur_row = @max_row if @max_row and @cur_row > @max_row
  when 'B'
    @cur_row += pars[0] ? pars[0] : 1
    @cur_row = @max_row if @max_row and @cur_row > @max_row
  when 'C'
    @cur_col += pars[0] ? pars[0] : 1
  when 'D'
    @cur_col -= pars[0] ? pars[0] : 1
  when 'E'
    @cur_row += pars[0] ? pars[0] : 1
    @cur_col = 1
    @max_row = @cur_row if @max_row and @cur_row > @max_row
  when 'F'
    @cur_row -= pars[0] ? pars[0] : 1
    @cur_col = 1
    @max_row = @cur_row if @max_row and @cur_row > @max_row
  when 'G'
    @cur_col = pars[0] ? pars[0] : 1
  when 'H'
    @cur_row = pars[0] ? pars[0] : 1
    @cur_col = pars[1] ? pars[1] : 1
    @max_row = @cur_row if @max_row and @cur_row > @max_row
  when 'f'
    @cur_row = pars[0] ? pars[0] : 1
    @cur_col = pars[1] ? pars[1] : 1
    @max_row = @cur_row if @max_row and @cur_row > @max_row
  end
  if @cur_row < 1
    @cur_row = 1
  end
  if @cur_col < 1
    @cur_col = 1
  elsif @cur_col > @max_col
    @cur_col = @max_col
  end
  return self
end

#fit!(width = 1) ⇒ Object

check if a character with width fits within the maximum columns, feed a line if not



206
207
208
209
210
211
212
213
# File 'lib/ansisys.rb', line 206

def fit!(width = 1)
  r = nil
  if @cur_col + width > @max_col + 1
    line_feed!
    r = "\n"
  end
  return r
end

#line_feed!Object

feed a line



216
217
218
219
220
# File 'lib/ansisys.rb', line 216

def line_feed!
  @cur_col = 1
  @cur_row += 1
  @max_row = @cur_row if @max_row and @cur_row > @max_row
end