63 lines
1.4 KiB
Ruby
63 lines
1.4 KiB
Ruby
#!/usr/bin/env ruby
|
|
|
|
require 'socket'
|
|
require 'io/console'
|
|
require 'optparse'
|
|
|
|
options = {
|
|
:port => 7570,
|
|
:host => 'localhost'
|
|
}
|
|
OptionParser.new do |opt|
|
|
opt.on('-pPORT', '--port PORT', Integer,
|
|
'Port to connect to (defaults to 7570)') do |o|
|
|
options[:port] = o
|
|
end
|
|
opt.on('-HHOST', '--host HOST', String,
|
|
'Host to connect to (defaults to `localhost`)') do |o|
|
|
options[:host] = o
|
|
end
|
|
opt.on('-h', '--help', 'Get help with this command') do
|
|
puts opt
|
|
exit
|
|
end
|
|
end.parse!(into: options)
|
|
|
|
HOST = options[:host]
|
|
PORT = options[:port]
|
|
#PORT = 6789
|
|
VERSION = 0.01
|
|
|
|
socket = TCPSocket.new(HOST, PORT)
|
|
|
|
puts "PORT: #{PORT}"
|
|
puts "HOST: #{HOST}"
|
|
|
|
begin
|
|
print "\x1b[?1049h\x1b[2J\x1b[3J" # Open and clear alternate buffer
|
|
|
|
#reader = Thread.new do
|
|
Thread.new do
|
|
loop do
|
|
msg = socket.gets
|
|
exit 0 unless msg
|
|
ver, msg = msg.split('|',2)
|
|
next unless ver.to_f <= VERSION
|
|
next unless msg != nil
|
|
# In message version 0.01 and lower,
|
|
# all messages are in an assumed format...
|
|
row,col,chars = msg.split('|',3)
|
|
print "\x1b[#{row};#{col}H#{chars}\x1b[A"
|
|
end
|
|
end
|
|
|
|
while (char = STDIN.noecho(&:getch))
|
|
exit unless char != "\x03" # ^C
|
|
exit unless char != "\x04" # ^D
|
|
next unless char.ord >= 0x20 # Non printing
|
|
next unless char.ord != 0x7F # DEL (Non print)
|
|
socket.puts "#{VERSION}|#{char}"
|
|
end
|
|
ensure
|
|
print "\x1b[?1049l" # Back to regular buffer
|
|
end
|