| |   |
| 6 | 6 | AUTO_LOAD = { |
| 7 | 7 | :Launcher => 'launcher.rb', |
| 8 | 8 | :RemoteDebugger => 'remote_debugger.rb', |
| 9 | | :Inspect => 'inspect.rb' |
| 9 | :Inspect => 'inspect.rb', |
| 10 | :PrettyAlign => 'pretty_align.rb' |
| 10 | 11 | } |
| 11 | 12 | |
| 12 | 13 | def self.const_missing(name) |
| toggle raw diff |
--- a/Support/lib/ruby_amp.rb
+++ b/Support/lib/ruby_amp.rb
@@ -6,7 +6,8 @@ module RubyAMP
AUTO_LOAD = {
:Launcher => 'launcher.rb',
:RemoteDebugger => 'remote_debugger.rb',
- :Inspect => 'inspect.rb'
+ :Inspect => 'inspect.rb',
+ :PrettyAlign => 'pretty_align.rb'
}
def self.const_missing(name) |
| |   |
| 1 | module RubyAMP::PrettyAlign |
| 2 | def pretty_align(separator, input) |
| 3 | |
| 4 | if separator |
| 5 | separator_regexp = Regexp.new(separator.strip.scan(/^\/?(.+?)\/?\w*$/).to_s) |
| 6 | |
| 7 | lines = [] |
| 8 | |
| 9 | input.split(/\n/, -1).each do |line| |
| 10 | if line =~ separator_regexp |
| 11 | separator = $&.strip |
| 12 | left, right = line.split(separator_regexp) |
| 13 | lines << [left.to_s.rstrip, separator, right.to_s.strip] |
| 14 | else |
| 15 | lines << [line] |
| 16 | end |
| 17 | end |
| 18 | |
| 19 | max_left = lines.max {|a,b| !a[2] ? -1 : a[0].length <=> b[0].length }[0].length |
| 20 | max_separator = lines.max {|a,b| !a[1] ? -1 : a[1].to_s.length <=> b[1].to_s.length }[1].length |
| 21 | |
| 22 | output = [] |
| 23 | lines.each do |left, separator, right| |
| 24 | if right |
| 25 | output << format("%-#{max_left}s %-#{max_separator}s %s", left, separator, right) |
| 26 | else |
| 27 | output << left |
| 28 | end |
| 29 | end |
| 30 | |
| 31 | output.join("\n") |
| 32 | else |
| 33 | input |
| 34 | end |
| 35 | end |
| 36 | end |
| toggle raw diff |
--- /dev/null
+++ b/Support/lib/ruby_amp/pretty_align.rb
@@ -0,0 +1,36 @@
+module RubyAMP::PrettyAlign
+ def pretty_align(separator, input)
+
+ if separator
+ separator_regexp = Regexp.new(separator.strip.scan(/^\/?(.+?)\/?\w*$/).to_s)
+
+ lines = []
+
+ input.split(/\n/, -1).each do |line|
+ if line =~ separator_regexp
+ separator = $&.strip
+ left, right = line.split(separator_regexp)
+ lines << [left.to_s.rstrip, separator, right.to_s.strip]
+ else
+ lines << [line]
+ end
+ end
+
+ max_left = lines.max {|a,b| !a[2] ? -1 : a[0].length <=> b[0].length }[0].length
+ max_separator = lines.max {|a,b| !a[1] ? -1 : a[1].to_s.length <=> b[1].to_s.length }[1].length
+
+ output = []
+ lines.each do |left, separator, right|
+ if right
+ output << format("%-#{max_left}s %-#{max_separator}s %s", left, separator, right)
+ else
+ output << left
+ end
+ end
+
+ output.join("\n")
+ else
+ input
+ end
+ end
+end
\ No newline at end of file |
| |   |
| 1 | require File.dirname(__FILE__) + "/../../spec_helper.rb" |
| 2 | |
| 3 | describe RubyAMP::PrettyAlign do |
| 4 | it "should align at a given text sequence" do |
| 5 | input = <<EOF |
| 6 | when "this string" then value |
| 7 | when "other string" then value |
| 8 | EOF |
| 9 | expected = <<EOF |
| 10 | when "this string" then value |
| 11 | when "other string" then value |
| 12 | EOF |
| 13 | pretty_align("then", input).should == expected |
| 14 | end |
| 15 | end |
| toggle raw diff |
--- /dev/null
+++ b/Support/spec/lib/pretty_align/pretty_align_spec.rb
@@ -0,0 +1,15 @@
+require File.dirname(__FILE__) + "/../../spec_helper.rb"
+
+describe RubyAMP::PrettyAlign do
+ it "should align at a given text sequence" do
+ input = <<EOF
+when "this string" then value
+when "other string" then value
+EOF
+ expected = <<EOF
+when "this string" then value
+when "other string" then value
+EOF
+ pretty_align("then", input).should == expected
+ end
+end
\ No newline at end of file |