Module: Guard::UI

Includes:
Colors
Defined in:
lib/guard/ui.rb,
lib/guard/ui/colors.rb,
lib/guard/ui/config.rb,
lib/guard/ui/logger.rb

Overview

The UI class helps to format messages for the user. Everything that is logged through this class is considered either as an error message or a diagnostic message and is written to standard error ($stderr).

If your Guard plugin does some output that is piped into another process for further processing, please just write it to STDOUT with puts.

Defined Under Namespace

Modules: Colors Classes: Config, Logger

Constant Summary

Constants included from Colors

Colors::ANSI_ESCAPE_BGBLACK, Colors::ANSI_ESCAPE_BGBLUE, Colors::ANSI_ESCAPE_BGCYAN, Colors::ANSI_ESCAPE_BGGREEN, Colors::ANSI_ESCAPE_BGMAGENTA, Colors::ANSI_ESCAPE_BGRED, Colors::ANSI_ESCAPE_BGWHITE, Colors::ANSI_ESCAPE_BGYELLOW, Colors::ANSI_ESCAPE_BLACK, Colors::ANSI_ESCAPE_BLUE, Colors::ANSI_ESCAPE_BRIGHT, Colors::ANSI_ESCAPE_CYAN, Colors::ANSI_ESCAPE_GREEN, Colors::ANSI_ESCAPE_MAGENTA, Colors::ANSI_ESCAPE_RED, Colors::ANSI_ESCAPE_WHITE, Colors::ANSI_ESCAPE_YELLOW

Class Method Summary collapse

Class Method Details

.action_with_scopes(action, scope) ⇒ Object

Show a scoped action message.

Parameters:

  • action (String)

    the action to show

  • scope (Hash)

    hash with a guard or a group scope


163
164
165
166
# File 'lib/guard/ui.rb', line 163

def action_with_scopes(action, scope)
  titles = Guard.state.scope.titles(scope)
  info "#{action} #{titles.join(', ')}"
end

.clear(opts = {}) ⇒ Object

Clear the output if clearable.


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/guard/ui.rb', line 133

def clear(opts = {})
  return unless Guard.state.session.clear?

  fail "UI not set up!" if @clearable.nil?
  return unless @clearable || opts[:force]

  @clearable = false
  Terminal.clear
rescue Errno::ENOENT => e
  warning("Failed to clear the screen: #{e.inspect}")
end

.clearableObject

Allow the screen to be cleared again.


154
155
156
# File 'lib/guard/ui.rb', line 154

def clearable
  @clearable = true
end

.debug(message, options = {}) ⇒ Object

Show a debug message that is prefixed with DEBUG and a timestamp.

Parameters:

  • message (String)

    the message to show

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • reset (Boolean)

    whether to clean the output before

  • plugin (String)

    manually define the calling plugin


121
122
123
# File 'lib/guard/ui.rb', line 121

def debug(message, options = {})
  _filtered_logger_message(message, :debug, :yellow, options)
end

.deprecation(message, options = {}) ⇒ Object

Show a red deprecation message that is prefixed with DEPRECATION. It has a log level of warn.

Parameters:

  • message (String)

    the message to show

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • reset (Boolean)

    whether to clean the output before

  • plugin (String)

    manually define the calling plugin


107
108
109
110
111
112
113
# File 'lib/guard/ui.rb', line 107

def deprecation(message, options = {})
  unless ENV["GUARD_GEM_SILENCE_DEPRECATIONS"] == "1"
    backtrace = Thread.current.backtrace[1..5].join("\n\t >")
    msg = format("%<message>s\nDeprecation backtrace: %<backtrace>s", message: message, backtrace: backtrace)
    warning(msg, options)
  end
end

.error(message, options = {}) ⇒ Object

Show a red error message that is prefixed with ERROR.

Parameters:

  • message (String)

    the message to show

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • reset (Boolean)

    whether to clean the output before

  • plugin (String)

    manually define the calling plugin


96
97
98
# File 'lib/guard/ui.rb', line 96

def error(message, options = {})
  _filtered_logger_message(message, :error, :red, options)
end

.info(message, options = {}) ⇒ Object

Show an info message.

Parameters:

  • message (String)

    the message to show

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • reset (Boolean)

    whether to clean the output before

  • plugin (String)

    manually define the calling plugin


76
77
78
# File 'lib/guard/ui.rb', line 76

def info(message, options = {})
  _filtered_logger_message(message, :info, nil, options)
end

.level=(new_level) ⇒ Object

Assigns a log level


65
66
67
68
# File 'lib/guard/ui.rb', line 65

def level=(new_level)
  options.logger_config.level = new_level
  @logger.level = new_level if @logger
end

.loggerObject

Get the Guard::UI logger instance


28
29
30
31
32
33
34
# File 'lib/guard/ui.rb', line 28

def logger
  @logger ||=
    begin
      require "lumberjack"
      Lumberjack::Logger.new(options.device, options.logger_config)
    end
end

.optionsHash

Get the logger options

Returns:

  • (Hash)

    the logger options


48
49
50
# File 'lib/guard/ui.rb', line 48

def options
  @options ||= Config.new
end

.options=(options) ⇒ Object

Set the logger options

TODO: deprecate?

Parameters:

  • options (Hash)

    the logger options

Options Hash (options):

  • level (Symbol)

    the log level

  • template (String)

    the logger template

  • time_format (String)

    the time format


60
61
62
# File 'lib/guard/ui.rb', line 60

def options=(options)
  @options = Config.new(options)
end

.reset_lineObject

Reset a line.


127
128
129
# File 'lib/guard/ui.rb', line 127

def reset_line
  $stderr.print(color_enabled? ? "\r\e[0m" : "\r\n")
end

.reset_loggerObject

Since logger is global, for Aruba in-process to properly separate output between calls, we need to reset

We don't use logger=() since it's expected to be a Lumberjack instance


40
41
42
# File 'lib/guard/ui.rb', line 40

def reset_logger
  @logger = nil
end

.warning(message, options = {}) ⇒ Object

Show a yellow warning message that is prefixed with WARNING.

Parameters:

  • message (String)

    the message to show

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • reset (Boolean)

    whether to clean the output before

  • plugin (String)

    manually define the calling plugin


86
87
88
# File 'lib/guard/ui.rb', line 86

def warning(message, options = {})
  _filtered_logger_message(message, :warn, :yellow, options)
end