Last active
August 4, 2017 18:56
-
-
Save ttdoda/ed7423dfb67acd87f463 to your computer and use it in GitHub Desktop.
あぁ^~端末がぴょんぴょんするんじゃぁ^~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# License: CC0 | |
# | |
gravity = 3 | |
require 'timeout' | |
include Math | |
$stdout.sync = 1 | |
def getCSIstr(t = 5) | |
resp = "" | |
Timeout.timeout(t) { | |
while c = $stdin.getc | |
case c.ord | |
when 0x1b | |
c = $stdin.getc | |
break if c.chr == "[" | |
$stdin.ungetc c | |
when 0x9c | |
break | |
when 0..0x1f | |
raise | |
end | |
end | |
while c = $stdin.getc | |
case c.ord | |
when 0x20..0x3f | |
resp << c.chr | |
when 0x40..0x7e | |
resp << c.chr | |
break | |
else | |
raise | |
end | |
end | |
} | |
resp | |
end | |
def moveTo(x, y) | |
print "\e[3;#{x.to_i};#{y.to_i}t" | |
end | |
def resizeTerm(h, w) | |
print "\e[8;#{h};#{w}t" | |
end | |
maxX = 0 | |
maxY = 0 | |
termWidth = -1 | |
termHeight = -1 | |
origX = -1 | |
origY = -1 | |
saved_mode = `stty -g` | |
begin | |
system("stty raw -echo") | |
print "\e[18t" | |
raise unless /(\d+)\;(\d+);(\d+)t/ =~ getCSIstr && $1 == "8" | |
termHeight = $2.to_i | |
termWidth = $3.to_i | |
print "\e[13t" | |
raise unless /(\d+)\;(\d+);(\d+)t/ =~ getCSIstr && $1 == "3" | |
origX = $2.to_i | |
origY = $3.to_i | |
resizeTerm 25, 81 | |
print "\e[14t" | |
raise unless /(\d+)\;(\d+);(\d+)t/ =~ getCSIstr && $1 == "4" | |
tmpHeight = $2.to_i | |
tmpWidth = $3.to_i | |
resizeTerm 24, 80 | |
print "\e[14t" | |
raise unless /(\d+)\;(\d+);(\d+)t/ =~ getCSIstr && $1 == "4" | |
winHeight = $2.to_i | |
winWidth = $3.to_i | |
cellHeight = tmpHeight - winHeight | |
cellWidth = tmpWidth - winWidth | |
print "\e[19t" | |
raise unless /(\d+)\;(\d+);(\d+)t/ =~ getCSIstr && $1 == "9" | |
rootHeight = $2.to_i * cellHeight | |
rootWidth = $3.to_i * cellWidth | |
maxY = rootHeight - winHeight | |
maxX = rootWidth - winWidth | |
rescue Timeout::Error, RuntimeError | |
puts "not supported terminal" | |
resizeTerm(termHeight, termWidth) if termHeight > 0 && termWidth > 0 | |
moveTo(origX, origY) if origX >= 0 && origY >= 0 | |
exit 1 | |
ensure | |
system("stty #{saved_mode}") | |
end | |
unitV = sqrt(maxY * gravity * 2) | |
dirH = maxX / 150.0 | |
dirV = 0 | |
x = origX | |
y = origY | |
begin | |
while true | |
x += dirH | |
if x < 0 | |
x = 0 | |
dirH = -dirH | |
elsif x > maxX | |
x = maxX | |
dirH = -dirH | |
end | |
y += dirV | |
if y > maxY | |
y = maxY | |
dirV = -unitV | |
elsif y < 0 | |
y = 0 | |
end | |
dirV += gravity | |
moveTo x, y | |
sleep 0.02 | |
end | |
rescue Interrupt | |
exit 0 | |
ensure | |
resizeTerm(termHeight, termWidth) | |
moveTo(origX, origY) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment