Class: U3dCore::Shell

Inherits:
Interface show all
Defined in:
lib/u3d_core/ui/implementations/shell.rb

Overview

Shell is the terminal output of things For documentation for each of the methods open ‘interface.rb`

Messaging: show text to the user collapse

Errors: Inputs collapse

Instance Method Summary collapse

Methods inherited from Interface

#crash!, #not_implemented, #to_s, #user_error!

Instance Method Details

#command(message) ⇒ Object



92
93
94
# File 'lib/u3d_core/ui/implementations/shell.rb', line 92

def command(message)
  log.info("$ #{message}".cyan.underline)
end

#command_output(message) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/u3d_core/ui/implementations/shell.rb', line 96

def command_output(message)
  actual = (message.split("\r").last || "") # as clearing the line will remove the `>` and the time stamp
  actual.split("\n").each do |msg|
    prefix = msg.include?("") ? "" : ""
    log.info(prefix + "" + msg.magenta)
  end
end

#confirm(message) ⇒ Object



131
132
133
134
# File 'lib/u3d_core/ui/implementations/shell.rb', line 131

def confirm(message)
  verify_interactive!(message)
  agree("#{format_string}#{message.to_s.yellow} (y/n)", true)
end

#deprecated(message) ⇒ Object



88
89
90
# File 'lib/u3d_core/ui/implementations/shell.rb', line 88

def deprecated(message)
  log.error(message.to_s.bold.blue)
end

#error(message) ⇒ Object



72
73
74
# File 'lib/u3d_core/ui/implementations/shell.rb', line 72

def error(message)
  log.error(message.to_s.red)
end

#format_string(datetime = Time.now, severity = "") ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/u3d_core/ui/implementations/shell.rb', line 48

def format_string(datetime = Time.now, severity = "")
  if U3dCore::Globals.log_timestamps?
    timestamp = ENV["U3D_UI_TIMESTAMP"]
    # default timestamp if none specified
    unless timestamp
      timestamp = if U3dCore::Globals.verbose?
                    '%Y-%m-%d %H:%M:%S.%2N'
                  else
                    '%H:%M:%S'
                  end
    end
  end
  # hide has last word
  timestamp = nil if ENV["U3D_HIDE_TIMESTAMP"]
  s = []
  s << "#{severity} " if U3dCore::Globals.verbose? && severity && !severity.empty?
  s << "[#{datetime.strftime(timestamp)}] " if timestamp
  s.join('')
end

#header(message) ⇒ Object



108
109
110
111
112
113
# File 'lib/u3d_core/ui/implementations/shell.rb', line 108

def header(message)
  i = message.length + 8
  success("-" * i)
  success("--- " + message + " ---")
  success("-" * i)
end

#important(message) ⇒ Object



76
77
78
# File 'lib/u3d_core/ui/implementations/shell.rb', line 76

def important(message)
  log.warn(message.to_s.yellow)
end

#input(message) ⇒ Object



126
127
128
129
# File 'lib/u3d_core/ui/implementations/shell.rb', line 126

def input(message)
  verify_interactive!(message)
  ask("#{format_string}#{message.to_s.yellow}").to_s.strip
end

#interactive?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/u3d_core/ui/implementations/shell.rb', line 119

def interactive?
  interactive = true
  interactive = false if $stdout.isatty == false
  interactive = false if Helper.ci?
  return interactive
end

#logObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/u3d_core/ui/implementations/shell.rb', line 28

def log
  return @log if @log

  $stdout.sync = true

  @log ||= if Helper.is_test?
             Logger.new(nil) # don't show any logs when running tests
           else
             Logger.new($stdout)
           end

  @log.formatter = proc do |severity, datetime, _progname, msg|
    "#{format_string(datetime, severity)}#{msg}\n"
  end

  require 'u3d_core/ui/disable_colors' if U3dCore::Helper.colors_disabled?

  @log
end

#message(message) ⇒ Object



84
85
86
# File 'lib/u3d_core/ui/implementations/shell.rb', line 84

def message(message)
  log.info(message.to_s)
end

#password(message) ⇒ Object



143
144
145
146
147
# File 'lib/u3d_core/ui/implementations/shell.rb', line 143

def password(message)
  verify_interactive!(message)

  ask("#{format_string}#{message.to_s.yellow}") { |q| q.echo = "*" }
end

#select(message, options) ⇒ Object



136
137
138
139
140
141
# File 'lib/u3d_core/ui/implementations/shell.rb', line 136

def select(message, options)
  verify_interactive!(message)

  important(message)
  choose(*options)
end

#success(message) ⇒ Object



80
81
82
# File 'lib/u3d_core/ui/implementations/shell.rb', line 80

def success(message)
  log.info(message.to_s.green)
end

#verbose(message) ⇒ Object



104
105
106
# File 'lib/u3d_core/ui/implementations/shell.rb', line 104

def verbose(message)
  log.debug(message.to_s) if U3dCore::Globals.verbose?
end