class Nanoc::CLI::CleaningStream
An output stream that passes output through stream cleaners. This can be used to strip ANSI color sequences, for instance.
Public Class Methods
@param [IO, StringIO] stream The stream to wrap
# File lib/nanoc/cli/cleaning_stream.rb, line 9 def initialize(stream) @stream = stream @stream_cleaners = [] end
Public Instance Methods
@see IO#<<
# File lib/nanoc/cli/cleaning_stream.rb, line 49 def <<(str) _nanoc_swallow_broken_pipe_errors_while do @stream.<<(_nanoc_clean(str)) end end
Adds a stream cleaner for the given class to this cleaning stream. If the cleaning stream already has the given stream cleaner, nothing happens.
@param [Nanoc::CLI::StreamCleaners::Abstract] klass The class of the
stream cleaner to add
@return [void]
# File lib/nanoc/cli/cleaning_stream.rb, line 21 def add_stream_cleaner(klass) unless @stream_cleaners.map(&:class).include?(klass) @stream_cleaners << klass.new end end
@see IO#close
# File lib/nanoc/cli/cleaning_stream.rb, line 102 def close @stream.close end
@see File#exist?
# File lib/nanoc/cli/cleaning_stream.rb, line 107 def exist? @stream.exist? end
@see File.exists?
# File lib/nanoc/cli/cleaning_stream.rb, line 112 def exists? @stream.exists? end
@see IO.sync=
# File lib/nanoc/cli/cleaning_stream.rb, line 137 def external_encoding @stream.external_encoding end
@see IO#flush
# File lib/nanoc/cli/cleaning_stream.rb, line 66 def flush _nanoc_swallow_broken_pipe_errors_while do @stream.flush end end
@see IO#isatty
# File lib/nanoc/cli/cleaning_stream.rb, line 61 def isatty tty? end
@see IO#print
# File lib/nanoc/cli/cleaning_stream.rb, line 78 def print(str) _nanoc_swallow_broken_pipe_errors_while do @stream.print(_nanoc_clean(str)) end end
@see IO#puts
# File lib/nanoc/cli/cleaning_stream.rb, line 85 def puts(*str) _nanoc_swallow_broken_pipe_errors_while do @stream.puts(*str.map { |ss| _nanoc_clean(ss) }) end end
Removes the stream cleaner for the given class from this cleaning stream. If the cleaning stream does not have the given stream cleaner, nothing happens.
@param [Nanoc::CLI::StreamCleaners::Abstract] klass The class of the
stream cleaner to add
@return [void]
# File lib/nanoc/cli/cleaning_stream.rb, line 35 def remove_stream_cleaner(klass) @stream_cleaners.delete_if { |c| c.class == klass } end
@see IO#reopen
# File lib/nanoc/cli/cleaning_stream.rb, line 97 def reopen(*args) @stream.reopen(*args) end
@see ARGF.set_encoding rubocop:disable Naming/AccessorMethodName
# File lib/nanoc/cli/cleaning_stream.rb, line 143 def set_encoding(*args) @stream.set_encoding(*args) end
@see StringIO#string
# File lib/nanoc/cli/cleaning_stream.rb, line 92 def string @stream.string end
@see IO.sync
# File lib/nanoc/cli/cleaning_stream.rb, line 127 def sync @stream.sync end
@see IO.sync=
# File lib/nanoc/cli/cleaning_stream.rb, line 132 def sync=(arg) @stream.sync = arg end
@see IO#tell
# File lib/nanoc/cli/cleaning_stream.rb, line 73 def tell @stream.tell end
@see IO#tty?
# File lib/nanoc/cli/cleaning_stream.rb, line 56 def tty? @cached_is_tty ||= @stream.tty? end
@see IO.winsize
# File lib/nanoc/cli/cleaning_stream.rb, line 117 def winsize @stream.winsize end
@see IO.winsize=
# File lib/nanoc/cli/cleaning_stream.rb, line 122 def winsize=(arg) @stream.winsize = arg end
@see IO#write
# File lib/nanoc/cli/cleaning_stream.rb, line 42 def write(str) _nanoc_swallow_broken_pipe_errors_while do @stream.write(_nanoc_clean(str)) end end
Protected Instance Methods
rubocop:enable Naming/AccessorMethodName
# File lib/nanoc/cli/cleaning_stream.rb, line 150 def _nanoc_clean(str) @stream_cleaners.reduce(str.to_s.scrub) { |acc, elem| elem.clean(acc) } end
# File lib/nanoc/cli/cleaning_stream.rb, line 154 def _nanoc_swallow_broken_pipe_errors_while yield rescue Errno::EPIPE end