Method: Blufin::Terminal.execute
- Defined in:
- lib/core/terminal.rb
.execute(command, path = nil, capture: false, verbose: true, text: nil, error_output: ERR_OUTPUT, display_error: true, ignore_error: false) ⇒ Object
Executes a command and shows that something is happening (via a cli-spinner). If capture: is false, returns TRUE:FALSE whether command was successful or not. If capture: is true, returns raw output of command. See: github.com/piotrmurach/tty-spinner/blob/master/lib/tty/spinner/formats.rb (for spinner options).
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/core/terminal.rb', line 83 def self.execute(command, path = nil, capture: false, verbose: true, text: nil, error_output: ERR_OUTPUT, display_error: true, ignore_error: false) text = text.is_a?(String) ? text : command t1 = Time.now spinner = TTY::Spinner.new("[:spinner] \x1B[38;5;208m#{text}#{!path.nil? ? " \x1B[38;5;246m\xe2\x86\x92 \x1B[38;5;240m#{path}" : nil}\x1B[0m", format: :dots) if verbose spinner.auto_spin if verbose path = File.('~/') if path.nil? path = File.(path) unless path.nil? `echo '' > #{ERR_OUTPUT}` if capture res = `cd #{path} && #{command} 2>#{error_output}` else res = system("cd #{path} && #{command} 1>/dev/null 2>#{error_output}") end t2 = Time.now delta = "#{'%.3f' % (t2 - t1).abs}s" error = `cat #{ERR_OUTPUT}`.to_s.strip if (res && error.length == 0) || ignore_error spinner.success("\x1B[38;5;246m\xe2\x86\x92 \x1B[38;5;46mComplete \x1B[38;5;240m(#{delta})\x1B[0m\x1B[0m") if verbose res = true unless capture else spinner.error("\x1B[38;5;246m\xe2\x86\x92 \x1B[38;5;196mFailed (#{delta})\x1B[0m") if verbose # If there is an error, output it (even if verbose == false). if error.length > 0 && display_error puts "\x1B[38;5;240m" system("cat #{ERR_OUTPUT}") puts "\x1B[0m" end res = false unless capture end error = nil if error.nil? || error.strip == '' return res, error end |