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


161
162
163
164
# File 'lib/guard/ui.rb', line 161

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.


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

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.


152
153
154
# File 'lib/guard/ui.rb', line 152

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


119
120
121
# File 'lib/guard/ui.rb', line 119

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


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

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


94
95
96
# File 'lib/guard/ui.rb', line 94

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


74
75
76
# File 'lib/guard/ui.rb', line 74

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

.level=(new_level) ⇒ Object

Assigns a log level


63
64
65
66
# File 'lib/guard/ui.rb', line 63

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

.loggerObject

Get the Guard::UI logger instance


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

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


46
47
48
# File 'lib/guard/ui.rb', line 46

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


58
59
60
# File 'lib/guard/ui.rb', line 58

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

.reset_and_clearObject

TODO: arguments: UI uses Guard::options anyway


145
146
147
148
# File 'lib/guard/ui.rb', line 145

def reset_and_clear
  @clearable = false
  clear(force: true)
end

.reset_lineObject

Reset a line.


125
126
127
# File 'lib/guard/ui.rb', line 125

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


38
39
40
# File 'lib/guard/ui.rb', line 38

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


84
85
86
# File 'lib/guard/ui.rb', line 84

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