Skip to content

Instantly share code, notes, and snippets.

@no6v
Created April 15, 2013 17:51
Show Gist options
  • Save no6v/5389943 to your computer and use it in GitHub Desktop.
Save no6v/5389943 to your computer and use it in GitHub Desktop.
# encoding: UTF-8
# :verical for earthquake.gem: https://gist.github.com/no6v/5187616
Earthquake.once do
begin
require "tapp"
require "tapp/object_extension"
rescue LoadError
warn "`gem install tapp` to use :vertical_debug".c(:notice)
next
end
Module.new do |tapp_wait|
::Tapp::ObjectExtension.instance_methods.each do |name|
define_method(name) do |label, *args, &block|
print label.to_s.c(:event)
puts "(input EOF to continue)".c(:info)
super(*args, &block).tap{STDIN.gets}
end
end
::Object.__send__(:prepend, tapp_wait)
end
end
Earthquake.init do
punctuation = "、。,."
h = "ー「」→↑←↓=…"
v = "|¬∟↓→↑←॥:"
hv = (h + v)
vh = (v + h)
sep = "||--"
command :vertical_debug do
puts "[input EOF (e.g. Ctrl+D) at the last]".c(:info)
text, _, signature = STDIN.gets(nil).rpartition(sep).reject(&:empty?).tapp(:text_signature)
lines = text.tr(hv, vh).tapp(:tr).each_line.map{|line|
line.chomp.split(/([#{punctuation}]+)/o).tapp(:split)
}.flat_map{|line|
line.each_slice(2).with_object(["", ""]){|(main, punct), (punct_line, main_line)|
punct ||= " "
punct_line << (" " * main.size.pred + punct).tapp(:punct_line)
main_line << (main + " " * punct.size.pred).tapp(:main_line)
}.tapp(:flat_map)
}.each{|line| line.chomp!(" ")}.tapp(:lines)
max = lines.map(&:size).max.tapp(:max)
status = lines.map{|line|
line.chars.fill(" ", line.size...max).tapp(:fill)
}.transpose.tapp(:transpose).map{|line| line.reverse.join.tapp(:reverse_join)}.join("\n")
status.gsub!(/ +$/, "")
status += signature.chomp if signature
confirm(status)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment