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)


27
28
29
# File 'lib/kamaze/project/tools/console/output.rb', line 27

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



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

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.



97
98
99
# File 'lib/kamaze/project/tools/console/output.rb', line 97

def output
  @output
end

Instance Method Details

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

Bufferize given arguments

Parameters:

  • strings (Array<String>)

Returns:

  • (Array<String>)


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

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)


44
45
46
47
48
# File 'lib/kamaze/project/tools/console/output.rb', line 44

def flush
  output.flush if output.respond_to?(:flush)

  self
end

#formattable_on?(method) ⇒ Boolean (protected)

Denote given method is formattable

Parameters:

  • method (Symbol)

Returns:

  • (Boolean)


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

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

#formattablesArray<Symbol> (protected)

Formattable methods

Returns:

  • (Array<Symbol>)


102
103
104
# File 'lib/kamaze/project/tools/console/output.rb', line 102

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 66

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

Returns:

  • (Boolean)


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

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)


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

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