Use terminal binary as a base for Terminal
parent
e537c1b78a
commit
0e2c1784c6
@ -1,67 +1,59 @@
|
|||||||
|
require 'open3'
|
||||||
|
|
||||||
class Terminal
|
class Terminal
|
||||||
|
|
||||||
|
BINARY_PATH = "/home/kill/code/ascii.io-converter/terminal"
|
||||||
|
|
||||||
def initialize(width, height)
|
def initialize(width, height)
|
||||||
@screen = TSM::Screen.new(width, height)
|
@process = Process.new("#{BINARY_PATH} #{width} #{height}")
|
||||||
@vte = TSM::Vte.new(@screen)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def feed(data)
|
def feed(data)
|
||||||
vte.input(data)
|
process.write("d\n#{data.bytesize}\n#{data}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def snapshot
|
def snapshot
|
||||||
lines = []
|
process.write("p\n")
|
||||||
|
lines = Oj.load(process.read_line)
|
||||||
screen.draw do |x, y, char, screen_attribute|
|
|
||||||
assign_cell(lines, x, y, char, screen_attribute)
|
|
||||||
end
|
|
||||||
|
|
||||||
Snapshot.build(lines)
|
Snapshot.build(lines)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cursor
|
def cursor
|
||||||
Cursor.new(screen.cursor_x, screen.cursor_y, screen.cursor_visible?)
|
process.write("c\n")
|
||||||
|
c = Oj.load(process.read_line)
|
||||||
|
|
||||||
|
Cursor.new(c['x'], c['y'], c['visible'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def release
|
def release
|
||||||
screen.release
|
process.stop
|
||||||
vte.release
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_reader :screen, :vte
|
attr_reader :process
|
||||||
|
|
||||||
def assign_cell(lines, x, y, char, screen_attribute)
|
class Process
|
||||||
line = lines[y] ||= []
|
|
||||||
line[x] = [sanitize_char(char), attributes_hash(screen_attribute)]
|
|
||||||
end
|
|
||||||
|
|
||||||
def sanitize_char(char)
|
def initialize(command)
|
||||||
char.
|
@stdin, @stdout, @thread = Open3.popen2(command)
|
||||||
encode('UTF-16', :invalid => :replace, :undef => :replace,
|
|
||||||
:replace => "\001").
|
|
||||||
encode('UTF-8').gsub(/\001+/, '?').
|
|
||||||
first
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes_hash(screen_attribute)
|
def write(data)
|
||||||
attrs = {}
|
raise "terminal died" unless @thread.alive?
|
||||||
|
@stdin.write(data)
|
||||||
[:fg, :bg, :bold?, :underline?, :inverse?, :blink?].each do |name|
|
|
||||||
assign_attr(attrs, screen_attribute, name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attrs
|
def read_line
|
||||||
|
raise "terminal died" unless @thread.alive?
|
||||||
|
@stdout.readline.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
def assign_attr(attrs, screen_attribute, name)
|
def stop
|
||||||
value = screen_attribute.public_send(name)
|
@stdin.close
|
||||||
|
|
||||||
if value
|
|
||||||
key = name.to_s.sub('?', '').to_sym
|
|
||||||
attrs[key] = value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
Oj.default_options = { mode: :strict }
|
Loading…
Reference in New Issue