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!

Constructor Details

#initialize(test_log_buffer: nil) ⇒ Shell

test_log_buffer: by default, don’t show any logs when running tests



29
30
31
# File 'lib/u3d_core/ui/implementations/shell.rb', line 29

def initialize(test_log_buffer: nil)
  @test_log_buffer = test_log_buffer
end

Instance Method Details

#command(message) ⇒ Object



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

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

#command_output(message) ⇒ Object



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

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



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

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

#deprecated(message) ⇒ Object



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

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

#error(message) ⇒ Object



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

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

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/u3d_core/ui/implementations/shell.rb', line 67

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



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

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

#important(message) ⇒ Object



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

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

#input(message) ⇒ Object



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

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

#interactive?Boolean

Returns:

  • (Boolean)


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

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

#logObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/u3d_core/ui/implementations/shell.rb', line 33

def log
  return @log if @log

  $stdout.sync = true

  @log ||= if Helper.test?
             Logger.new(@test_log_buffer)
           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



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

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

#password(message) ⇒ Object



160
161
162
163
164
# File 'lib/u3d_core/ui/implementations/shell.rb', line 160

def password(message)
  verify_interactive!(message)

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

#select(message, options) ⇒ Object



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

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

  important(message)
  choose(*options)
end

#success(message) ⇒ Object



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

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

#verbose(message) ⇒ Object



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

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