Module: Satorix::Shared::Console

Instance Method Summary collapse

Instance Method Details

#colorize(text, color = nil) ⇒ Object



25
26
27
# File 'lib/satorix/shared/console.rb', line 25

def colorize(text, color = nil)
  "\033[#{ colors[color] }m#{ text }\033[0m"
end

#colorsObject



14
15
16
17
18
19
20
21
22
# File 'lib/satorix/shared/console.rb', line 14

def colors
  Hash.new(39).merge(red: '0;31', light_red: '1;31',
                     green: '0;32', light_green: '1;32',
                     yellow: '0;33', light_yellow: '1;33',
                     blue: '0;34', light_blue: '1;34',
                     magenta: '0;35', light_magenta: '1;35',
                     cyan: '0;36', light_cyan: '1;36',
                     white: '0;37', light_white: '1;37')
end

#humanize_time(seconds) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/satorix/shared/console.rb', line 69

def humanize_time(seconds)
  return 'less than 1 second' if seconds < 1
  [[60, :second], [60, :minute], [24, :hour], [1000, :day]].map do |count, name|
    if seconds > 0
      seconds, n = seconds.divmod(count)
      n = n.to_i
      "#{ n } #{ name }#{ 's' if n > 1 }" if n > 0
    end
  end.compact.reverse.join(' ')
end

#log(text, color = nil) ⇒ Object



30
31
32
# File 'lib/satorix/shared/console.rb', line 30

def log(text, color = nil)
  puts color ? colorize(text, color) : text
end

#log_bench(message) ⇒ Object



61
62
63
64
65
66
# File 'lib/satorix/shared/console.rb', line 61

def log_bench(message)
  log_header message
  result = nil
  log_duration "Time elapsed: #{ humanize_time Benchmark.realtime { result = yield } }."
  result
end

#log_command(text) ⇒ Object



35
36
37
# File 'lib/satorix/shared/console.rb', line 35

def log_command(text)
  log text, :cyan
end

#log_duration(text) ⇒ Object



40
41
42
# File 'lib/satorix/shared/console.rb', line 40

def log_duration(text)
  log text, :light_cyan
end

#log_error(text) ⇒ Object



50
51
52
# File 'lib/satorix/shared/console.rb', line 50

def log_error(text)
  log text, :light_red
end

#log_error_and_abort(text) ⇒ Object



55
56
57
58
# File 'lib/satorix/shared/console.rb', line 55

def log_error_and_abort(text)
  log_error text
  abort text
end

#log_header(text) ⇒ Object



45
46
47
# File 'lib/satorix/shared/console.rb', line 45

def log_header(text)
  log("\n#{ text }", :light_green)
end

#run_command(command, filtered_text: [], quiet: false) ⇒ Object

This method is used EVERYWHERE. Seemingly small changes can have far-ranging and extreme consequences. Modify only with extreme caution.

Note: Many applications (like echo and the Flynn CLI) append a newline to their output.

If you are using the command result, it may be desirable to chomp the trailing
newline. It is left to the implementing call to handle this, as it proved
cumbersome to handle in this method in a way that worked perfectly in all cases.


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/satorix/shared/console.rb', line 89

def run_command(command, filtered_text: [], quiet: false)
  command = command.shelljoin if command.is_a?(Array)
  logged_command = logged_command(command, filtered_text)
  log_command(logged_command) unless quiet

  ''.tap do |output|
    IO.popen(bash(command)) do |io|
      until io.eof?
        line = io.gets
        puts line unless quiet || line.empty?
        $stdout.flush
        output.concat line
      end
    end

    handle_run_command_error(logged_command, quiet) unless $CHILD_STATUS.success?
  end
end

#source_env_from(file) ⇒ Object

stackoverflow.com/questions/1197224/source-shell-script-into-environment-within-a-ruby-script TODO : reduce / consolidate? Find variables changed as a result of sourcing the given file, and update in ENV.



112
113
114
# File 'lib/satorix/shared/console.rb', line 112

def source_env_from(file)
  bash_source(file).each { |k, v| ENV[k] = v }
end