Class: Pdfh::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfh/utils/console.rb

Overview

All console output formats

Instance Method Summary collapse

Constructor Details

#initialize(verbose) ⇒ self



7
8
9
# File 'lib/pdfh/utils/console.rb', line 7

def initialize(verbose)
  @verbose = verbose
end

Instance Method Details

#debug(message = nil) ⇒ void

This method returns an undefined value.



12
13
14
15
16
# File 'lib/pdfh/utils/console.rb', line 12

def debug(message = nil)
  msg = message.to_s
  msg = msg.colorize(:cyan) unless msg.colorized?
  output(msg) if verbose?
end

#error_print(message, exit_app: true) ⇒ void

This method returns an undefined value.

Parameters:

  • message (String)
  • exit_app (Boolean) (defaults to: true)

    exit application if true (default)



39
40
41
42
# File 'lib/pdfh/utils/console.rb', line 39

def error_print(message, exit_app: true)
  output "Error, #{message}".colorize(:red)
  exit 1 if exit_app
end

#headline(title) ⇒ void

This method returns an undefined value.

Prints visual separator in shell for easier reading for humans ——— Title ——————————————— … ——————

Examples:

output

Parameters:



28
29
30
31
32
33
34
# File 'lib/pdfh/utils/console.rb', line 28

def headline(title)
  _, cols = console_size
  line_length = cols - (title.size + 5)
  left  = ("" * 3).to_s.red
  right = ("" * line_length).to_s.red
  output "\n#{left} #{title.colorize(color: :blue, mode: :bold)} #{right}"
end

#ident_print(field, value, color: :green, width: 3) ⇒ void

This method returns an undefined value.

Examples:

usage

ident_print("Name", "iax")
# =>     Name: "iax"


54
55
56
57
58
# File 'lib/pdfh/utils/console.rb', line 54

def ident_print(field, value, color: :green, width: 3)
  field_str = field.to_s.rjust(width)
  value_str = value.colorize(color)
  output "#{" " * 4}#{field_str}: #{value_str}"
end

#info(message) ⇒ void

This method returns an undefined value.



19
20
21
# File 'lib/pdfh/utils/console.rb', line 19

def info(message)
  output(message)
end

This method returns an undefined value.

Show options used to run the current sync job

Parameters:

  • options (Hash)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pdfh/utils/console.rb', line 63

def print_options(options) # rubocop:disable Metrics/CyclomaticComplexity
  max_size = options.keys.map(&:size).max + 3
  options.each do |key, value|
    left  = key.inspect.rjust(max_size).cyan
    right = case value
            when NilClass   then value.inspect.colorize(color: :black, mode: :bold)
            when TrueClass  then value.inspect.colorize(color: :green, mode: :bold)
            when FalseClass then value.inspect.colorize(color: :red, mode: :bold)
            when Symbol     then value.inspect.yellow
            when String     then value.inspect.light_magenta
            else
              value.inspect.red
            end
    debug "#{left} => #{right}"
  end

  nil
end

#warn_print(message) ⇒ void

This method returns an undefined value.

Parameters:



46
47
48
# File 'lib/pdfh/utils/console.rb', line 46

def warn_print(message)
  output "Warning, #{message}".colorize(:yellow)
end