Skip to content

Commit

Permalink
Added ANSI colored output to ronin-web diff (closes #80).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Apr 25, 2024
1 parent 2c95b7e commit 3c78c04
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/ronin/web/cli/commands/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require 'ronin/web/cli/command'
require 'ronin/support/network/http'

require 'command_kit/colors'
require 'nokogiri/diff'

module Ronin
Expand All @@ -46,6 +47,8 @@ module Commands
#
class Diff < Command

include CommandKit::Colors

usage '[options] {URL | FILE} {URL | FILE}'

argument :page1, required: true,
Expand Down Expand Up @@ -81,12 +84,37 @@ def run(page1,page2)
doc2 = parse_doc(page2)

doc1.diff(doc2) do |change,node|
unless change == ' '
puts "#{change} #{node}"
unless change == ' ' # ignroe unchanged nodes
print_change(change,node)
end
end
end

#
# Prints a change to the document.
#
# @param ["+", "-"] change
# The type of change.
#
# * `+` - indicates an added node.
# * `-` - indicates a removed node.
#
# @param [Nokogiri::HTML::Node, Nokogiri::HTML::Node] node
# The node that was changed.
#
def print_change(change,node)
color = case change
when '+' then colors.method(:green)
when '-' then colors.method(:red)
end

content = node.to_s

content.each_line(chomp: true) do |line|
puts color.call("#{change} #{line}")
end
end

#
# Reads a web page.
#
Expand Down

0 comments on commit 3c78c04

Please sign in to comment.