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`

Defined Under Namespace

Classes: EPipeIgnorerLogDevice

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



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

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

#command_output(message) ⇒ Object



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

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



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

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

#deprecated(message) ⇒ Object



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

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

#error(message) ⇒ Object



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

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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/u3d_core/ui/implementations/shell.rb', line 62

def format_string(datetime = Time.now, severity = "")
  if U3dCore::Globals.log_timestamps?
    timestamp = ENV["U3D_UI_TIMESTAMP"]
    # default timestamp if none specified
    timestamp ||= if U3dCore::Globals.verbose?
                    '%Y-%m-%d %H:%M:%S.%2N'
                  else
                    '%H:%M:%S'
                  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



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

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

#important(message) ⇒ Object



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

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

#input(message) ⇒ Object



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

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

#interactive?Boolean

Returns:

  • (Boolean)


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

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.test?
             Logger.new(nil) # don't show any logs when running tests
           else
             Logger.new(EPipeIgnorerLogDevice.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



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

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

#password(message) ⇒ Object



155
156
157
158
159
# File 'lib/u3d_core/ui/implementations/shell.rb', line 155

def password(message)
  verify_interactive!(message)

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

#select(message, options) ⇒ Object



148
149
150
151
152
153
# File 'lib/u3d_core/ui/implementations/shell.rb', line 148

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

  important(message)
  choose(*options)
end

#success(message) ⇒ Object



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

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

#verbose(message) ⇒ Object



116
117
118
# File 'lib/u3d_core/ui/implementations/shell.rb', line 116

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