Class: Lhj::Shell

Inherits:
Interface show all
Defined in:
lib/lhj/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

#abort_with_message!, #build_failure!, #crash!, #not_implemented, #shell_error!, #test_failure!, #to_s, #user_error!

Instance Method Details

#command(message) ⇒ Object



55
56
57
# File 'lib/lhj/ui/implementations/shell.rb', line 55

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

#command_output(message) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/lhj/ui/implementations/shell.rb', line 59

def command_output(message)
  actual = (encode_as_utf_8_if_possible(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



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

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

#content_error(content, error_line) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lhj/ui/implementations/shell.rb', line 75

def content_error(content, error_line)
  error_line = error_line.to_i
  return unless error_line > 0

  contents = content.split(/\r?\n/).map(&:chomp)

  start_line = error_line - 2 < 1 ? 1 : error_line - 2
  end_line = error_line + 2 < contents.length ? error_line + 2 : contents.length

  Range.new(start_line, end_line).each do |line|
    str = line == error_line ? " => " : "    "
    str << line.to_s.rjust(Math.log10(end_line) + 1)
    str << ":\t#{contents[line - 1]}"
    error(str)
  end
end

#deprecated(message) ⇒ Object



51
52
53
# File 'lib/lhj/ui/implementations/shell.rb', line 51

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

#error(message) ⇒ Object



35
36
37
# File 'lib/lhj/ui/implementations/shell.rb', line 35

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

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



27
28
29
# File 'lib/lhj/ui/implementations/shell.rb', line 27

def format_string(datetime = Time.now, severity = "")
    return "[#{datetime.strftime('%H:%M:%S')}]: "
end

#header(message) ⇒ Object



71
72
73
# File 'lib/lhj/ui/implementations/shell.rb', line 71

def header(message)
  success(message)
end

#important(message) ⇒ Object



39
40
41
# File 'lib/lhj/ui/implementations/shell.rb', line 39

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

#input(message) ⇒ Object



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

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

#interactive?Boolean

Returns:

  • (Boolean)


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

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

#logObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lhj/ui/implementations/shell.rb', line 8

def log
  return @log if @log

  $stdout.sync = true

  # if !ENV.key?('DEBUG')
  #   $stdout.puts("Logging disabled while running tests. Force them by setting the DEBUG environment variable")
  #   @log ||= Logger.new(nil) # don't show any logs when running tests
  # else
    @log ||= Logger.new($stdout)
  # end

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

  @log
end

#message(message) ⇒ Object



47
48
49
# File 'lib/lhj/ui/implementations/shell.rb', line 47

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

#password(message) ⇒ Object



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

def password(message)
  verify_interactive!(message)

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

#select(message, options) ⇒ Object



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

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

  important(message)
  choose(*options)
end

#success(message) ⇒ Object



43
44
45
# File 'lib/lhj/ui/implementations/shell.rb', line 43

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

#verbose(message) ⇒ Object



67
68
69
# File 'lib/lhj/ui/implementations/shell.rb', line 67

def verbose(message)
  log.debug(message.to_s)
end