Class: Kamaze::Project::Tools::Console::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/kamaze/project/tools/console/output.rb,
lib/kamaze/project/tools/console/output/buffer.rb

Overview

Provide a console output

Console output can be initialized from a IO (STDOUT and/or STDERR) a File, using File.open(), or a StringIO.

As a result, console provides a convenient way to suppress or replace standard outputs (keeping them untouched).

Console output uses cli-ui to provide colored formatting.

Defined Under Namespace

Classes: Buffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to = $stdout) ⇒ Output

Returns a new instance of Output.

Parameters:

  • to (IO) (defaults to: $stdout)


34
35
36
# File 'lib/kamaze/project/tools/console/output.rb', line 34

def initialize(to = $stdout)
  @output = to
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/kamaze/project/tools/console/output.rb', line 57

def method_missing(method, *args, &block)
  if respond_to_missing?(method)
    args = bufferize(*args) if formattable_on?(method.to_sym)

    return output.public_send(method, *args, &block)
  end

  super
end

Instance Attribute Details

#outputObject (readonly, protected)

Returns the value of attribute output.



104
105
106
# File 'lib/kamaze/project/tools/console/output.rb', line 104

def output
  @output
end

Instance Method Details

#bufferize(*strings) ⇒ Array<Buffer>|Array<String> (protected)

Bufferize given arguments

Parameters:

  • strings (Array<String>)

Returns:

  • (Array<Buffer>|Array<String>)


125
126
127
# File 'lib/kamaze/project/tools/console/output.rb', line 125

def bufferize(*strings)
  strings.to_a.map { |s| Buffer.new(self, s) }
end

#flushself

Flushes any buffered data to the underlying operating system

note that this is Ruby internal buffering only; the OS may buffer the data as well

Returns:

  • (self)


51
52
53
54
55
# File 'lib/kamaze/project/tools/console/output.rb', line 51

def flush
  self.tap do
    output.flush if output.respond_to?(:flush)
  end
end

#formattable_on?(method) ⇒ Boolean (protected)

Denote given method is formattable

Parameters:

  • method (Symbol)

Returns:

  • (Boolean)


116
117
118
# File 'lib/kamaze/project/tools/console/output.rb', line 116

def formattable_on?(method)
  formattables.include?(method.to_sym)
end

#formattablesArray<Symbol> (protected)

Formattable methods

Returns:

  • (Array<Symbol>)


109
110
111
# File 'lib/kamaze/project/tools/console/output.rb', line 109

def formattables
  [:puts, :print, :write]
end

#puts(s) ⇒ nil

Writes the given objects to ios as with IO#print.

Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each element on a new line. If called without arguments, outputs a single record separator.

Parameters:

  • s (String)

Returns:

  • (nil)


# File 'lib/kamaze/project/tools/console/output.rb', line 73

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/kamaze/project/tools/console/output.rb', line 67

def respond_to_missing?(method, include_private = false)
  return true if output.respond_to?(method, include_private)

  super(method, include_private)
end

#tty?Boolean

Denote is a tty

Returns:

  • (Boolean)


41
42
43
# File 'lib/kamaze/project/tools/console/output.rb', line 41

def tty?
  output.respond_to?(:'tty?') ? output.tty? : false
end