Class: OTerm::VT100

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

Direct Known Subclasses

Output

Constant Summary collapse

ESC =
27.chr
MAX_DIM =
9999
BLANK =

graphic font characters

'_'
DIAMOND =
'`'
CHECKER =
'a'
HT =
'b'
FF =
'c'
CR =
'd'
LF =
'e'
DEGREE =
'f'
PLUS_MINUS =
'g'
NL =
'h'
VT =
'i'
LOW_RIGHT =
'j'
UP_RIGHT =
'k'
UP_LEFT =
'l'
LOW_LEFT =
'm'
CROSS =
'n'
DASH1 =
'o'
DASH3 =
'p'
DASH5 =
'q'
DASH7 =
'r'
DASH9 =
's'
LEFT_T =
't'
RIGHT_T =
'u'
LOW_T =
'v'
UP_T =
'w'
BAR =
'x'
LTE =
'y'
GTE =
'z'
PI =
'{'
NE =
'|'
DOT =
'~'
BLACK =

colors

30
RED =
31
GREEN =
32
YELLOW =
33
BLUE =
34
MAGENTA =
35
CYAN =
36
WHITE =
37

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(con) ⇒ VT100

Returns a new instance of VT100.



53
54
55
56
# File 'lib/oterm/vt100.rb', line 53

def initialize(con)
  @con = con
  @is_vt100 = false
end

Instance Attribute Details

#conObject

Returns the value of attribute con.



51
52
53
# File 'lib/oterm/vt100.rb', line 51

def con
  @con
end

Instance Method Details

#absolute_originObject



132
133
134
# File 'lib/oterm/vt100.rb', line 132

def absolute_origin()
  @con.print("\x1b[?6l") if @is_vt100
end

#attrs_offObject



136
137
138
# File 'lib/oterm/vt100.rb', line 136

def attrs_off()
  @con.print("\x1b[m") if @is_vt100
end

#big_bottomObject



168
169
170
# File 'lib/oterm/vt100.rb', line 168

def big_bottom()
  @con.print("\x1b#4") if @is_vt100
end

#big_topObject



164
165
166
# File 'lib/oterm/vt100.rb', line 164

def big_top()
  @con.print("\x1b#3") if @is_vt100
end


152
153
154
# File 'lib/oterm/vt100.rb', line 152

def blink()
  @con.print("\x1b[5m") if @is_vt100
end

#boldObject



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

def bold()
  @con.print("\x1b[1m") if @is_vt100
end

#clear_lineObject



116
117
118
# File 'lib/oterm/vt100.rb', line 116

def clear_line()
  @con.print("\x1b[2K") if @is_vt100
end

#clear_screenObject



112
113
114
# File 'lib/oterm/vt100.rb', line 112

def clear_screen()
  @con.print("\x1b[2J") if @is_vt100
end

#clear_to_endObject



120
121
122
# File 'lib/oterm/vt100.rb', line 120

def clear_to_end()
  @con.print("\x1b[0K") if @is_vt100
end

#clear_to_startObject



124
125
126
# File 'lib/oterm/vt100.rb', line 124

def clear_to_start()
  @con.print("\x1b[1K") if @is_vt100
end

#default_fontObject



107
108
109
110
# File 'lib/oterm/vt100.rb', line 107

def default_font()
  # TBD allow to set to specific character set for original terminal, for now US font
  @con.print("\x1b(B") if @is_vt100
end

#dimObject



144
145
146
# File 'lib/oterm/vt100.rb', line 144

def dim()
  @con.print("\x1b[2m") if @is_vt100
end

#down(n) ⇒ Object



201
202
203
# File 'lib/oterm/vt100.rb', line 201

def down(n)
  @con.print("\x1b[#{n}B") if @is_vt100
end

#frame(y, x, h, w) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/oterm/vt100.rb', line 235

def frame(y, x, h, w)
  return if 2 > h || 2 > w
  graphic_font()
  set_cursor(y, x)
  @con.print(UP_LEFT + DASH5 * (w - 2) + UP_RIGHT)
  (h - 2).times do |i|
    i += 1
    set_cursor(y + i, x)
    @con.print(BAR)
    set_cursor(y + i, x + w - 1)
    @con.print(BAR)
  end
  set_cursor(y + h - 1, x)
  @con.print(LOW_LEFT + DASH5 * (w - 2) + LOW_RIGHT)
  default_font()
end

#get_cursorObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/oterm/vt100.rb', line 71

def get_cursor()
  v, h = 0, 0
  if @is_vt100
    @con.print("\x1b[6n")
    # expect: ^[<v>;<h>R
    rx = /^\x1b\[(\d+);(\d+)R/
    m = recv_wait(16, 1.0, rx)
    v, h = m.captures.map { |s| s.to_i }
  end
  return v, h
end

#graphic_fontObject



103
104
105
# File 'lib/oterm/vt100.rb', line 103

def graphic_font()
  @con.print("\x1b(2") if @is_vt100
end

#homeObject



213
214
215
# File 'lib/oterm/vt100.rb', line 213

def home()
  @con.print("\x1b[H") if @is_vt100
end

#identifyObject



62
63
64
65
66
67
68
69
# File 'lib/oterm/vt100.rb', line 62

def identify()
  @con.print("\x1b[c")
  # expect: ^[[?1;<n>0c
  rx = /^\x1b\[\?1;.+c/
  m = recv_wait(10, 1.0, rx)
  # Don't care about the type, just that the reply is valid for a vt100.
  @is_vt100 = nil != m
end

#invisibleObject



160
161
162
# File 'lib/oterm/vt100.rb', line 160

def invisible()
  @con.print("\x1b[8m") if @is_vt100
end

#is_vt100?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/oterm/vt100.rb', line 58

def is_vt100?()
  return @is_vt100
end

#left(n) ⇒ Object



205
206
207
# File 'lib/oterm/vt100.rb', line 205

def left(n)
  @con.print("\x1b[#{n}D") if @is_vt100
end

#narrowObject



172
173
174
# File 'lib/oterm/vt100.rb', line 172

def narrow()
  @con.print("\x1b#5") if @is_vt100
end

#recv_wait(max, timeout, pat) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/oterm/vt100.rb', line 252

def recv_wait(max, timeout, pat)
  giveup = Time.now + timeout
  reply = ''
  begin
    while nil == pat.match(reply)
      # just peek incase the string is not what we want
      reply = @con.recv_nonblock(max, Socket::MSG_PEEK)

      # DEBUG
      # reply.each_byte { |x| print("#{x} ") }
      # plain = reply.gsub(/./) { |c| c.ord < 32 || 127 <= c.ord  ? '*' : c }
      # puts "[#{reply.size()}] #{plain}"

    end
  rescue IO::WaitReadable
    now = Time.now
    if now < giveup
      IO.select([@con], [], [], giveup - now)
      retry
    end
  end
  m = pat.match(reply)
  if nil != m
    # There was a match so read the characters we already peeked.
    cnt = m.to_s().size
    if 0 < cnt
      @con.recv_nonblock(cnt)
    end
  end
  m
end

#relative_originObject



128
129
130
# File 'lib/oterm/vt100.rb', line 128

def relative_origin()
  @con.print("\x1b[?6h") if @is_vt100
end

#resetObject

Reset terminal to initial state.



99
100
101
# File 'lib/oterm/vt100.rb', line 99

def reset()
  @con.print("\x1bc") if @is_vt100
end

#restore_cursorObject

Restore cursor position and attributes.



94
95
96
# File 'lib/oterm/vt100.rb', line 94

def restore_cursor()
  @con.print("\x1b8") if @is_vt100
end

#reverseObject



156
157
158
# File 'lib/oterm/vt100.rb', line 156

def reverse()
  @con.print("\x1b[7m") if @is_vt100
end

#right(n) ⇒ Object



209
210
211
# File 'lib/oterm/vt100.rb', line 209

def right(n)
  @con.print("\x1b[#{n}C") if @is_vt100
end

#save_cursorObject

Save cursor position and attributes.



89
90
91
# File 'lib/oterm/vt100.rb', line 89

def save_cursor()
  @con.print("\x1b7") if @is_vt100
end

#screen_sizeObject



227
228
229
230
231
232
233
# File 'lib/oterm/vt100.rb', line 227

def screen_size()
  save_cursor()
  set_cursor(MAX_DIM, MAX_DIM)
  h, w = get_cursor()
  restore_cursor()
  return h, w
end

#scroll(n) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/oterm/vt100.rb', line 217

def scroll(n)
  return unless @is_vt100
  if 0 > n
    n = -n
    n.times { @con.print("\x1b[D") }
  elsif 0 < n
    n.times { @con.print("\x1b[M") }
  end
end

#set_colors(fg, bg) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/oterm/vt100.rb', line 180

def set_colors(fg, bg)
  return unless @is_vt100
  if nil == fg
    if nil != bg
      bg += 10
      @con.print("\x1b[#{bg}m")
    end
  else
    if nil != bg
      bg += 10
      @con.print("\x1b[#{fg};#{bg}m")
    else
      @con.print("\x1b[#{fg}m")
    end
  end
end

#set_cursor(v, h) ⇒ Object

Move cursor to screen location v, h.



84
85
86
# File 'lib/oterm/vt100.rb', line 84

def set_cursor(v, h)
  @con.print("\x1b[#{v};#{h}H") if @is_vt100
end

#underlineObject



148
149
150
# File 'lib/oterm/vt100.rb', line 148

def underline()
  @con.print("\x1b[4m") if @is_vt100
end

#up(n) ⇒ Object



197
198
199
# File 'lib/oterm/vt100.rb', line 197

def up(n)
  @con.print("\x1b[#{n}A") if @is_vt100
end

#wideObject



176
177
178
# File 'lib/oterm/vt100.rb', line 176

def wide()
  @con.print("\x1b#6") if @is_vt100
end