Commit 6452a09ed990a185c4527eade31b2e8a447f1ee8

added spec for pretty-align

Commit diff

Support/lib/ruby_amp.rb

 
66 AUTO_LOAD = {
77 :Launcher => 'launcher.rb',
88 :RemoteDebugger => 'remote_debugger.rb',
9 :Inspect => 'inspect.rb'
9 :Inspect => 'inspect.rb',
10 :PrettyAlign => 'pretty_align.rb'
1011 }
1112
1213 def self.const_missing(name)
toggle raw diff

Support/lib/ruby_amp/pretty_align.rb

 
1module 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
36end
toggle raw diff

Support/spec/lib/pretty_align/pretty_align_spec.rb

 
1require File.dirname(__FILE__) + "/../../spec_helper.rb"
2
3describe RubyAMP::PrettyAlign do
4 it "should align at a given text sequence" do
5 input = <<EOF
6when "this string" then value
7when "other string" then value
8EOF
9 expected = <<EOF
10when "this string" then value
11when "other string" then value
12EOF
13 pretty_align("then", input).should == expected
14 end
15end
toggle raw diff

Support/spec/spec_helper.rb

 
1require File.dirname(__FILE__) + "/../lib/ruby_amp.rb"
toggle raw diff