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

new(stream) click to toggle source

@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

<<(str) click to toggle source

@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
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 21
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 102
def close
  @stream.close
end
exist?() click to toggle source

@see File#exist?

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

@see File.exists?

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

@see IO.sync=

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

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

@see IO#isatty

# File lib/nanoc/cli/cleaning_stream.rb, line 61
def isatty
  tty?
end
print(str) click to toggle source

@see IO#print

puts(*str) click to toggle source

@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
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 35
def remove_stream_cleaner(klass)
  @stream_cleaners.delete_if { |c| c.class == klass }
end
reopen(*args) click to toggle source

@see IO#reopen

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

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

@see StringIO#string

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

@see IO.sync

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

@see IO.sync=

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

@see IO#tell

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

@see IO#tty?

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

@see IO.winsize

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

@see IO.winsize=

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

@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

_nanoc_clean(str) click to toggle source

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