Class: Spectator::UI
- Inherits:
-
Object
- Object
- Spectator::UI
- Defined in:
- lib/spectator/ui.rb
Instance Attribute Summary collapse
-
#interrupted_status ⇒ Object
Returns the value of attribute interrupted_status.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #<<(event) ⇒ Object
- #ask(question) ⇒ Object
- #ask_what_to_do ⇒ Object
- #can_run_specs? ⇒ Boolean
- #exit ⇒ Object
-
#initialize(config) ⇒ UI
constructor
A new instance of UI.
- #interrupt! ⇒ Object
- #noop ⇒ Object
- #on(event, &block) ⇒ Object
- #on_status(status, &block) ⇒ Object
- #run(cmd) ⇒ Object
- #run_all ⇒ Object
- #start ⇒ Object
- #terminal_columns ⇒ Object
- #wait_for_changes ⇒ Object
Constructor Details
#initialize(config) ⇒ UI
Returns a new instance of UI.
7 8 9 10 11 12 13 14 |
# File 'lib/spectator/ui.rb', line 7 def initialize(config) @mutex = Mutex.new @status_mutex = Mutex.new @status = nil @status_callbacks = {} @callbacks = {} @queue = Queue.new end |
Instance Attribute Details
#interrupted_status ⇒ Object
Returns the value of attribute interrupted_status.
56 57 58 |
# File 'lib/spectator/ui.rb', line 56 def interrupted_status @interrupted_status end |
#status ⇒ Object
Returns the value of attribute status.
38 39 40 |
# File 'lib/spectator/ui.rb', line 38 def status @status end |
Instance Method Details
#<<(event) ⇒ Object
16 17 18 19 |
# File 'lib/spectator/ui.rb', line 16 def << event @queue << event p event end |
#ask(question) ⇒ Object
89 90 91 92 93 |
# File 'lib/spectator/ui.rb', line 89 def ask question print question.yellow $stdout.flush STDIN.gets.chomp.strip.downcase end |
#ask_what_to_do ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/spectator/ui.rb', line 66 def ask_what_to_do self.status = :wait_for_input answer = ask('--- What to do now? (q=quit, a=all-specs): ') case answer when 'q' then exit when 'a' then run_all else puts '--- Bad input, ignored.'.yellow wait_for_changes end end |
#can_run_specs? ⇒ Boolean
117 118 119 120 |
# File 'lib/spectator/ui.rb', line 117 def can_run_specs? p [:can_run_specs?, status] status == :waiting_for_changes end |
#exit ⇒ Object
95 96 97 98 99 |
# File 'lib/spectator/ui.rb', line 95 def exit self.status = :exiting puts '--- Exiting...'.white super end |
#interrupt! ⇒ Object
51 52 53 54 |
# File 'lib/spectator/ui.rb', line 51 def interrupt! self.interrupted_status = status self << :interrupt end |
#noop ⇒ Object
47 48 49 |
# File 'lib/spectator/ui.rb', line 47 def noop @noop ||= lambda {} end |
#on(event, &block) ⇒ Object
62 63 64 |
# File 'lib/spectator/ui.rb', line 62 def on event, &block @callbacks[event] = block end |
#on_status(status, &block) ⇒ Object
58 59 60 |
# File 'lib/spectator/ui.rb', line 58 def on_status status, &block @status_callbacks[status] = block end |
#run(cmd) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/spectator/ui.rb', line 106 def run cmd $running = true start = Time.now puts "=== running: #{cmd} ".ljust(terminal_columns, '=').cyan success = system cmd puts "=== time: #{(Time.now - start).to_i} seconds ".ljust(terminal_columns, '=').cyan success ensure $running = false end |
#run_all ⇒ Object
78 79 80 81 |
# File 'lib/spectator/ui.rb', line 78 def run_all self.status = :waiting_for_changes self << :run_all end |
#start ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/spectator/ui.rb', line 21 def start queue = @queue mutex = @mutex thread = Thread.new do loop do wait_for_changes event = queue.pop p [:queue, event] mutex.synchronize { @callbacks[event].call } end end p thread Thread.pass sleep end |
#terminal_columns ⇒ Object
101 102 103 104 |
# File 'lib/spectator/ui.rb', line 101 def terminal_columns cols = `stty -a 2>&1`.scan(/ (\d+) columns/).flatten.first $?.success? ? cols.to_i : 80 end |
#wait_for_changes ⇒ Object
83 84 85 86 87 |
# File 'lib/spectator/ui.rb', line 83 def wait_for_changes return if status == :waiting_for_changes self.status = :waiting_for_changes puts '--- Waiting for changes...'.cyan end |