Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 82 lines (64 sloc) 1.99 KB
#!/usr/bin/env ruby
require 'cocoa'
require 'socket'
$pomodoro_text = 'N/A'
$server_thread = Thread.new do
File.unlink '/tmp/pomodoro.sock'
UNIXServer.open '/tmp/pomodoro.sock' do |server|
while client = server.accept
client.puts "#{$pomodoro_text}"
client.close
end
end
end
NSStatusBar = Cocoa::NSStatusBar
class NSStatusBar
attach_method :_insertStatusItem, :args=>2, :names=>[:withPriority], :types=>["@", "i"], :retval=>"v"
end
class MyAppDelegate < Cocoa::NSObject
def applicationDidFinishLaunching(notification)
menu = Cocoa::NSMenu.new
item = menu.addItemWithTitle("Void", action: :void, keyEquivalent: "")
item.setTarget(self)
statusBar = NSStatusBar.systemStatusBar
# thanks tunnelblick!! (from MenuController.m)
@item = item = statusBar.statusItemWithLength(0).retain
statusBar.removeStatusItem(item)
statusBar._insertStatusItem(item, withPriority:2147483646)
item.setLength(0)
item.setMenu(menu)
item.setHighlightMode(true)
item.setLength(Cocoa::NSVariableStatusItemLength)
time = 25 * 60
if ARGV.length == 1
time = ARGV[0].to_i * 60
elsif ARGV.length == 2
time = ARGV[0].to_i * 60 + ARGV[1].to_i
end
item.setTitle("...")
@finish = Time.now + Rational(time)
Cocoa::NSTimer.scheduledTimerWithTimeInterval(1.0, target:self, selector: :updateTime, userInfo:nil, repeats:true)
end
def void(sender)
# Cocoa gem prevents exit from working correctly, hence this hack.
$server_thread.exit
exec "false"
end
def updateTime(sender)
update
end
def update
left = (@finish - Time.now).to_i
$pomodoro_text = "%d:%02d" % [ left / 60, left % 60 ]
@item.setTitle($pomodoro_text)
if left < 0
# Cocoa gem prevents exit from working correctly, hence this hack.
$server_thread.exit
exec "true"
end
end
end
Cocoa::NSAutoreleasePool.new
$app = Cocoa::NSApplication.sharedApplication
$app.setDelegate(MyAppDelegate.alloc.init.autorelease)
$app.run