class Antimony::Session

Constants

KEYBOARD_METHODS

Public Class Methods

new(host) click to toggle source
# File lib/antimony/session.rb, line 3
def initialize(host)
  @cursor_position = 0
  @screen_text = blank_screen
  @session_log = []

  @connection = Net::Telnet.new('Host' => host, 'Timeout' => 10)

  update_screen
end

Public Instance Methods

close() click to toggle source
# File lib/antimony/session.rb, line 13
def close
  @connection.close
end
log(text) click to toggle source
# File lib/antimony/session.rb, line 56
def log(text)
  @session_log.push(text)
end
log_screen() click to toggle source
# File lib/antimony/session.rb, line 60
def log_screen
  log(printable_screen_text.join("\n"))
end
print_screen() click to toggle source
printable_screen_text() click to toggle source
# File lib/antimony/session.rb, line 35
def printable_screen_text
  @screen_text.join.scan(LINE)
end
screen_text() click to toggle source
# File lib/antimony/session.rb, line 31
def screen_text
  @screen_text.join
end
send_keys(keys, count = 1, text = true) click to toggle source
# File lib/antimony/session.rb, line 17
def send_keys(keys, count = 1, text = true)
  count.times { @connection.print keys }
  update_screen
  add_text(keys) if text
  log_screen
  print_screen if Antimony.configuration.show_output
end
session_log() click to toggle source
# File lib/antimony/session.rb, line 45
def session_log
  txt = EMPTY.clone
  @session_log.each_with_index do |entry, i|
    txt += LOG_SEPARATOR + ENTER
    txt += "#{i}: #{ENTER}"
    txt += entry + ENTER
  end
  txt += LOG_SEPARATOR
  txt
end
value_at(row, column, length) click to toggle source
# File lib/antimony/session.rb, line 25
def value_at(row, column, length)
  start_index = ((row - 1) * 80) + (column - 1)
  end_index = start_index + length - 1
  @screen_text[start_index..end_index].join
end

Private Instance Methods

add_text(text) click to toggle source
# File lib/antimony/session.rb, line 72
def add_text(text)
  text.chars.each do |char|
    @screen_text[@cursor_position] = char
    @cursor_position += 1
  end
end
blank_screen() click to toggle source
# File lib/antimony/session.rb, line 116
def blank_screen
  (1..1920).to_a.map { |_i| SPACE }
end
chunk() click to toggle source
# File lib/antimony/session.rb, line 85
def chunk
  @chunk = @connection.waitfor RECEIVE_OPTS rescue nil
end
cursor_position(esc) click to toggle source
# File lib/antimony/session.rb, line 89
def cursor_position(esc)
  esc_code = /\e\[/
  pos = esc.gsub(esc_code, EMPTY).gsub(H, EMPTY).split(SEMICOLON)
  row_index = pos[0].to_i - 1
  col_index = pos[1].to_i - 1
  (row_index * 80) + col_index
end
parse_ansi() click to toggle source
# File lib/antimony/session.rb, line 97
def parse_ansi
  ansi = @screen_data.scan(ANSI_REGEX).map do |e|
    {
      value: e[0],
      type: e[0].include?(ESCAPE) ? :esc : :chr
    }
  end

  ansi.each do |e|
    if (e[:type] == :esc) && (e[:value].end_with? H)
      @cursor_position = cursor_position(e[:value])
    elsif e[:type] == :chr
      @screen_text[@cursor_position] = e[:value]
      @cursor_position += 1
    end
  end
  @screen_text
end
receive_data() click to toggle source
# File lib/antimony/session.rb, line 79
def receive_data
  whole = []
  whole.push(@chunk) while chunk
  whole.join
end
update_screen() click to toggle source
# File lib/antimony/session.rb, line 66
def update_screen
  @screen_data = receive_data
  @screen_text = blank_screen if @screen_data.include?('[2J')
  parse_ansi
end