[DRE-commits] [ruby-aruba] 45/98: Improve format of announcer output

Hideki Yamane henrich at moszumanska.debian.org
Tue Mar 22 12:20:38 UTC 2016


This is an automated email from the git hooks/post-receive script.

henrich pushed a commit to branch debian/sid
in repository ruby-aruba.

commit 6312be75d6a50356090c055243ed1732c33780da
Author: Dennis Günnewig <dg1 at ratiodata.de>
Date:   Mon Jan 4 11:35:54 2016 +0100

    Improve format of announcer output
---
 lib/aruba/colorizer.rb              | 112 ++++++++++++++++++++++++++++++++++++
 lib/aruba/platforms/announcer.rb    |  20 ++++---
 lib/aruba/platforms/simple_table.rb |  18 ++----
 3 files changed, 130 insertions(+), 20 deletions(-)

diff --git a/lib/aruba/colorizer.rb b/lib/aruba/colorizer.rb
new file mode 100644
index 0000000..327d1ef
--- /dev/null
+++ b/lib/aruba/colorizer.rb
@@ -0,0 +1,112 @@
+module Aruba
+  # The ANSIColor module can be used for namespacing and mixed into your own
+  # classes.
+  module AnsiColor
+    # :stopdoc:
+    ATTRIBUTES = [
+      [ :clear        ,   0 ],
+      [ :reset        ,   0 ],     # synonym for :clear
+      [ :bold         ,   1 ],
+      [ :dark         ,   2 ],
+      [ :italic       ,   3 ],     # not widely implemented
+      [ :underline    ,   4 ],
+      [ :underscore   ,   4 ],     # synonym for :underline
+      [ :blink        ,   5 ],
+      [ :rapid_blink  ,   6 ],     # not widely implemented
+      [ :negative     ,   7 ],     # no reverse because of String#reverse
+      [ :concealed    ,   8 ],
+      [ :strikethrough,   9 ],     # not widely implemented
+      [ :black        ,  30 ],
+      [ :red          ,  31 ],
+      [ :green        ,  32 ],
+      [ :yellow       ,  33 ],
+      [ :blue         ,  34 ],
+      [ :magenta      ,  35 ],
+      [ :cyan         ,  36 ],
+      [ :white        ,  37 ],
+      [ :on_black     ,  40 ],
+      [ :on_red       ,  41 ],
+      [ :on_green     ,  42 ],
+      [ :on_yellow    ,  43 ],
+      [ :on_blue      ,  44 ],
+      [ :on_magenta   ,  45 ],
+      [ :on_cyan      ,  46 ],
+      [ :on_white     ,  47 ],
+    ]
+
+    ATTRIBUTE_NAMES = ATTRIBUTES.transpose.first
+    # :startdoc:
+
+    # Returns true, if the coloring function of this module
+    # is switched on, false otherwise.
+    def self.coloring?
+      @coloring
+    end
+
+    # Turns the coloring on or off globally, so you can easily do
+    # this for example:
+    #  Cucumber::Term::ANSIColor::coloring = STDOUT.isatty
+    def self.coloring=(val)
+      @coloring = val
+    end
+    self.coloring = true
+
+    ATTRIBUTES.each do |c, v|
+      define_method(c) do |string|
+          result = ''
+          result << "\e[#{v}m" if Aruba::AnsiColor.coloring?
+          if block_given?
+            result << yield
+          elsif string
+            result << string
+          elsif respond_to?(:to_str)
+            result << to_str
+          else
+            return result #only switch on
+          end
+          result << "\e[0m" if Aruba::AnsiColor.coloring?
+          result
+      end
+    end
+
+    # Regular expression that is used to scan for ANSI-sequences while
+    # uncoloring strings.
+    COLORED_REGEXP = /\e\[(?:[34][0-7]|[0-9])?m/
+
+
+    def self.included(klass)
+      if klass == String
+        ATTRIBUTES.delete(:clear)
+        ATTRIBUTE_NAMES.delete(:clear)
+      end
+    end
+
+    # Returns an uncolored version of the string, that is all
+    # ANSI-sequences are stripped from the string.
+    def uncolored(string = nil) # :yields:
+      if block_given?
+        yield.gsub(COLORED_REGEXP, '')
+      elsif string
+        string.gsub(COLORED_REGEXP, '')
+      elsif respond_to?(:to_str)
+        to_str.gsub(COLORED_REGEXP, '')
+      else
+        ''
+      end
+    end
+
+    module_function
+
+    # Returns an array of all Aruba::Platforms::AnsiColor attributes as symbols.
+    def attributes
+      ATTRIBUTE_NAMES
+    end
+    extend self
+  end
+end
+
+module Aruba
+  class Colorizer
+    include Aruba::AnsiColor
+  end
+end
diff --git a/lib/aruba/platforms/announcer.rb b/lib/aruba/platforms/announcer.rb
index 4f14393..e5a29bb 100644
--- a/lib/aruba/platforms/announcer.rb
+++ b/lib/aruba/platforms/announcer.rb
@@ -1,4 +1,7 @@
 require 'shellwords'
+require 'aruba/colorizer'
+
+Aruba::AnsiColor.coloring = false if !STDOUT.tty? && !ENV.has_key?("AUTOTEST")
 
 # Aruba
 module Aruba
@@ -48,7 +51,7 @@ module Aruba
 
       private
 
-      attr_reader :announcers, :announcer, :channels, :output_formats
+      attr_reader :announcers, :announcer, :channels, :output_formats, :colorizer
 
       public
 
@@ -57,6 +60,8 @@ module Aruba
         @announcers << PutsAnnouncer.new
         @announcers << KernelPutsAnnouncer.new
 
+        @colorizer = Aruba::Colorizer.new
+
         @announcer         = @announcers.first
         @channels          = {}
         @output_formats    = {}
@@ -75,7 +80,7 @@ module Aruba
         output_format :command, '$ %s'
         output_format :directory, '$ cd %s'
         output_format :environment, proc { |n, v| format('$ export %s=%s', n, Shellwords.escape(v)) }
-        output_format :full_environment, proc { |h| Aruba.platform.simple_table(h) }
+        output_format :full_environment, proc { |h| format("<<-ENVIRONMENT\n%s\nENVIRONMENT", Aruba.platform.simple_table(h)) }
         output_format :modified_environment, proc { |n, v| format('$ export %s=%s', n, Shellwords.escape(v)) }
         output_format :stderr, "<<-STDERR\n%s\nSTDERR"
         output_format :stdout, "<<-STDOUT\n%s\nSTDOUT"
@@ -83,12 +88,9 @@ module Aruba
         output_format :stop_signal, proc { |p, s| format('Command will be stopped with `kill -%s %s`', s, p) }
         output_format :timeout, '# %s-timeout: %s seconds'
         output_format :wait_time, '# %s: %s seconds'
-        output_format(
-          :command_filesystem_status, proc do |status|
-          puts 'Command Filesystem Status:'
-          Aruba.platform.simple_table(status.to_h, :sort => false)
-        end
-        )
+        # rubocop:disable Metrics/LineLength
+        output_format :command_filesystem_status, proc { |status| format("<<-COMMAND FILESYSTEM STATUS\n%s\nCOMMAND FILESYSTEM STATUS", Aruba.platform.simple_table(status.to_h, :sort => false)) }
+        # rubocop:enable Metrics/LineLength
 
         # rubocop:disable Metrics/LineLength
         if @options[:stdout]
@@ -187,6 +189,8 @@ module Aruba
                   else
                     the_output_format.call(*args)
                   end
+        message += "\n"
+        message = colorizer.cyan(message)
 
         announcer.announce(message)
 
diff --git a/lib/aruba/platforms/simple_table.rb b/lib/aruba/platforms/simple_table.rb
index 69f06c5..4ebc5fa 100644
--- a/lib/aruba/platforms/simple_table.rb
+++ b/lib/aruba/platforms/simple_table.rb
@@ -37,22 +37,16 @@ module Aruba
           hash.each do |k,v|
             rows << format("# %-#{name_size}s => %s", k, v)
           end
-
-          if opts[:sort] == true
-            rows.sort
-          else
-            rows
-          end
         else
-          result = hash.each_with_object([]) do |(k,v), a|
+          rows = hash.each_with_object([]) do |(k,v), a|
             a << format("# %-#{name_size}s => %s", k, v)
           end
+        end
 
-          if opts[:sort] == true
-            result.sort
-          else
-            result
-          end
+        if opts[:sort] == true
+          rows.sort.join("\n")
+        else
+          rows.join("\n")
         end
       end
     end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-aruba.git



More information about the Pkg-ruby-extras-commits mailing list