class OTerm::VT100

Constants

BAR
BLACK

colors

BLANK

graphic font characters

BLUE
CHECKER
CR
CROSS
CYAN
DASH1
DASH3
DASH5
DASH7
DASH9
DEGREE
DIAMOND
DOT
ESC
FF
GREEN
GTE
HT
LEFT_T
LF
LOW_LEFT
LOW_RIGHT
LOW_T
LTE
MAGENTA
MAX_DIM
NE
NL
PI
PLUS_MINUS
RED
RIGHT_T
UP_LEFT
UP_RIGHT
UP_T
VT
WHITE
YELLOW

Attributes

con[RW]

Public Class Methods

new(con) click to toggle source
# File lib/oterm/vt100.rb, line 53
def initialize(con)
  @con = con
  @is_vt100 = false
end

Public Instance Methods

absolute_origin() click to toggle source
# File lib/oterm/vt100.rb, line 132
def absolute_origin()
  @con.print("\x1b[?6l") if @is_vt100
end
attrs_off() click to toggle source
# File lib/oterm/vt100.rb, line 136
def attrs_off()
  @con.print("\x1b[m") if @is_vt100
end
big_bottom() click to toggle source
# File lib/oterm/vt100.rb, line 168
def big_bottom()
  @con.print("\x1b#4") if @is_vt100
end
big_top() click to toggle source
# File lib/oterm/vt100.rb, line 164
def big_top()
  @con.print("\x1b#3") if @is_vt100
end
bold() click to toggle source
# File lib/oterm/vt100.rb, line 140
def bold()
  @con.print("\x1b[1m") if @is_vt100
end
clear_line() click to toggle source
# File lib/oterm/vt100.rb, line 116
def clear_line()
  @con.print("\x1b[2K") if @is_vt100
end
clear_screen() click to toggle source
# File lib/oterm/vt100.rb, line 112
def clear_screen()
  @con.print("\x1b[2J") if @is_vt100
end
clear_to_end() click to toggle source
# File lib/oterm/vt100.rb, line 120
def clear_to_end()
  @con.print("\x1b[0K") if @is_vt100
end
clear_to_start() click to toggle source
# File lib/oterm/vt100.rb, line 124
def clear_to_start()
  @con.print("\x1b[1K") if @is_vt100
end
default_font() click to toggle source
# 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
dim() click to toggle source
# File lib/oterm/vt100.rb, line 144
def dim()
  @con.print("\x1b[2m") if @is_vt100
end
down(n) click to toggle source
# File lib/oterm/vt100.rb, line 201
def down(n)
  @con.print("\x1b[#{n}B") if @is_vt100
end
frame(y, x, h, w) click to toggle source
# 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_cursor() click to toggle source
# 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_font() click to toggle source
# File lib/oterm/vt100.rb, line 103
def graphic_font()
  @con.print("\x1b(2") if @is_vt100
end
home() click to toggle source
# File lib/oterm/vt100.rb, line 213
def home()
  @con.print("\x1b[H") if @is_vt100
end
identify() click to toggle source
# 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
invisible() click to toggle source
# File lib/oterm/vt100.rb, line 160
def invisible()
  @con.print("\x1b[8m") if @is_vt100
end
is_vt100?() click to toggle source
# File lib/oterm/vt100.rb, line 58
def is_vt100?()
  return @is_vt100
end
left(n) click to toggle source
# File lib/oterm/vt100.rb, line 205
def left(n)
  @con.print("\x1b[#{n}D") if @is_vt100
end
narrow() click to toggle source
# File lib/oterm/vt100.rb, line 172
def narrow()
  @con.print("\x1b#5") if @is_vt100
end
recv_wait(max, timeout, pat) click to toggle source
# 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_origin() click to toggle source
# File lib/oterm/vt100.rb, line 128
def relative_origin()
  @con.print("\x1b[?6h") if @is_vt100
end
reset() click to toggle source

Reset terminal to initial state.

# File lib/oterm/vt100.rb, line 99
def reset()
  @con.print("\x1bc") if @is_vt100
end
restore_cursor() click to toggle source

Restore cursor position and attributes.

# File lib/oterm/vt100.rb, line 94
def restore_cursor()
  @con.print("\x1b8") if @is_vt100
end
reverse() click to toggle source
# File lib/oterm/vt100.rb, line 156
def reverse()
  @con.print("\x1b[7m") if @is_vt100
end
right(n) click to toggle source
# File lib/oterm/vt100.rb, line 209
def right(n)
  @con.print("\x1b[#{n}C") if @is_vt100
end
save_cursor() click to toggle source

Save cursor position and attributes.

# File lib/oterm/vt100.rb, line 89
def save_cursor()
  @con.print("\x1b7") if @is_vt100
end
screen_size() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Move cursor to screen location v, h.

# File lib/oterm/vt100.rb, line 84
def set_cursor(v, h)
  @con.print("\x1b[#{v};#{h}H") if @is_vt100
end
underline() click to toggle source
# File lib/oterm/vt100.rb, line 148
def underline()
  @con.print("\x1b[4m") if @is_vt100
end
up(n) click to toggle source
# File lib/oterm/vt100.rb, line 197
def up(n)
  @con.print("\x1b[#{n}A") if @is_vt100
end
wide() click to toggle source
# File lib/oterm/vt100.rb, line 176
def wide()
  @con.print("\x1b#6") if @is_vt100
end