Created
January 7, 2011 05:55
-
-
Save rummelonp/769166 to your computer and use it in GitHub Desktop.
タスクをCLIから登録/Growlで通知するためのシンプルなRubyスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
## task_notify はタスクを登録・通知するためのシンプルなスクリプトです。 | |
## | |
## 使い方: task_notify [options] | |
## | |
## タスクを登録し、指定時間後にGrowlで表示することが出来ます。 | |
## $ task_notify --new-task 牛乳買いに行く,東京ストアに牛乳1L2つ買いに行く,1.hours | |
## 上記のようにコマンドを実行することで1時間後に通知するようタスクを登録することが出来ます。 | |
## 時間は 1.hour+30.minutes のように書くことが出来ます。 | |
## 上記のように指定した場合1時間30分後に登録したタスクが通知されます。 | |
## $ task_notify --show | |
## 上記のようにコマンドを実行することで未通知のタスク一覧を表示することが出来ます。 | |
## 登録されたタスクはHOMEディレクトリの.task_nofityファイルに保存されています。 | |
## 通知済みのタスクは削除されます。 | |
## | |
## 必要なもの: | |
## このスクリプトを動作させるためには | |
## RubygemsのActiveSupport | |
## Growlのコマンドラインインターフェースgrowlnotify | |
## 上記のものが必要になります。 | |
## ActiveSupportは下記のようにコマンドを実行することでインストールすることが出来ます。 | |
## $ sudo gem install activesupport | |
## growlnotifyはGrowlのイメージファイルの中にインストーラが入っています。 | |
## http://growl.info/ からダウンロードしてください。 | |
## | |
## 設定: | |
## このスクリプトをパスが通ったディレクトリに置きます。 | |
## crontab にこのスクリプトを登録します。 | |
## 例: */1 * * * * /Users/mitukiii/bin/task_notify --run > /dev/null 2>&1 | |
## 上記のように登録すると1分ごとにタスク一覧を確認し、 | |
## 通知時間が来たタスクをGrowlで表示します。 | |
## | |
require 'rubygems' | |
require 'yaml' | |
require 'active_support/core_ext/numeric/time' | |
require 'active_support/core_ext/time/calculations' | |
module TaskNotify | |
extend self | |
attr_accessor :growl, :icon, :path, :tasks | |
class ParserError < RuntimeError | |
end | |
def parse(values) | |
values = values.split(',').map(&:strip) | |
unless values.length == 3 | |
raise ParserError.new '引数の数が不正です' | |
end | |
t, m, n = values | |
{:title => t, :message => m, :notify_at => parse_time(n)} | |
end | |
def parse_time(value) | |
unless value.match(/(\d+\.(seconds?|minutes?|hours?|days?|weeks?|fortnights?)\+?)+[^\+]$/) | |
raise ParserError.new 'notify_atの形式が不正です' | |
end | |
eval(value).to_i.since | |
end | |
def load | |
@tasks = YAML.load_file(@path) rescue [] | |
end | |
def save | |
YAML.dump(@tasks, open(@path, 'w')) | |
end | |
def add(task) | |
@tasks << task | |
end | |
def run | |
now = Time.now | |
notify_tasks = @tasks.select do |t| | |
t[:notify_at] < now rescue true | |
end | |
notify_tasks.each do |t| | |
`#{@growl} '#{t[:title]}' -m '#{t[:message]}' --image #{@icon}` | |
end | |
@tasks -= notify_tasks | |
end | |
end | |
if $0 == __FILE__ | |
require 'optparse' | |
TaskNotify.path = File.join ENV['HOME'], '.task_notify' | |
TaskNotify.growl = '/usr/local/bin/growlnotify' | |
TaskNotify.icon = '/Applications/Mail.app/Contents/Resources/getNewMail.tiff' | |
def usage | |
File.readlines(__FILE__). | |
grep(/^##.*/). | |
map {|l| l.chomp[3..-1]}. | |
join("\n") | |
end | |
opt = OptionParser.new do |opt| | |
opt.banner = usage | |
opt.separator "Options" | |
run_text = '通知時間が来たタスクをGrowlで表示する' | |
opt.on('-r', '--run', run_text) do | |
TaskNotify.load | |
TaskNotify.run | |
TaskNotify.save | |
exit | |
end | |
show_text = '未通知のタスク一覧を表示する' | |
opt.on('-s', '--show', show_text) do | |
TaskNotify.load | |
if TaskNotify.tasks.empty? | |
puts '未通知のタスクはありません' | |
else | |
TaskNotify.tasks.sort_by {|t| | |
t[:notify_at] | |
}.each { |t| | |
puts t[:title] | |
puts ' ' + t[:message] | |
puts ' ' + t[:notify_at].strftime('%Y/%m/%d %H:%M') | |
puts | |
} | |
end | |
exit | |
end | |
new_task_text = '新しいタスクを追加する' | |
opt.on('-n title,message', | |
'--new-task title,message,notify_at', | |
new_task_text) do |v| | |
task = TaskNotify.parse v | |
TaskNotify.load | |
TaskNotify.add task | |
TaskNotify.save | |
puts "#{task[:title]} を #{task[:notify_at].strftime('%Y/%m/%d %H:%M')} に通知します" | |
exit | |
end | |
help_text = 'このヘルプを表示する' | |
opt.on('-h', '--help', help_text) | |
begin | |
opt.parse! ARGV | |
rescue OptionParser::MissingArgument | |
puts '引数がありません' | |
exit | |
rescue OptionParser::InvalidOption | |
puts opt.help | |
exit | |
rescue TaskNotify::ParserError | |
puts $!.message | |
exit | |
end | |
end | |
puts opt.help | |
exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment