From 3c78c04fcf9e734451c7075945a8e5b73d14452e Mon Sep 17 00:00:00 2001 From: Postmodern Date: Wed, 24 Apr 2024 18:43:15 -0700 Subject: [PATCH] Added ANSI colored output to `ronin-web diff` (closes #80). --- lib/ronin/web/cli/commands/diff.rb | 32 ++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/ronin/web/cli/commands/diff.rb b/lib/ronin/web/cli/commands/diff.rb index de72d792..ab16c64e 100644 --- a/lib/ronin/web/cli/commands/diff.rb +++ b/lib/ronin/web/cli/commands/diff.rb @@ -21,6 +21,7 @@ require 'ronin/web/cli/command' require 'ronin/support/network/http' +require 'command_kit/colors' require 'nokogiri/diff' module Ronin @@ -46,6 +47,8 @@ module Commands # class Diff < Command + include CommandKit::Colors + usage '[options] {URL | FILE} {URL | FILE}' argument :page1, required: true, @@ -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. #