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

new(stream) click to toggle source

@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

<<(s) click to toggle source

@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
add_stream_cleaner(klass) click to toggle source

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
close() click to toggle source

@see IO#close

# File lib/nanoc/cli/cleaning_stream.rb, line 96
def close
  @stream.close
end
exist?() click to toggle source

@see File#exist?

# File lib/nanoc/cli/cleaning_stream.rb, line 101
def exist?
  @stream.exist?
end
exists?() click to toggle source

@see File.exists?

# File lib/nanoc/cli/cleaning_stream.rb, line 106
def exists?
  @stream.exists?
end
external_encoding() click to toggle source

@see IO.sync=

# File lib/nanoc/cli/cleaning_stream.rb, line 131
def external_encoding
  @stream.external_encoding
end
flush() click to toggle source

@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
print(s) click to toggle source

@see IO#print

puts(*s) click to toggle source

@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
remove_stream_cleaner(klass) click to toggle source

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
reopen(*a) click to toggle source

@see IO#reopen

# File lib/nanoc/cli/cleaning_stream.rb, line 91
def reopen(*a)
  @stream.reopen(*a)
end
set_encoding(*args) click to toggle source

@see ARGF.set_encoding

# File lib/nanoc/cli/cleaning_stream.rb, line 136
def set_encoding(*args)
  @stream.set_encoding(*args)
end
string() click to toggle source

@see StringIO#string

# File lib/nanoc/cli/cleaning_stream.rb, line 86
def string
  @stream.string
end
sync() click to toggle source

@see IO.sync

# File lib/nanoc/cli/cleaning_stream.rb, line 121
def sync
  @stream.sync
end
sync=(arg) click to toggle source

@see IO.sync=

# File lib/nanoc/cli/cleaning_stream.rb, line 126
def sync=(arg)
  @stream.sync = arg
end
tell() click to toggle source

@see IO#tell

# File lib/nanoc/cli/cleaning_stream.rb, line 67
def tell
  @stream.tell
end
tty?() click to toggle source

@see IO#tty?

# File lib/nanoc/cli/cleaning_stream.rb, line 55
def tty?
  @cached_is_tty ||= @stream.tty?
end
winsize() click to toggle source

@see IO.winsize

# File lib/nanoc/cli/cleaning_stream.rb, line 111
def winsize
  @stream.winsize
end
winsize=(arg) click to toggle source

@see IO.winsize=

# File lib/nanoc/cli/cleaning_stream.rb, line 116
def winsize=(arg)
  @stream.winsize = arg
end
write(s) click to toggle source

@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

_nanoc_clean(s) click to toggle source
# 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
_nanoc_swallow_broken_pipe_errors_while() { || ... } click to toggle source
# File lib/nanoc/cli/cleaning_stream.rb, line 146
def _nanoc_swallow_broken_pipe_errors_while
  yield
rescue Errno::EPIPE
end