lib/nanoc/cli.rb in nanoc-3.4.1 vs lib/nanoc/cli.rb in nanoc-3.4.2

- old
+ new

@@ -47,11 +47,10 @@ # # @return [void] def self.run(args) Nanoc::CLI::ErrorHandler.handle_while do self.setup - self.load_custom_commands self.root_command.run(args) end end # Adds the given command to the collection of available commands. @@ -63,17 +62,23 @@ self.root_command.add_command(cmd) end protected - # Makes the commandline interface ready for using by loading the commands. + # Makes the commandline interface ready for use. # # @return [void] def self.setup - # Set up output streams self.setup_cleaning_streams + self.setup_commands + self.load_custom_commands + end + # Sets up the root command and base subcommands. + # + # @return [void] + def self.setup_commands # Reinit @root_command = nil # Add help command help_cmd = Cri::Command.new_basic_help @@ -86,11 +91,11 @@ cmd = self.load_command_at(filename) self.add_command(cmd) end end - # Loads the commands in `commands/`. + # Loads site-specific commands in `commands/`. # # @return [void] def self.load_custom_commands self.recursive_contents_of('commands').each do |filename| # Create command @@ -151,28 +156,36 @@ # @return [void] def self.setup_cleaning_streams $stdout = Nanoc::CLI::CleaningStream.new($stdout) $stderr = Nanoc::CLI::CleaningStream.new($stderr) - if !self.enable_utf8? + if !self.enable_utf8?($stdout) $stdout.add_stream_cleaner(Nanoc::CLI::StreamCleaners::UTF8) + end + + if !self.enable_utf8?($stderr) $stderr.add_stream_cleaner(Nanoc::CLI::StreamCleaners::UTF8) end - if !self.enable_ansi_colors? + if !self.enable_ansi_colors?($stdout) $stdout.add_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors) + end + + if !self.enable_ansi_colors?($stderr) $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? + def self.enable_utf8?(io) + return true if !io.tty? + %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? + def self.enable_ansi_colors?(io) + return false if !io.tty? begin require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /mswin|mingw/ rescue LoadError return false