Create a gist now

Instantly share code, notes, and snippets.

@cxx /3moji.rb
Created May 19, 2014

What would you like to do?
SUPER IMPORTANT 3文字
#!/usr/bin/env ruby
require 'optparse'
fg = "\u{2b1c}"
bg = "\u{2139}"
opt = OptionParser.new
opt.on("-f CHAR") {|v| fg = v[0] }
opt.on("-b CHAR") {|v| bg = v[0] }
opt.parse!(ARGV)
max_chars = 140
max_cols = 17
bdf_dir = File.dirname(__FILE__)
text = ARGV.join(" ").encode("Shift_JIS")
bdf_files =
case text.bytesize
when 1, 2 then ["mplus_j10r-jisx0201.bdf", "mplus_j10r.bdf"]
when 3, 4 then ["misaki_4x8_jisx0201.bdf", "misaki_gothic.bdf"]
when 5, 6 then ["3x8.bdf", "k6x8.bdf"]
else fail
end
bdf = bdf_files.map {|f| IO.read("#{bdf_dir}/#{f}") }.join("\n")
bits = text.chars.map do |c|
case c.bytesize
when 1
index = c.codepoints.first
when 2
lead, trail = c.encode("ISO-2022-JP").bytes[3,2]
index = (lead << 8) + trail
end
pos = (/^ENCODING #{index}$/ =~ bdf)
width = /BBX (\d+)/.match(bdf, pos)[1].to_i
/BITMAP\n(.*?)ENDCHAR/m.match(bdf, pos)[1].lines.map do |row|
nb = row.strip.size * 4
sprintf("%0*b", width, row.hex >> (nb-width))
end
end
result_a = bits.first.each_index.map do |i|
bits.map {|a| a[i] }.join[0,max_cols]
end
ncols, nrows = result_a.first.size, result_a.size
nchars = (ncols+1)*nrows - 1
result_a.pop if nchars > max_chars
if ncols < max_cols and nchars+nrows <= max_chars
result_a.map! {|row| "0#{row}" }
end
result = result_a.join("\n").tr("01", bg+fg)
puts result
begin
require 'launchy'
require 'uri'
query = URI.encode_www_form({"text" => result})
Launchy.open("https://twitter.com/share?#{query}")
rescue LoadError
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment