lib/nanoc/cli.rb in nanoc-3.3.7 vs lib/nanoc/cli.rb in nanoc-3.4.0

- old
+ new

@@ -5,17 +5,20 @@ module Nanoc::CLI module Commands end - autoload 'Logger', 'nanoc/cli/logger' - autoload 'CommandRunner', 'nanoc/cli/command_runner' - autoload 'ErrorHandler', 'nanoc/cli/error_handler' + autoload 'ANSIStringColorizer', 'nanoc/cli/ansi_string_colorizer' + autoload 'Logger', 'nanoc/cli/logger' + autoload 'CommandRunner', 'nanoc/cli/command_runner' + autoload 'CleaningStream', 'nanoc/cli/cleaning_stream' + autoload 'StreamCleaners', 'nanoc/cli/stream_cleaners' + autoload 'ErrorHandler', 'nanoc/cli/error_handler' # Deprecated; use CommandRunner instead # TODO [in nanoc 4.0] remove me - autoload 'Command', 'nanoc/cli/command_runner' + autoload 'Command', 'nanoc/cli/command_runner' # @return [Boolean] true if debug output is enabled, false if not # # @since 3.2.0 def self.debug? @@ -58,10 +61,13 @@ # Makes the commandline interface ready for using by loading the commands. # # @return [void] def self.setup + # Set up output streams + self.setup_cleaning_streams + # Reinit @root_command = nil # Add help command help_cmd = Cri::Command.new_basic_help @@ -130,8 +136,44 @@ def self.recursive_contents_of(path) return [] unless File.directory?(path) files, dirs = *Dir[path + '/*'].sort.partition { |e| File.file?(e) } dirs.each { |d| files.concat self.recursive_contents_of(d) } files + end + + # Wraps `$stdout` and `$stderr` in appropriate cleaning streams. + # + # @return [void] + def self.setup_cleaning_streams + $stdout = Nanoc::CLI::CleaningStream.new($stdout) + $stderr = Nanoc::CLI::CleaningStream.new($stderr) + + if !self.enable_utf8? + $stdout.add_stream_cleaner(Nanoc::CLI::StreamCleaners::UTF8) + $stderr.add_stream_cleaner(Nanoc::CLI::StreamCleaners::UTF8) + end + + if !self.enable_ansi_colors? + $stdout.add_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors) + $stderr.add_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors) + end + end + + # @return [Boolean] true if UTF-8 support is present, false if not + def self.enable_utf8? + %w( LC_ALL LC_CTYPE LANG ).any? { |e| ENV[e] =~ /UTF/ } + end + + # @return [Boolean] true if color support is present, false if not + def self.enable_ansi_colors? + return false if !$stdout.tty? + + begin + require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /mswin|mingw/ + rescue LoadError + return false + end + + return true end end