class Antimony::Session

Constants

KEYBOARD_METHODS

Public Class Methods

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

  puts "Establishing SSH tunnel ..."
  @gateway = Net::SSH::Gateway.new(host, username, :password => password)
  tunnel_port = @gateway.open(host, 23)

  puts "Connecting to #{host} using local port #{tunnel_port} ..."
  @connection = Net::Telnet.new(
    'Host' => 'localhost',
    'Port' => tunnel_port,
    'Timeout' => 10
  )

  update_screen
end

Public Instance Methods

close() click to toggle source
# File lib/antimony/session.rb, line 23
def close
  @connection.close
  @gateway.shutdown!
end
log(text) click to toggle source
# File lib/antimony/session.rb, line 67
def log(text)
  @session_log.push(text)
end
log_screen() click to toggle source
# File lib/antimony/session.rb, line 71
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 46
def printable_screen_text
  @screen_text.join.scan(LINE)
end
screen_text() click to toggle source
# File lib/antimony/session.rb, line 42
def screen_text
  @screen_text.join
end
send_keys(keys, count = 1, text = true) click to toggle source
# File lib/antimony/session.rb, line 28
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 56
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 36
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 83
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 128
def blank_screen
  (1..1920).to_a.map { |_i| SPACE }
end
chunk() click to toggle source
# File lib/antimony/session.rb, line 96
def chunk
  @chunk = @connection.waitfor RECEIVE_OPTS
rescue # rubocop:disable Lint/HandleExceptions
end
cursor_position(esc) click to toggle source
# File lib/antimony/session.rb, line 101
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 109
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 90
def receive_data
  whole = []
  whole.push(@chunk) while chunk
  whole.join
end
update_screen() click to toggle source
# File lib/antimony/session.rb, line 77
def update_screen
  @screen_data = receive_data
  @screen_text = blank_screen if @screen_data.include?('[2J')
  parse_ansi
end