Class: Opts::Shell

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

Constant Summary collapse

CLEAR =
"\e[0m"
BOLD =
"\e[1m"
BLACK =
"\e[30m"
RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
BLUE =
"\e[34m"
MAGENTA =
"\e[35m"
CYAN =
"\e[36m"
WHITE =
"\e[37m"
ON_BLACK =
"\e[40m"
ON_RED =
"\e[41m"
ON_GREEN =
"\e[42m"
ON_YELLOW =
"\e[43m"
ON_BLUE =
"\e[44m"
ON_MAGENTA =
"\e[45m"
ON_CYAN =
"\e[46m"
ON_WHITE =
"\e[47m"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Shell

Returns a new instance of Shell.



26
27
28
29
30
31
# File 'lib/opts/shell.rb', line 26

def initialize(app, options={})
  @app     = app
  @options = { :color => true }.merge(options)
  @padding = 0
  @quite   = false
end

Instance Method Details

#call(env, args) ⇒ Object



33
34
35
36
# File 'lib/opts/shell.rb', line 33

def call(env, args)
  env[:shell] = self
  @app.call(env, args)
end

#inspectObject



98
99
100
# File 'lib/opts/shell.rb', line 98

def inspect
  "#<#{self.class} #{@options[:color] ? 'COLORED' : 'BW'}>"
end

#paddingObject



46
47
48
# File 'lib/opts/shell.rb', line 46

def padding
  @padding
end

#quite?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/opts/shell.rb', line 58

def quite?
  @quite
end

#say(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)$/)) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opts/shell.rb', line 62

def say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)$/))
  message = message.to_s
  message = set_color(message, color) if color
  
  spaces = "  " * padding
  
  if force_new_line
    $stdout.puts(spaces + message)
  else
    $stdout.print(spaces + message)
  end
  $stdout.flush
end

#set_color(string, color = false, bold = false) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/opts/shell.rb', line 88

def set_color(string, color=false, bold=false)
  if color and @options[:color]
    color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
    bold  = bold ? BOLD : ""
    "#{bold}#{color}#{string}#{CLEAR}"
  else
    string
  end
end

#status(type, message, log_status = true) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/opts/shell.rb', line 76

def status(type, message, log_status=true)
  return if quiet? || log_status == false
  spaces = "  " * (padding + 1)
  color  = log_status.is_a?(Symbol) ? log_status : :green
  
  status = status.to_s.rjust(12)
  status = set_color status, color, true if color
  
  $stdout.puts "#{status}#{spaces}#{message}"
  $stdout.flush
end

#with_padding(padding) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/opts/shell.rb', line 38

def with_padding(padding)
  _padding = @padding
  @padding = padding
  yield
ensure
  @padding = _padding
end

#with_quite(quite = true) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/opts/shell.rb', line 50

def with_quite(quite=true)
  _quite = @quite
  @quite = quite
  yield
ensure
  @quite = _quite
end