Module: Scripto::PrintCommands

Included in:
Scripto, Main
Defined in:
lib/scripto/print_commands.rb

Constant Summary collapse

RESET =
"\e[0m".freeze
GREEN =
"\e[1;37;42m".freeze
YELLOW =
"\e[1;37;43m".freeze
RED =
"\e[1;37;41m".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/scripto/print_commands.rb', line 8

def verbose
  @verbose
end

Instance Method Details

Print a colored banner to $stderr in green.



37
38
39
40
41
# File 'lib/scripto/print_commands.rb', line 37

def banner(str, color: GREEN)
  now = Time.new.strftime('%H:%M:%S')
  s = "#{str} ".ljust(72, ' ')
  $stderr.puts "#{color}[#{now}] #{s}#{RESET}"
end

#fatal(str) ⇒ Object

Print a red error banner to $stderr, then exit.



49
50
51
52
# File 'lib/scripto/print_commands.rb', line 49

def fatal(str)
  banner(str, color: RED)
  exit(1)
end

#vbanner(str = nil) ⇒ Object

Print a colored banner to $stderr, but only if #verbose?.



22
23
24
# File 'lib/scripto/print_commands.rb', line 22

def vbanner(str = nil)
  banner(str) if verbose?
end

#verbose!Object

Turn on verbose mode. #vbanner, #vputs and #vprintf will start printing now, and file ops will be printed too.



17
18
19
# File 'lib/scripto/print_commands.rb', line 17

def verbose!
  @verbose = true
end

#verbose?Boolean

Is verbose mode turned on?

Returns:

  • (Boolean)


11
12
13
# File 'lib/scripto/print_commands.rb', line 11

def verbose?
  !!@verbose
end

#vprintf(str, *args) ⇒ Object

Printf to $stderr, but only if #verbose?.



32
33
34
# File 'lib/scripto/print_commands.rb', line 32

def vprintf(str, *args)
  $stderr.printf(str, *args) if verbose?
end

#vputs(str = nil) ⇒ Object

Puts to $stderr, but only if #verbose?.



27
28
29
# File 'lib/scripto/print_commands.rb', line 27

def vputs(str = nil)
  $stderr.puts(str) if verbose?
end

#warning(str) ⇒ Object

Print a yellow warning banner to $stderr.



44
45
46
# File 'lib/scripto/print_commands.rb', line 44

def warning(str)
  banner("Warning: #{str}", color: YELLOW)
end