| 1 |
doda |
6449 |
#!/usr/bin/env ruby |
| 2 |
|
|
# encoding: ASCII-8BIT |
| 3 |
|
|
|
| 4 |
|
|
Encoding.default_external = "ASCII-8BIT" if RUBY_VERSION >= "1.9.0" |
| 5 |
|
|
|
| 6 |
|
|
require 'timeout' |
| 7 |
|
|
require 'kconv' |
| 8 |
|
|
|
| 9 |
|
|
def rawio(t=nil, &blk) |
| 10 |
|
|
saved_mode = nil |
| 11 |
|
|
open("|stty -g") do |stty| |
| 12 |
|
|
saved_mode = stty.gets |
| 13 |
|
|
end |
| 14 |
|
|
begin |
| 15 |
|
|
system("stty raw -echo") |
| 16 |
|
|
if t |
| 17 |
|
|
return Timeout.timeout(t, &blk) |
| 18 |
|
|
else |
| 19 |
|
|
return blk.call |
| 20 |
|
|
end |
| 21 |
|
|
ensure |
| 22 |
|
|
system("stty #{saved_mode}") |
| 23 |
|
|
end |
| 24 |
|
|
end |
| 25 |
|
|
|
| 26 |
|
|
def check_kcode |
| 27 |
|
|
begin |
| 28 |
|
|
rawio(1) do |
| 29 |
|
|
print "\r\xe6\xa4\xa3\xe6\x8e\xa7\e[6n\e[1K\r" |
| 30 |
|
|
buff = "" |
| 31 |
|
|
while c = STDIN.getc |
| 32 |
|
|
buff << c.chr |
| 33 |
|
|
if /(\x9c|\x1b\[)(\d+);(\d+)R/ =~ buff |
| 34 |
|
|
case $3.to_i |
| 35 |
|
|
when 5 |
| 36 |
|
|
$out_code = :toutf8 |
| 37 |
|
|
when 6 |
| 38 |
|
|
$out_code = :toeuc |
| 39 |
|
|
when 7 |
| 40 |
|
|
$out_code = :tosjis |
| 41 |
|
|
end |
| 42 |
|
|
break |
| 43 |
|
|
end |
| 44 |
|
|
end |
| 45 |
|
|
end |
| 46 |
|
|
rescue Timeout::Error |
| 47 |
|
|
$out_code = nil |
| 48 |
|
|
end |
| 49 |
|
|
end |
| 50 |
|
|
|
| 51 |
|
|
def msgout(msg) |
| 52 |
|
|
if $out_code |
| 53 |
|
|
puts msg.to_s.method($out_code).call |
| 54 |
|
|
else |
| 55 |
|
|
puts msg.to_s |
| 56 |
|
|
end |
| 57 |
|
|
end |
| 58 |
|
|
|
| 59 |
|
|
def getClipboard |
| 60 |
|
|
begin |
| 61 |
|
|
return rawio(1) do |
| 62 |
|
|
rdata = "" |
| 63 |
|
|
cbnum = "" |
| 64 |
|
|
|
| 65 |
|
|
print "\e]52;c;?\e\\" |
| 66 |
|
|
|
| 67 |
|
|
while (c = STDIN.getc) |
| 68 |
|
|
break if c.ord == 3 || c.ord == 4 |
| 69 |
|
|
rdata << c |
| 70 |
|
|
if /(\e\]|\x9d)52;([cps0-7]+);/ =~ rdata |
| 71 |
|
|
cbnum = $2 |
| 72 |
|
|
break |
| 73 |
|
|
end |
| 74 |
|
|
end |
| 75 |
|
|
|
| 76 |
|
|
rdata = "" |
| 77 |
|
|
if (cbnum != "") |
| 78 |
|
|
while (c = STDIN.getc) |
| 79 |
|
|
break if c.ord == 3 || c.ord == 4 |
| 80 |
|
|
rdata << c |
| 81 |
|
|
if /(\x9c|\x1b\\)/ =~ rdata |
| 82 |
|
|
return $`.unpack("m")[0] |
| 83 |
|
|
break |
| 84 |
|
|
end |
| 85 |
|
|
end |
| 86 |
|
|
end |
| 87 |
|
|
nil |
| 88 |
|
|
end |
| 89 |
|
|
rescue Timeout::Error |
| 90 |
|
|
nil |
| 91 |
|
|
end |
| 92 |
|
|
end |
| 93 |
|
|
|
| 94 |
|
|
def setClipboard(data) |
| 95 |
|
|
printf "\e]52;c;#{[data].pack("m").chomp}\e\\" |
| 96 |
|
|
end |
| 97 |
|
|
|
| 98 |
|
|
def getBracketedString |
| 99 |
|
|
bracket_type = nil |
| 100 |
|
|
rdata = "" |
| 101 |
|
|
begin |
| 102 |
|
|
return rawio(30) do |
| 103 |
|
|
pdata = nil |
| 104 |
|
|
|
| 105 |
|
|
c = STDIN.getc |
| 106 |
|
|
if c.ord == 3 || c.ord == 4 |
| 107 |
|
|
return [:interrupt, rdata] |
| 108 |
|
|
end |
| 109 |
|
|
rdata << c |
| 110 |
|
|
|
| 111 |
|
|
# ������������������������������������ 1 ������������ |
| 112 |
|
|
Timeout.timeout(1) do |
| 113 |
|
|
while (c = STDIN.getc) |
| 114 |
|
|
if c.ord == 3 || c.ord == 4 |
| 115 |
|
|
return [:interrupt, rdata] |
| 116 |
|
|
end |
| 117 |
|
|
rdata << c |
| 118 |
|
|
if /(?:\e\[|\x9b)20([01])~/ =~ rdata |
| 119 |
|
|
pdata = $` |
| 120 |
|
|
bracket_type = $1.to_i |
| 121 |
|
|
break |
| 122 |
|
|
end |
| 123 |
|
|
end |
| 124 |
|
|
|
| 125 |
|
|
case bracket_type |
| 126 |
|
|
when nil |
| 127 |
|
|
# ���������������� |
| 128 |
|
|
return [:interrupt, rdata] |
| 129 |
|
|
when 0 |
| 130 |
|
|
nil |
| 131 |
|
|
when 1 |
| 132 |
|
|
return [:nostart, pdata] |
| 133 |
|
|
else |
| 134 |
|
|
# �������������� |
| 135 |
|
|
return [:invalid, bracket_type] |
| 136 |
|
|
end |
| 137 |
|
|
|
| 138 |
|
|
rdata = "" |
| 139 |
|
|
while (c = STDIN.getc) |
| 140 |
|
|
if c.ord == 3 || c.ord == 4 |
| 141 |
|
|
return [:interrupt, rdata] |
| 142 |
|
|
end |
| 143 |
|
|
rdata << c |
| 144 |
|
|
if /(\e\[|\x9b)201~/ =~ rdata |
| 145 |
|
|
return [:ok, $`] |
| 146 |
|
|
end |
| 147 |
|
|
end |
| 148 |
|
|
end |
| 149 |
|
|
[:noend, rdata] |
| 150 |
|
|
end |
| 151 |
|
|
rescue Timeout::Error |
| 152 |
|
|
case bracket_type |
| 153 |
|
|
when nil |
| 154 |
|
|
[:timeout, rdata] |
| 155 |
|
|
when 0 |
| 156 |
|
|
[:noend, rdata] |
| 157 |
|
|
when 1 |
| 158 |
|
|
[:nostart, rdata] |
| 159 |
|
|
else |
| 160 |
|
|
return [:invalid, bracket_type] |
| 161 |
|
|
end |
| 162 |
|
|
ensure |
| 163 |
|
|
print "\e[?2004l" |
| 164 |
|
|
end |
| 165 |
|
|
end |
| 166 |
|
|
|
| 167 |
|
|
def testBracketedPaste(msg, str, enableBracket) |
| 168 |
|
|
msgout msg |
| 169 |
|
|
setClipboard str |
| 170 |
|
|
print "\e[?2004h" if enableBracket |
| 171 |
|
|
msgout "������������(������������������������Alt+v��)������������������" |
| 172 |
|
|
result, data = getBracketedString |
| 173 |
|
|
|
| 174 |
|
|
case result |
| 175 |
|
|
when :ok |
| 176 |
|
|
if enableBracket |
| 177 |
|
|
if str == data |
| 178 |
|
|
msgout "����: ��" |
| 179 |
|
|
else |
| 180 |
|
|
msgout "����: �� - ������������������������������������: \"#{data}\"" |
| 181 |
|
|
end |
| 182 |
|
|
else |
| 183 |
|
|
if str == data |
| 184 |
|
|
msgout "����: �� - �������������� Bracket ������������������" |
| 185 |
|
|
else |
| 186 |
|
|
msgout "����: �� - �������������� Bracket ������������������������������������������������������: \"#{data}\"" |
| 187 |
|
|
end |
| 188 |
|
|
end |
| 189 |
|
|
when :interrupt |
| 190 |
|
|
msgout "����: �� - ����������������������" |
| 191 |
|
|
when :invalid |
| 192 |
|
|
msgout "����: �� - ����������������" |
| 193 |
|
|
when :timeout |
| 194 |
|
|
if enableBracket |
| 195 |
|
|
if str == data |
| 196 |
|
|
msgout "����: �� - ������ Bracket ��������������������" |
| 197 |
|
|
else |
| 198 |
|
|
msgout "����: �� - ��������������������������������: \"#{data}\"" |
| 199 |
|
|
end |
| 200 |
|
|
else |
| 201 |
|
|
if str == data |
| 202 |
|
|
msgout "����: ��" |
| 203 |
|
|
else |
| 204 |
|
|
msgout "����: �� - ������������������������������������: \"#{data}\"" |
| 205 |
|
|
end |
| 206 |
|
|
end |
| 207 |
|
|
when :nostart |
| 208 |
|
|
msgout "����: �� - ���� Bracket ���������������� Bracket ����������������������: \"#{data}\"" |
| 209 |
|
|
when :noend |
| 210 |
|
|
msgout "����: �� - ���� Bracket ���������������� Bracket ����������������������������: \"#{data}\"" |
| 211 |
|
|
else |
| 212 |
|
|
msgout "����: �� - ����������������" |
| 213 |
|
|
end |
| 214 |
|
|
puts "" |
| 215 |
|
|
end |
| 216 |
|
|
|
| 217 |
|
|
check_kcode |
| 218 |
|
|
msgout "Bracketed Paste Mode ��������������������" |
| 219 |
|
|
msgout "����������������������������������������������������" |
| 220 |
|
|
msgout "������������������������������������������������������������������/��������������������" |
| 221 |
|
|
msgout "������������������������������������������" |
| 222 |
|
|
STDIN.gets |
| 223 |
|
|
|
| 224 |
|
|
testBracketedPaste("������1: ����", "Bracketed Paste Test", true) |
| 225 |
|
|
testBracketedPaste("������2: ��������", "", true) |
| 226 |
|
|
testBracketedPaste("������3: ��������", "Non-Bracketed Mode Test", false) |