class Nanoc::CLI::CleaningStream
An output stream that passes output through stream cleaners. This can be used to strip ANSI color sequences, for instance.
@api private
Public Class Methods
@param [IO, StringIO] stream The stream to wrap
# File lib/nanoc/cli/cleaning_stream.rb, line 8 def initialize(stream) @stream = stream @stream_cleaners = [] end
Public Instance Methods
@see IO#<<
# File lib/nanoc/cli/cleaning_stream.rb, line 48 def <<(s) _nanoc_swallow_broken_pipe_errors_while do @stream.<<(_nanoc_clean(s)) 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 20 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 96 def close @stream.close end
@see File#exist?
# File lib/nanoc/cli/cleaning_stream.rb, line 101 def exist? @stream.exist? end
@see File.exists?
# File lib/nanoc/cli/cleaning_stream.rb, line 106 def exists? @stream.exists? end
@see IO.sync=
# File lib/nanoc/cli/cleaning_stream.rb, line 131 def external_encoding @stream.external_encoding end
@see IO#flush
# File lib/nanoc/cli/cleaning_stream.rb, line 60 def flush _nanoc_swallow_broken_pipe_errors_while do @stream.flush end end
@see IO#print
# File lib/nanoc/cli/cleaning_stream.rb, line 72 def print(s) _nanoc_swallow_broken_pipe_errors_while do @stream.print(_nanoc_clean(s)) end end
@see IO#puts
# File lib/nanoc/cli/cleaning_stream.rb, line 79 def puts(*s) _nanoc_swallow_broken_pipe_errors_while do @stream.puts(*s.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 34 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 91 def reopen(*a) @stream.reopen(*a) end
@see ARGF.set_encoding
# File lib/nanoc/cli/cleaning_stream.rb, line 136 def set_encoding(*args) @stream.set_encoding(*args) end
@see StringIO#string
# File lib/nanoc/cli/cleaning_stream.rb, line 86 def string @stream.string end
@see IO.sync
# File lib/nanoc/cli/cleaning_stream.rb, line 121 def sync @stream.sync end
@see IO.sync=
# File lib/nanoc/cli/cleaning_stream.rb, line 126 def sync=(arg) @stream.sync = arg end
@see IO#tell
# File lib/nanoc/cli/cleaning_stream.rb, line 67 def tell @stream.tell end
@see IO#tty?
# File lib/nanoc/cli/cleaning_stream.rb, line 55 def tty? @cached_is_tty ||= @stream.tty? end
@see IO.winsize
# File lib/nanoc/cli/cleaning_stream.rb, line 111 def winsize @stream.winsize end
@see IO.winsize=
# File lib/nanoc/cli/cleaning_stream.rb, line 116 def winsize=(arg) @stream.winsize = arg end
@see IO#write
# File lib/nanoc/cli/cleaning_stream.rb, line 41 def write(s) _nanoc_swallow_broken_pipe_errors_while do @stream.write(_nanoc_clean(s)) end end
Protected Instance Methods
# File lib/nanoc/cli/cleaning_stream.rb, line 142 def _nanoc_clean(s) @stream_cleaners.reduce(s.to_s) { |a, e| e.clean(a) } end
# File lib/nanoc/cli/cleaning_stream.rb, line 146 def _nanoc_swallow_broken_pipe_errors_while yield rescue Errno::EPIPE end