Class: Macinbox::Task
- Inherits:
-
Object
- Object
- Macinbox::Task
- Defined in:
- lib/macinbox/task.rb
Class Method Summary collapse
- .backtick(cmd) ⇒ Object
- .print_progress_bar(io, activity, percent_done) ⇒ Object
- .progress_bar(activity, percent_done) ⇒ Object
- .run(cmd) ⇒ Object
- .run_as_sudo_user(cmd) ⇒ Object
- .run_with_input(cmd) ⇒ Object
- .run_with_progress(activity, cmd, opts = {}) ⇒ Object
- .write_file_to_io_with_progress(source, destination) ⇒ Object
Class Method Details
.backtick(cmd) ⇒ Object
77 78 79 80 |
# File 'lib/macinbox/task.rb', line 77 def self.backtick(cmd) Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose IO.popen(cmd).read.chomp end |
.print_progress_bar(io, activity, percent_done) ⇒ Object
37 38 39 |
# File 'lib/macinbox/task.rb', line 37 def self.(io, activity, percent_done) io.print TTY::Line::CLEAR + TTY::Color::GREEN + (activity, percent_done) + TTY::Color::RESET if io.isatty end |
.progress_bar(activity, percent_done) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/macinbox/task.rb', line 21 def self.(activity, percent_done) @spinner ||= Enumerator.new { |e| loop { e.yield '|'; e.yield '/'; e.yield '-'; e.yield '\\' } } columns = STDOUT.winsize[1] - 8 header = activity + ": " + percent_done.round(0).to_s + "% done " = "" if percent_done.round(0).to_i < 100 = columns - header.size - 2 = (percent_done * / 100.0).to_i = - = "#" * = @spinner.next + " " * (-1) rescue "" = "[" + + + "]" end header + end |
.run(cmd) ⇒ Object
11 12 13 14 |
# File 'lib/macinbox/task.rb', line 11 def self.run(cmd) Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose system(*cmd) or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$? >> 8}") end |
.run_as_sudo_user(cmd) ⇒ Object
16 17 18 19 |
# File 'lib/macinbox/task.rb', line 16 def self.run_as_sudo_user(cmd) Logger.info "Running command: sudo -u #{ENV["SUDO_USER"]} #{Shellwords.join(cmd)}" if $verbose system "sudo", "-u", ENV["SUDO_USER"], *cmd or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$?.to_i}") end |
.run_with_input(cmd) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/macinbox/task.rb', line 82 def self.run_with_input(cmd) Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose IO.popen(cmd, "w") do |pipe| yield pipe end $? == 0 or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$?.to_i}") end |
.run_with_progress(activity, cmd, opts = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/macinbox/task.rb', line 41 def self.run_with_progress(activity, cmd, opts={}) STDERR.print TTY::Cursor::INVISIBLE (STDERR, activity, 0.0) Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose IO.popen cmd, opts do |pipe| pipe.each_line do |line| percent = yield line (STDERR, activity, percent) if percent end end STDERR.puts TTY::Cursor::NORMAL end |
.write_file_to_io_with_progress(source, destination) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/macinbox/task.rb', line 54 def self.write_file_to_io_with_progress(source, destination) activity = Logger.prefix + File.basename(source) eof = false bytes_written = 0 total_size = File.size(source) last_percent_done = -1 STDERR.print TTY::Cursor::INVISIBLE (STDERR, activity, 0.0) File.open(source) do |file| until eof begin bytes_written += destination.write(file.readpartial(1024*1024)) percent_done = ((bytes_written.to_f / total_size.to_f) * 100).round(1) last_percent_done = percent_done (STDERR, activity, percent_done) rescue EOFError eof = true end end end STDERR.puts TTY::Cursor::NORMAL end |