ytodo/task.rb

97 lines
3.2 KiB
Ruby
Raw Normal View History

2009-05-18 09:39:17 +00:00
#!/usr/bin/env ruby
#
require 'taskTime.rb'
require 'contact.rb'
2009-05-18 09:39:17 +00:00
class Task
2009-06-02 15:43:28 +00:00
def initialize(raw_input)
2009-05-18 09:39:17 +00:00
@notes=[]
@contacts=[]
@contexts=[]
@projects=[]
2009-05-22 14:45:53 +00:00
@tags=[]
@priority=0
2009-05-18 09:39:17 +00:00
@dates=TaskTime.new()
2009-06-02 15:43:28 +00:00
from_s(raw_input)
2009-05-18 09:39:17 +00:00
end
def to_s
2009-06-02 15:43:28 +00:00
res=@description
if (@contexts.length>0): res+=' ' + @contexts.map { |x| x.to_s }.join(" ") end
if (@projects.length>0): res+=' ' + @projects.map { |x| '['+x.to_s+']' }.join(" ") end
if (@priority != 0): res+=' /'+ @priority.to_s + '\\' end
if (@notes.length>0): res+=' ' + @notes.map { |x| '('+x.to_s+')' }.join(" ") end
if (@contacts.length>0): res+=' ' + @contacts.map { |x| x.to_s }.join(" ") end
if (@dates): res+=' ' + @dates.to_s end
if (@tags.length>0): res+=' ' + '{' + @tags.map{ |x| x.to_s }.join(", ") + '}' end
return res
end
def to_detailled_s
res='desc: '+@description
if (@contexts.length>0):
res+="\n contexts:" + @contexts.map { |x| x.to_s }.join(" ")
end
if (@projects.length>0):
res+="\n projects:" + @projects.map { |x| '['+x.to_s+']' }.join(" ")
end
if (@priority != 0):
res+="\n priority: " + @priority.to_s
end
if (@notes.length>0):
res+="\n notes : " + @notes.map { |x| '('+x.to_s+')' }.join(" ")
end
if (@contacts.length>0):
res+="\n contacts:" + @contacts.map { |x| x.to_s }.join(" ")
end
if (@dates):
res+="\n dates :\n" + @dates.to_detailled_s
end
if (@tags.length>0):
res+="\n tags :" + '{' + @tags.map{ |x| x.to_s }.join(", ") + '}'
end
2009-06-02 15:43:28 +00:00
return res
end
2009-05-22 14:45:53 +00:00
# -- constant class variable for each part
# -- of the regular expressions
2009-05-22 14:45:53 +00:00
# Regular Expressions for that class
@@StdTokenRegExp=Regexp.new(%{(\\w+|"[^"]*")})
2009-05-22 14:45:53 +00:00
# Context
@@ContextsRegExp=Regexp.new(%{ @#{@@StdTokenRegExp.inspect[1..-2]}})
2009-05-22 14:45:53 +00:00
# Project
@@ProjectsRegExp=Regexp.new(%{\\[#{@@StdTokenRegExp.inspect[1..-2]}\\]})
2009-05-22 14:45:53 +00:00
# Contact
@@ContactsRegExp=Regexp.new(%{ (c|contact):#{@@StdTokenRegExp.inspect[1..-2]}})
2009-05-22 14:45:53 +00:00
# Notes
@@NotesRegExp=Regexp.new(%{\\(([^\)]*)\\)})
2009-05-22 14:45:53 +00:00
def from_s( raw_input )
2009-06-02 15:43:28 +00:00
@contexts=raw_input.scan(@@ContextsRegExp).map{ |x| x[0] }
@projects=raw_input.scan(@@ProjectsRegExp).map{ |x| x[0] }
@contacts=raw_input.scan(@@ContactsRegExp).map{ |x| x[1] }
@notes =raw_input.scan( @@NotesRegExp).map{ |x| x[0] }
2009-05-22 14:45:53 +00:00
# somehow special for the priority
2009-06-02 15:43:28 +00:00
@priority= raw_input.scan( /!/ ).length - raw_input.scan( /\?/ ).length
2009-05-22 14:45:53 +00:00
@description=raw_input.gsub(
2009-06-02 15:43:28 +00:00
Regexp.union(@@ContextsRegExp, @@ProjectsRegExp,
@@ContactsRegExp, @@NotesRegExp,
/!/, /\?/, @dates.regexp),"")
@dates=TaskTime.new(raw_input)
end
2009-05-18 09:39:17 +00:00
end
2009-06-02 15:43:28 +00:00
task = Task.new("")
while true:
print "> "
task.from_s( STDIN.gets.chomp )
print "------------\n"
print task.to_s
print "\n------------\n"
print task.to_detailled_s
print "\n------------\n"
end