Class: TTY::Shell::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/shell/statement.rb

Overview

A class representing a statement output to shell.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell = nil, options = {}) ⇒ Statement

Initialize a Statement

Parameters:

  • shell (TTY::Shell) (defaults to: nil)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :newline (Symbol)

    force a newline break after the message

  • :color (Symbol)

    change the message display to color



30
31
32
33
34
# File 'lib/tty/shell/statement.rb', line 30

def initialize(shell=nil, options={})
  @shell = shell || Shell.new
  @newline = options.fetch :newline, true
  @color   = options.fetch :color, nil
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



15
16
17
# File 'lib/tty/shell/statement.rb', line 15

def color
  @color
end

#newlineObject (readonly)

Returns the value of attribute newline.



13
14
15
# File 'lib/tty/shell/statement.rb', line 13

def newline
  @newline
end

Instance Method Details

#declare(message) ⇒ Object

Output the message to the shell

Parameters:

  • message (String)

    the message to be printed to stdout



42
43
44
45
46
47
48
49
50
51
# File 'lib/tty/shell/statement.rb', line 42

def declare(message)
  message = TTY::terminal.color.set message, *color if color

  if newline && /( |\t)(\e\[\d+(;\d+)*m)?\Z/ !~ message
    shell.output.puts message
  else
    shell.output.print message
    shell.output.flush
  end
end