Class: WatchmonkeyCli::Platypus

Inherits:
Object
  • Object
show all
Defined in:
lib/watchmonkey_cli/hooks/platypus.rb

Class Method Summary collapse

Class Method Details

.hook!(app, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/watchmonkey_cli/hooks/platypus.rb', line 3

def self.hook!(app, opts = {})
  opts = opts.reverse_merge(notifications: true)
  app.instance_eval do
    # send errors via notification center
    hook :result_dump do |robj, args, checker|
      if robj.error?
        robj.messages.each do |m|
          msg  = "#{robj.args[0].try(:name) || robj.args[0].presence || "?"}: #{m}"

          case opts[:notifications]
          when 1
            # makes no sound
            fmsg = msg.gsub('"', '\"').gsub("'", %{'"'"'})
            `osascript -e 'display notification "#{fmsg}" with title "WatchMonkey"'`
          when 2
            # makes a sound
            sync { puts "NOTIFICATION:#{msg}" }
          end
        end
      end
    end if opts[:notifications]

    hook :wm_work_start, :wm_work_end do
      # mastermind calculation I swear :D (<-- no idea what I did here)
      # sync { puts "PROGRESS:#{((@[email protected]{|t| t[:working] }.length.to_d) / @threads.length * 100).to_i}" }
      sync do
        active = @threads.select{|t| t[:working] }.length
        total  = @threads.select{|t| t[:working] }.length + @queue.length
        perc   = total.zero? ? 100 : (active.to_d / total * 100).to_i
        puts "PROGRESS:#{perc}"
      end
    end
  end
end