2013-09-14 11:16:47 +00:00
|
|
|
require 'open3'
|
|
|
|
|
2013-08-04 21:06:56 +00:00
|
|
|
class Terminal
|
|
|
|
|
2013-09-21 10:26:26 +00:00
|
|
|
BINARY_PATH = (Rails.root + "bin" + "terminal").to_s
|
2013-09-14 11:16:47 +00:00
|
|
|
|
2013-08-04 21:06:56 +00:00
|
|
|
def initialize(width, height)
|
2013-09-14 11:16:47 +00:00
|
|
|
@process = Process.new("#{BINARY_PATH} #{width} #{height}")
|
2013-08-04 21:06:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def feed(data)
|
2013-09-20 20:38:59 +00:00
|
|
|
process.write("d\n#{data.bytesize}\n")
|
|
|
|
process.write(data)
|
2013-08-13 17:33:01 +00:00
|
|
|
end
|
2013-08-04 21:06:56 +00:00
|
|
|
|
2013-08-13 17:33:01 +00:00
|
|
|
def snapshot
|
2013-09-14 11:16:47 +00:00
|
|
|
process.write("p\n")
|
2013-09-22 10:25:34 +00:00
|
|
|
lines = Yajl::Parser.new.parse(process.read_line)
|
2013-08-04 21:06:56 +00:00
|
|
|
|
2013-09-10 19:29:17 +00:00
|
|
|
Snapshot.build(lines)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cursor
|
2013-09-14 11:16:47 +00:00
|
|
|
process.write("c\n")
|
2013-09-22 10:25:34 +00:00
|
|
|
c = Yajl::Parser.new.parse(process.read_line)
|
2013-09-14 11:16:47 +00:00
|
|
|
|
|
|
|
Cursor.new(c['x'], c['y'], c['visible'])
|
2013-08-04 21:06:56 +00:00
|
|
|
end
|
|
|
|
|
2013-08-06 10:49:54 +00:00
|
|
|
def release
|
2013-09-14 11:16:47 +00:00
|
|
|
process.stop
|
2013-08-06 10:49:54 +00:00
|
|
|
end
|
|
|
|
|
2013-08-04 21:06:56 +00:00
|
|
|
private
|
|
|
|
|
2013-09-14 11:16:47 +00:00
|
|
|
attr_reader :process
|
2013-08-04 21:06:56 +00:00
|
|
|
|
2013-09-14 11:16:47 +00:00
|
|
|
class Process
|
2013-08-29 13:10:46 +00:00
|
|
|
|
2013-09-14 11:16:47 +00:00
|
|
|
def initialize(command)
|
|
|
|
@stdin, @stdout, @thread = Open3.popen2(command)
|
2013-08-21 20:41:31 +00:00
|
|
|
end
|
|
|
|
|
2013-09-14 11:16:47 +00:00
|
|
|
def write(data)
|
2015-05-08 10:14:19 +00:00
|
|
|
check_thread!
|
2013-09-14 11:16:47 +00:00
|
|
|
@stdin.write(data)
|
|
|
|
end
|
2013-08-21 20:41:31 +00:00
|
|
|
|
2013-09-14 11:16:47 +00:00
|
|
|
def read_line
|
2015-05-08 10:14:19 +00:00
|
|
|
check_thread!
|
2013-09-14 11:16:47 +00:00
|
|
|
@stdout.readline.strip
|
|
|
|
end
|
2013-08-21 20:41:31 +00:00
|
|
|
|
2013-09-14 11:16:47 +00:00
|
|
|
def stop
|
|
|
|
@stdin.close
|
2013-08-21 20:41:31 +00:00
|
|
|
end
|
2013-09-14 11:16:47 +00:00
|
|
|
|
2015-05-08 10:14:19 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def check_thread!
|
|
|
|
raise "terminal died, exit code: #{@thread.value.exitstatus}, signaled?: #{@thread.value.signaled?}" unless @thread.alive?
|
|
|
|
end
|
2013-08-21 20:41:31 +00:00
|
|
|
end
|
|
|
|
|
2013-08-04 21:06:56 +00:00
|
|
|
end
|