Module: GitBundle::Console
- Included in:
- CLI, GitBundle::Commands::Checkout, GitBundle::Commands::Generic, GitBundle::Commands::Push, GitBundle::Commands::Version
- Defined in:
- lib/git_bundle/console.rb
Defined Under Namespace
Classes: ExecutionResult
Constant Summary collapse
- COLORS =
{ error: 31, attention: 32, prompt: 33, heading: 34, highlight: 36 }
Instance Method Summary collapse
- #clear_line ⇒ Object
- #parallel(items, heading_proc = nil) ⇒ Object
- #puts_attention(text) ⇒ Object
- #puts_error(text) ⇒ Object
- #puts_heading(text) ⇒ Object
- #puts_prompt(text) ⇒ Object
- #puts_repo_heading(repo) ⇒ Object
- #puts_repo_heading_switch(repo, new_branch) ⇒ Object
- #puts_stay_on_line(text) ⇒ Object
- #puts_wait_line(names) ⇒ Object
Instance Method Details
#clear_line ⇒ Object
13 14 15 16 |
# File 'lib/git_bundle/console.rb', line 13 def clear_line STDOUT.print "\r\e[0K" STDOUT.flush end |
#parallel(items, heading_proc = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/git_bundle/console.rb', line 51 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) ⇒ Object
30 31 32 |
# File 'lib/git_bundle/console.rb', line 30 def puts_attention(text) puts colorize(text, COLORS[:attention]) end |
#puts_error(text) ⇒ Object
38 39 40 |
# File 'lib/git_bundle/console.rb', line 38 def puts_error(text) puts colorize(text, COLORS[:error]) end |
#puts_heading(text) ⇒ Object
26 27 28 |
# File 'lib/git_bundle/console.rb', line 26 def puts_heading(text) puts colorize("\n=== #{text}", COLORS[:heading]) end |
#puts_prompt(text) ⇒ Object
34 35 36 |
# File 'lib/git_bundle/console.rb', line 34 def puts_prompt(text) puts colorize(text, COLORS[:prompt]) end |
#puts_repo_heading(repo) ⇒ Object
18 19 20 |
# 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) ⇒ Object
22 23 24 |
# 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) ⇒ Object
42 43 44 45 |
# File 'lib/git_bundle/console.rb', line 42 def puts_stay_on_line(text) STDOUT.print text STDOUT.flush end |
#puts_wait_line(names) ⇒ Object
47 48 49 |
# File 'lib/git_bundle/console.rb', line 47 def puts_wait_line(names) puts_stay_on_line "Waiting for #{names.map { |name| colorize(name, COLORS[:highlight], true) }.join(', ')}" end |