lib/daddy/cucumber/diff.rb in daddy-0.1.9 vs lib/daddy/cucumber/diff.rb in daddy-0.1.11

- old
+ new

@@ -8,11 +8,11 @@ options[:as] ||= 'edit' git = Daddy::Git.new a = File.read(file).gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;') b = git.show_previous(file, true).gsub(/[<>]/, '<' => '&lt;', '>' => '&gt;') - diff = format_diff(Differ.diff(a, b)) + diff = format_diff(Differ.diff(a, b), options) show_filename(file, options) puts "<pre>#{diff}</pre>" end @@ -36,24 +36,51 @@ end private def show_filename(filename, options = {}) - if options[:as] == 'new' - puts "<span class=\"new\">[新規作成] #{filename}</span>" - elsif options[:as] == 'auto' - puts "<span class=\"auto\">[自動生成] #{filename}</span>" - elsif options[:as] == 'edit' - puts "<span class=\"edit\">[編集] #{filename}</span>" - else - puts filename + labels = [] + if options[:as].is_a?(String) or options[:as].is_a?(Symbol) + labels << options[:as] + elsif options[:as].is_a?(Array) + labels += options[:as] end + + html = "<span class=\"#{labels.join(' ')}\">" + labels.each do |label| + case label.to_s + when 'auto' + html << '[自動生成] ' + when 'new' + html << '[新規作成] ' + when 'edit' + html << '[編集] ' + end + end + + html << filename + html << "</span>" + + puts html end - def format_diff(diff) + def format_diff(diff, options = {}) lines = [] diff_lines = diff.to_s.split("\n") + + if options[:from] and options[:to] + from = options[:from] - 1 + to = options[:to] - 1 + diff_lines = diff_lines[from..to] + elsif options[:from] + from = options[:from] - 1 + diff_lines = diff_lines[from..-1] + elsif options[:to] + to = options[:to] - 1 + diff_lines = diff_lines[0..to] + end + diff_lines.each_with_index do |line, i| if line.index('</del><ins class="differ">') split = line.split('</del><ins class="differ">') lines << split[0] if split[1].start_with?('"')