module GitBundle::Console

Constants

COLORS
ExecutionResult

Public Instance Methods

clear_line() click to toggle source
# File lib/git_bundle/console.rb, line 13
def clear_line
  STDOUT.print "\r\e[0K"
  STDOUT.flush
end
parallel(items, heading_proc = nil) { |item| ... } click to toggle source
# File lib/git_bundle/console.rb, line 55
def parallel(items, heading_proc = nil)
  heading_proc ||= -> (item) { puts_repo_heading(item) }

  item_results = items.map { |item| { item: item, output: '', error: false, complete: false, thread: nil } }

  items.each_with_index do |item, index|
    results = item_results[index]
    results[:thread] = Thread.new do
      result = yield(item)
      results[:output] = result.output
      results[:error] = result.error
    end
  end

  waiting_for_items = items.map(&:name)
  puts_wait_line(waiting_for_items)

  until waiting_for_items.empty?
    item_results.each do |result|
      next if result[:complete]

      if result[:thread].join(0.1)
        result[:complete] = true
        clear_line

        heading_proc.call(result[:item])
        puts result[:output]

        waiting_for_items.delete(result[:item].name)
        puts_wait_line(waiting_for_items) unless waiting_for_items.empty?
      end
    end
  end

  puts ''
  errors = item_results.select { |item| item[:error] }.map { |item| item[:item].name }
  puts_error "Command failed in #{errors.join(', ')}." unless errors.empty?
end
puts_attention(text) click to toggle source
# File lib/git_bundle/console.rb, line 34
def puts_attention(text)
  puts colorize(text, COLORS[:attention])
end
puts_diverged_repos(repos) click to toggle source
# File lib/git_bundle/console.rb, line 26
def puts_diverged_repos(repos)
  repos.each { |repo| puts colorize("  #{repo.name} (#{repo.branch})", COLORS[:prompt], true) }
end
puts_error(text) click to toggle source
# File lib/git_bundle/console.rb, line 42
def puts_error(text)
  puts colorize(text, COLORS[:error])
end
puts_heading(text) click to toggle source
# File lib/git_bundle/console.rb, line 30
def puts_heading(text)
  puts colorize("\n=== #{text}", COLORS[:heading])
end
puts_prompt(text) click to toggle source
# File lib/git_bundle/console.rb, line 38
def puts_prompt(text)
  puts colorize(text, COLORS[:prompt])
end
puts_repo_heading(repo) click to toggle source
# File lib/git_bundle/console.rb, line 18
def puts_repo_heading(repo)
  puts colorize("\n=== #{repo.name} (#{repo.branch})", COLORS[:heading], true)
end
puts_repo_heading_switch(repo, new_branch) click to toggle source
# File lib/git_bundle/console.rb, line 22
def puts_repo_heading_switch(repo, new_branch)
  puts colorize("\n=== #{repo.name} (#{repo.branch} ⇒ #{new_branch})", COLORS[:heading], true)
end
puts_stay_on_line(text) click to toggle source
# File lib/git_bundle/console.rb, line 46
def puts_stay_on_line(text)
  STDOUT.print text
  STDOUT.flush
end
puts_wait_line(names) click to toggle source
# File lib/git_bundle/console.rb, line 51
def puts_wait_line(names)
  puts_stay_on_line "Waiting for #{names.map { |name| colorize(name, COLORS[:highlight], true) }.join(', ')}"
end

Private Instance Methods

colorize(text, color_code, bold = false) click to toggle source
# File lib/git_bundle/console.rb, line 96
def colorize(text, color_code, bold = false)
  if bold
    "\e[1m\e[#{color_code}m#{text}\e[0m"
  else
    "\e[#{color_code}m#{text}\e[0m"
  end
end