| 1 |
doda |
6443 |
#!/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 testClipboard |
| 99 |
|
|
def rwClipboard(data) |
| 100 |
|
|
msgout "������������������������ \"#{data}\" ������������������������" |
| 101 |
|
|
setClipboard data |
| 102 |
|
|
|
| 103 |
|
|
msgout "���������������������������������������������������" |
| 104 |
|
|
cbdata = getClipboard |
| 105 |
|
|
|
| 106 |
|
|
if cbdata == data |
| 107 |
|
|
msgout "������������������������������������������������������������������������������������������������" |
| 108 |
|
|
return :ok |
| 109 |
|
|
elsif cbdata == nil |
| 110 |
|
|
msgout "������������������������������������������������������������������������������������" |
| 111 |
|
|
msgout "������������������������������������������������������������������������������������������" |
| 112 |
|
|
return :timeout |
| 113 |
|
|
else |
| 114 |
|
|
msgout "���������������������������������������������������������������������������������������������" |
| 115 |
|
|
msgout "set: #{data.inspect}" |
| 116 |
|
|
msgout "get: #{cbdata.inspect}" |
| 117 |
|
|
return :error |
| 118 |
|
|
end |
| 119 |
|
|
end |
| 120 |
|
|
|
| 121 |
|
|
check_kcode |
| 122 |
|
|
msgout "������������������������������������������������������������������������" |
| 123 |
|
|
msgout "������������������������������������������������������������������������������" |
| 124 |
|
|
msgout "������������������������������������������������������������������������/������������������������������" |
| 125 |
doda |
6447 |
msgout "���������������������������������������������������������������" |
| 126 |
doda |
6443 |
STDIN.gets |
| 127 |
|
|
|
| 128 |
|
|
rwClipboard "Clipboard Test" |
| 129 |
doda |
6445 |
puts "" |
| 130 |
|
|
rwClipboard "" |
| 131 |
doda |
6443 |
end |
| 132 |
|
|
|
| 133 |
|
|
case ARGV.shift |
| 134 |
|
|
when "--set" |
| 135 |
|
|
setClipboard(ARGV.join(" ")) |
| 136 |
|
|
when "--get" |
| 137 |
|
|
if (cbdata = getClipboard) |
| 138 |
|
|
p cbdata |
| 139 |
|
|
else |
| 140 |
|
|
check_kcode |
| 141 |
|
|
msgout "������������������������������������������������������������������������������������" |
| 142 |
|
|
end |
| 143 |
|
|
when "--test" |
| 144 |
|
|
testClipboard |
| 145 |
|
|
else |
| 146 |
|
|
check_kcode |
| 147 |
|
|
msgout "���������: #$0 [ --set | --get | --test ] [ ��������������� ]" |
| 148 |
doda |
6447 |
msgout " --set ������������������������������������������������������������������������������" |
| 149 |
|
|
msgout " --get ���������������������������������������������" |
| 150 |
|
|
msgout " --test ���������������������������������������������������������������������" |
| 151 |
doda |
6443 |
end |