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



31
32
33
34
# File 'lib/u3d_core/ui/implementations/shell.rb', line 31

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

Instance Method Details

#command(message) ⇒ Object



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

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

#command_output(message) ⇒ Object



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

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



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

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

#deprecated(message) ⇒ Object



107
108
109
# File 'lib/u3d_core/ui/implementations/shell.rb', line 107

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

#error(message) ⇒ Object



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

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

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



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

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



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

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

#important(message) ⇒ Object



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

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

#input(message) ⇒ Object



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

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

#interactive?Boolean

Returns:

  • (Boolean)


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

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

#logObject



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

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



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

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

#password(message) ⇒ Object



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

def password(message)
  verify_interactive!(message)

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

#select(message, options) ⇒ Object



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

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

  important(message)
  choose(*options)
end

#success(message) ⇒ Object



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

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

#verbose(message) ⇒ Object



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

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