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
- #run(cmd) ⇒ Object
- #run_all ⇒ Object
- #start ⇒ Object
- #wait_for_changes ⇒ Object
Constructor Details
#initialize(config) ⇒ UI
Returns a new instance of UI.
7 8 9 10 11 12 13 |
# File 'lib/spectator/ui.rb', line 7 def initialize(config) @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.
39 40 41 |
# File 'lib/spectator/ui.rb', line 39 def interrupted_status @interrupted_status end |
#status ⇒ Object
Returns the value of attribute status.
28 29 30 |
# File 'lib/spectator/ui.rb', line 28 def status @status end |
Instance Method Details
#<<(event) ⇒ Object
15 16 17 18 |
# File 'lib/spectator/ui.rb', line 15 def << event @queue << event p event end |
#ask(question) ⇒ Object
68 69 70 71 72 |
# File 'lib/spectator/ui.rb', line 68 def ask question print question.yellow $stdout.flush STDIN.gets.chomp.strip.downcase end |
#ask_what_to_do ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/spectator/ui.rb', line 45 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
91 92 93 94 |
# File 'lib/spectator/ui.rb', line 91 def can_run_specs? p [:can_run_specs?, status] status == :waiting_for_changes end |
#exit ⇒ Object
74 75 76 77 78 |
# File 'lib/spectator/ui.rb', line 74 def exit self.status = :exiting puts '--- Exiting...'.white super end |
#interrupt! ⇒ Object
34 35 36 37 |
# File 'lib/spectator/ui.rb', line 34 def interrupt! self.interrupted_status = status self << :interrupt end |
#noop ⇒ Object
30 31 32 |
# File 'lib/spectator/ui.rb', line 30 def noop @noop ||= lambda {} end |
#on(event, &block) ⇒ Object
41 42 43 |
# File 'lib/spectator/ui.rb', line 41 def on event, &block @callbacks[event] = block end |
#run(cmd) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/spectator/ui.rb', line 80 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
57 58 59 60 |
# File 'lib/spectator/ui.rb', line 57 def run_all self.status = :waiting_for_changes self << :run_all end |
#start ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/spectator/ui.rb', line 20 def start wait_for_changes thread = Thread.new { event_loop } p thread Thread.pass sleep end |
#wait_for_changes ⇒ Object
62 63 64 65 66 |
# File 'lib/spectator/ui.rb', line 62 def wait_for_changes return if status == :waiting_for_changes self.status = :waiting_for_changes puts '--- Waiting for changes...'.cyan end |