Class: Macinbox::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/macinbox/task.rb

Class Method Summary collapse

Class Method Details

.backtick(cmd) ⇒ Object



68
69
70
# File 'lib/macinbox/task.rb', line 68

def self.backtick(cmd)
  IO.popen(cmd).read.chomp
end

.progress_bar(activity, percent_done) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/macinbox/task.rb', line 17

def self.progress_bar(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 "
  bar = ""
  if percent_done.round(0).to_i < 100
    bar_available_size = columns - header.size - 2
    bar_size = (percent_done * bar_available_size / 100.0).to_i
    bar_remainder = bar_available_size - bar_size
    bar_full = "#" * bar_size
    bar_empty = @spinner.next + " " * (bar_remainder-1) rescue ""
    bar = "[" + bar_full + bar_empty + "]"
  end
  header + bar
end

.run(cmd) ⇒ Object



9
10
11
# File 'lib/macinbox/task.rb', line 9

def self.run(cmd)
  system(*cmd) or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$? >> 8}")
end

.run_as_sudo_user(cmd) ⇒ Object



13
14
15
# File 'lib/macinbox/task.rb', line 13

def self.run_as_sudo_user(cmd)
  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



72
73
74
75
76
77
# File 'lib/macinbox/task.rb', line 72

def self.run_with_input(cmd)
  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



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/macinbox/task.rb', line 33

def self.run_with_progress(activity, cmd, opts={})
  STDERR.print TTY::Cursor::INVISIBLE
  STDERR.print TTY::Line::CLEAR + TTY::Color::GREEN + progress_bar(activity, 0.0) + TTY::Color::RESET
  IO.popen cmd, opts do |pipe|
    pipe.each_line do |line|
      percent = yield line
      STDERR.print TTY::Line::CLEAR + TTY::Color::GREEN + progress_bar(activity, percent) + TTY::Color::RESET if percent
    end
  end
  STDERR.puts TTY::Cursor::NORMAL
end

.write_file_to_io_with_progress(source, destination) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/macinbox/task.rb', line 45

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.print TTY::Line::CLEAR + TTY::Color::GREEN + progress_bar(activity, 0.0) + TTY::Color::RESET
  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.print TTY::Line::CLEAR + TTY::Color::GREEN + progress_bar(activity, percent_done) + TTY::Color::RESET
      rescue EOFError
        eof = true
      end
    end
  end
  STDERR.puts TTY::Cursor::NORMAL
end