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 = Shell.new, options = {}) ⇒ Statement

Initialize a Statement

Parameters:

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

Options Hash (options):

  • :newline (Symbol)

    force a newline break after the message

  • :color (Symbol)

    change the message display to color



35
36
37
38
39
40
# File 'lib/tty/shell/statement.rb', line 35

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

Instance Attribute Details

#colorObject (readonly)

Color used to display statement



20
21
22
# File 'lib/tty/shell/statement.rb', line 20

def color
  @color
end

#newlineObject (readonly)

Flag to display newline



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

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/tty/shell/statement.rb', line 48

def declare(message)
  message = @pastel.decorate 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