Skip to content

Commit 3c78c04

Browse files
committed
Added ANSI colored output to ronin-web diff (closes #80).
1 parent 2c95b7e commit 3c78c04

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

lib/ronin/web/cli/commands/diff.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require 'ronin/web/cli/command'
2222
require 'ronin/support/network/http'
2323

24+
require 'command_kit/colors'
2425
require 'nokogiri/diff'
2526

2627
module Ronin
@@ -46,6 +47,8 @@ module Commands
4647
#
4748
class Diff < Command
4849

50+
include CommandKit::Colors
51+
4952
usage '[options] {URL | FILE} {URL | FILE}'
5053

5154
argument :page1, required: true,
@@ -81,12 +84,37 @@ def run(page1,page2)
8184
doc2 = parse_doc(page2)
8285

8386
doc1.diff(doc2) do |change,node|
84-
unless change == ' '
85-
puts "#{change} #{node}"
87+
unless change == ' ' # ignroe unchanged nodes
88+
print_change(change,node)
8689
end
8790
end
8891
end
8992

93+
#
94+
# Prints a change to the document.
95+
#
96+
# @param ["+", "-"] change
97+
# The type of change.
98+
#
99+
# * `+` - indicates an added node.
100+
# * `-` - indicates a removed node.
101+
#
102+
# @param [Nokogiri::HTML::Node, Nokogiri::HTML::Node] node
103+
# The node that was changed.
104+
#
105+
def print_change(change,node)
106+
color = case change
107+
when '+' then colors.method(:green)
108+
when '-' then colors.method(:red)
109+
end
110+
111+
content = node.to_s
112+
113+
content.each_line(chomp: true) do |line|
114+
puts color.call("#{change} #{line}")
115+
end
116+
end
117+
90118
#
91119
# Reads a web page.
92120
#

0 commit comments

Comments
 (0)