Add colors to mewny
This commit is contained in:
parent
41bb37b3c6
commit
131fef7866
1 changed files with 72 additions and 25 deletions
97
mewny.rb
97
mewny.rb
|
|
@ -31,7 +31,7 @@ OptionParser.new do |opt|
|
||||||
options[:server] = false
|
options[:server] = false
|
||||||
end
|
end
|
||||||
opt.on('-HHOST', '--host HOST', String,
|
opt.on('-HHOST', '--host HOST', String,
|
||||||
'(client only) Host to connect to (defaults to `localhost`)') do |o|
|
'(client only) Host to connect to (defaults to `localhost`)') do |o|
|
||||||
options[:host] = o
|
options[:host] = o
|
||||||
end
|
end
|
||||||
opt.on('-rROWS', '--rows ROWS', Integer,
|
opt.on('-rROWS', '--rows ROWS', Integer,
|
||||||
|
|
@ -48,9 +48,9 @@ OptionParser.new do |opt|
|
||||||
# X -- eXposed
|
# X -- eXposed
|
||||||
'(server only) Serve publicly, rather than privately. Please ONLY add this option if you entierly know what you\'re doing and have permission from/are the server owner. Say please to expose.'
|
'(server only) Serve publicly, rather than privately. Please ONLY add this option if you entierly know what you\'re doing and have permission from/are the server owner. Say please to expose.'
|
||||||
) do |o|
|
) do |o|
|
||||||
if o == "please"
|
if o == "please"
|
||||||
options[:server_host] = PUBLIC_HOST
|
options[:server_host] = PUBLIC_HOST
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
opt.on('-x', '--private',
|
opt.on('-x', '--private',
|
||||||
# x -- not visible
|
# x -- not visible
|
||||||
|
|
@ -74,11 +74,20 @@ VERSION = {
|
||||||
:SRV_TXT => '0.01',
|
:SRV_TXT => '0.01',
|
||||||
:CLN_TXT => '0.01',
|
:CLN_TXT => '0.01',
|
||||||
:CLN_ARO => '0.02',
|
:CLN_ARO => '0.02',
|
||||||
|
:SRV_CO8 => '0.30', # 8-color
|
||||||
|
:CLN_CO8 => '0.30', # 8-color
|
||||||
}
|
}
|
||||||
|
|
||||||
def do_server
|
def do_server
|
||||||
server = TCPServer.new(SRV_HOST, PORT)
|
server = TCPServer.new(SRV_HOST, PORT)
|
||||||
sockets = [server]
|
sockets = [server]
|
||||||
|
sockats = {
|
||||||
|
server => {
|
||||||
|
:color => {
|
||||||
|
:co8 => 7
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
puts "PORT: #{PORT}"
|
puts "PORT: #{PORT}"
|
||||||
puts "GRID SIZE: #{ROWS}r#{COLS}c"
|
puts "GRID SIZE: #{ROWS}r#{COLS}c"
|
||||||
|
|
@ -87,14 +96,20 @@ def do_server
|
||||||
|
|
||||||
pre = "#{ROWS}r#{COLS}c >>> "
|
pre = "#{ROWS}r#{COLS}c >>> "
|
||||||
buffer = pre.ljust(ROWS*COLS)[0...ROWS*COLS]
|
buffer = pre.ljust(ROWS*COLS)[0...ROWS*COLS]
|
||||||
|
cobuf = Array.new(ROWS*COLS, {
|
||||||
|
:co8 => 7
|
||||||
|
})
|
||||||
windex = pre.length # Wipe your buffer clean with your write index
|
windex = pre.length # Wipe your buffer clean with your write index
|
||||||
windex %= buffer.length
|
windex = windex%buffer.length
|
||||||
motions = []
|
motions = []
|
||||||
|
|
||||||
def new_buf_wi_out(buf, wi, newtext, motions: [])
|
def new_buf_cob_wi_out(buf, cob, wi, newtext, newcolors, motions: [])
|
||||||
buf = buf.dup
|
buf = buf.dup
|
||||||
|
cob = cob.dup
|
||||||
out = ''
|
out = ''
|
||||||
nti = 0 # New Text Index
|
nti = 0 # New Text Index
|
||||||
|
wco = {} #Writing Color
|
||||||
|
cur = motions.length > 0 #Update cursor at end
|
||||||
while (nti < newtext.length) || (motions.length > 0)
|
while (nti < newtext.length) || (motions.length > 0)
|
||||||
if motions.length > 0 && nti >= motions[0][:nti]
|
if motions.length > 0 && nti >= motions[0][:nti]
|
||||||
case motions[0][:dir]
|
case motions[0][:dir]
|
||||||
|
|
@ -107,40 +122,61 @@ def do_server
|
||||||
when 'r'
|
when 'r'
|
||||||
wi += 1
|
wi += 1
|
||||||
end
|
end
|
||||||
wi %= ROWS*COLS
|
wi = wi%(ROWS*COLS)
|
||||||
motions.shift # remove first motion
|
motions.shift # remove first motion
|
||||||
end
|
end
|
||||||
|
next if nti >= newtext.length
|
||||||
|
if newcolors[nti] != wco
|
||||||
|
wco = newcolors[nti]
|
||||||
|
if wco.has_key? :co8
|
||||||
|
out << "#{VERSION[:SRV_CO8]}|#{wco[:co8]}\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
out << "#{VERSION[:SRV_TXT]}|"
|
out << "#{VERSION[:SRV_TXT]}|"
|
||||||
out << ((wi/COLS)+1).to_s + '|'
|
out << ((wi/COLS)+1).to_s + '|'
|
||||||
out << ((wi%COLS)+1).to_s + '|'
|
out << ((wi%COLS)+1).to_s + '|'
|
||||||
next if nti >= newtext.length
|
|
||||||
loop do
|
loop do
|
||||||
out << newtext[nti]
|
out << newtext[nti]
|
||||||
buf[wi] = newtext[nti]
|
buf[wi] = newtext[nti]
|
||||||
|
cob[wi] = wco
|
||||||
wi += 1
|
wi += 1
|
||||||
nti += 1
|
nti += 1
|
||||||
|
break if newcolors[nti] != wco
|
||||||
break if motions.length > 0 && nti >= motions[0][:nti]
|
break if motions.length > 0 && nti >= motions[0][:nti]
|
||||||
break if nti >= newtext.length
|
break if nti >= newtext.length
|
||||||
break if wi%COLS == 0
|
break if wi%COLS == 0
|
||||||
end
|
end
|
||||||
wi %= ROWS*COLS
|
wi = wi%(ROWS*COLS)
|
||||||
out << "\n"
|
out << "\n"
|
||||||
end
|
end
|
||||||
[buf, wi, out]
|
if cur
|
||||||
|
out << "#{VERSION[:SRV_TXT]}|"
|
||||||
|
out << ((wi/COLS)+1).to_s + '|'
|
||||||
|
out << ((wi%COLS)+1).to_s + "|\n"
|
||||||
|
end
|
||||||
|
[buf, cob, wi, out]
|
||||||
end
|
end
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
ready, = IO.select(sockets)
|
ready, = IO.select(sockets)
|
||||||
newtext = ''
|
newtext = ''
|
||||||
|
newcolors = []
|
||||||
|
|
||||||
ready.each do |sck|
|
ready.each do |sck|
|
||||||
if sck == server
|
if sck == server
|
||||||
client = server.accept
|
client = server.accept
|
||||||
|
sockats[client] = {
|
||||||
|
:color => {
|
||||||
|
:co8 => 7
|
||||||
|
}
|
||||||
|
}
|
||||||
sockets << client
|
sockets << client
|
||||||
puts "Connection: #{client.peeraddr}"
|
puts "Connection: #{client.peeraddr}"
|
||||||
client.puts new_buf_wi_out(buffer,0,buffer)[2]
|
client.puts new_buf_cob_wi_out(buffer,cobuf,0,buffer,cobuf)[3]
|
||||||
if buffer.length > 1
|
if buffer.length > 1
|
||||||
client.puts new_buf_wi_out(buffer,windex-1,buffer[windex-1])[2]
|
client.puts new_buf_cob_wi_out(
|
||||||
|
buffer,cobuf,windex-1,
|
||||||
|
buffer[windex-1],[cobuf[windex-1]])[3]
|
||||||
end
|
end
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
@ -150,15 +186,18 @@ def do_server
|
||||||
puts "Disconnect: #{sck.peeraddr}"
|
puts "Disconnect: #{sck.peeraddr}"
|
||||||
sck.close
|
sck.close
|
||||||
sockets.delete(sck)
|
sockets.delete(sck)
|
||||||
|
sockats.delete(sck)
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
ver, msg = msg.chomp.split('|',2)
|
ver, msg = msg.chomp.split('|',2)
|
||||||
|
next unless msg != nil
|
||||||
case ver
|
case ver
|
||||||
when VERSION[:CLN_TXT]
|
when VERSION[:CLN_TXT]
|
||||||
next unless msg != nil
|
|
||||||
newtext << clean_chars(msg)
|
newtext << clean_chars(msg)
|
||||||
|
msg.length.times do
|
||||||
|
newcolors << sockats[sck][:color].dup
|
||||||
|
end
|
||||||
when VERSION[:CLN_ARO]
|
when VERSION[:CLN_ARO]
|
||||||
next unless msg != nil
|
|
||||||
dir = 'l'
|
dir = 'l'
|
||||||
case msg
|
case msg
|
||||||
when 'A'
|
when 'A'
|
||||||
|
|
@ -174,15 +213,17 @@ def do_server
|
||||||
end
|
end
|
||||||
motions << {
|
motions << {
|
||||||
:nti => newtext.length - 1,
|
:nti => newtext.length - 1,
|
||||||
:dir => dir
|
:dir => dir,
|
||||||
}
|
}
|
||||||
|
when VERSION[:CLN_CO8]
|
||||||
|
sockats[sck][:color][:co8] = msg.to_i.clamp(0,7)
|
||||||
end
|
end
|
||||||
rescue Errno::ECONNRESET, Errno::EPIPE
|
rescue Errno::ECONNRESET, Errno::EPIPE
|
||||||
puts "Disconnect (err): #{sck.peeraddr}"
|
puts "Disconnect (err): #{sck.peeraddr}"
|
||||||
sck.close
|
sck.close
|
||||||
sockets.delete(sck)
|
sockets.delete(sck)
|
||||||
end
|
end
|
||||||
buffer, windex, out = new_buf_wi_out(buffer,windex,newtext,motions:motions)
|
buffer, cobuf, windex, out = new_buf_cob_wi_out(buffer,cobuf,windex,newtext,newcolors,motions:motions)
|
||||||
motions = []
|
motions = []
|
||||||
clients = sockets - [server]
|
clients = sockets - [server]
|
||||||
clients.each do |c|
|
clients.each do |c|
|
||||||
|
|
@ -223,6 +264,7 @@ def do_client
|
||||||
|
|
||||||
begin
|
begin
|
||||||
print "\x1b[?1049h\x1b[2J\x1b[3J" # Open and clear alternate buffer
|
print "\x1b[?1049h\x1b[2J\x1b[3J" # Open and clear alternate buffer
|
||||||
|
print "\x1b[m" # Reset color
|
||||||
|
|
||||||
#reader = Thread.new do
|
#reader = Thread.new do
|
||||||
Thread.new do
|
Thread.new do
|
||||||
|
|
@ -230,17 +272,21 @@ def do_client
|
||||||
msg = socket.gets
|
msg = socket.gets
|
||||||
exit 0 unless msg
|
exit 0 unless msg
|
||||||
ver, msg = msg.split('|',2)
|
ver, msg = msg.split('|',2)
|
||||||
next unless ver == VERSION[:SRV_TXT]
|
|
||||||
next unless msg != nil
|
next unless msg != nil
|
||||||
# In message version 0.01 and lower,
|
case ver
|
||||||
# all messages are in an assumed format...
|
when VERSION[:SRV_TXT]
|
||||||
row,col,chars = msg.split('|',3)
|
row,col,chars = msg.split('|',3)
|
||||||
print "\x1b[#{row};#{col}H#{chars.chomp}"
|
print "\x1b[#{row};#{col}H#{chars.chomp}"
|
||||||
|
when VERSION[:SRV_CO8]
|
||||||
|
next if ENV["TERM"].downcase == "dumb"
|
||||||
|
print "\x1b[3#{msg.to_i.clamp(0,7)}m"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
socket.puts "#{VERSION[:CLN_CO8]}|#{rand(1..7)}"
|
||||||
while (char = STDIN.noecho(&:getch))
|
while (char = STDIN.noecho(&:getch))
|
||||||
exit unless char != "\x04" # ^D
|
exit if char == "\x04" # ^D
|
||||||
if char == "\x1b" # Detect arrows
|
if char == "\x1b" # Detect arrows
|
||||||
char2 = STDIN.noecho(&:getch)
|
char2 = STDIN.noecho(&:getch)
|
||||||
if /[A-D]/.match? (char3 = STDIN.noecho(&:getch).upcase)
|
if /[A-D]/.match? (char3 = STDIN.noecho(&:getch).upcase)
|
||||||
|
|
@ -251,12 +297,13 @@ def do_client
|
||||||
end
|
end
|
||||||
elsif (char == "\x08" || # backspace
|
elsif (char == "\x08" || # backspace
|
||||||
char == "\x7F" ) # delete
|
char == "\x7F" ) # delete
|
||||||
socket.puts "#{VERSION[:CLN_ARO]}|D"
|
socket.puts "#{VERSION[:CLN_ARO]}|D"
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
socket.puts "#{VERSION[:CLN_TXT]}|#{clean_chars(char)}"
|
socket.puts "#{VERSION[:CLN_TXT]}|#{clean_chars(char)}"
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
|
print "\x1b[m" # Reset color
|
||||||
print "\x1b[?1049l" # Back to regular buffer
|
print "\x1b[?1049l" # Back to regular buffer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue