Gestion sauvegarde + Dates en verbose

sauvegarde dans le fichier todolist.rb.tasks
    chargement du fichier todolist.rb.tasks (bug des [XX] ... )
        débuts de ligne
    dates en verboses created:"Fri Jui ... "
                      started:"Fri Jui ... "
                      ...
This commit is contained in:
Yann Esposito 2009-06-05 15:59:33 +02:00
parent e1c4dfe3b0
commit 98b593dcdf
3 changed files with 105 additions and 19 deletions

View file

@ -65,6 +65,8 @@ class Task
@@ContactsRegExp=Regexp.new(%{ (c|contact):#{@@StdTokenRegExp.inspect[1..-2]}})
# Notes
@@NotesRegExp=Regexp.new(%{\\(([^\)]*)\\)})
# Tags
@@TagsRegExp=Regexp.new(%{\\{#{@@StdTokenRegExp.inspect[1..-2]}\\}})
def from_s( raw_input )
@ -72,6 +74,7 @@ class Task
@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] }
@tags =raw_input.scan( @@TagsRegExp).map{ |x| x[0] }
# somehow special for the priority
@priority= raw_input.scan( /!/ ).length - raw_input.scan( /\?/ ).length
@ -84,6 +87,7 @@ class Task
end
end
if __FILE__ == $0:
task = Task.new("")
while true:
print "> "
@ -94,3 +98,4 @@ while true:
print task.to_detailled_s
print "\n------------\n"
end
end

View file

@ -3,6 +3,7 @@
# -- pour la lecture de la date à partir du language naturel
require 'rubygems'
require 'chronic'
require 'time'
class TaskTime
@creation_date # creation date of the task
@ -19,16 +20,18 @@ class TaskTime
# Regular Expressions for that class
@@StdTokenRegExp=Regexp.new(%{(\\w+|"[^"]*")})
# Notes
@@Time=Regexp.new(%{#(#{@@StdTokenRegExp.inspect[1..-2]}(,|->))?#{@@StdTokenRegExp.inspect[1..-2]}})
@@TimeShort=Regexp.new(%{#(#{@@StdTokenRegExp.inspect[1..-2]}(,|->))?#{@@StdTokenRegExp.inspect[1..-2]}})
@@TimeVerbose=Regexp.new(%{ (created|done|due|duration|max_duration|min_duration):(#{@@StdTokenRegExp.inspect[1..-2]})} )
@@Time=Regexp.union(@@TimeShort,@@TimeVerbose)
def to_s
res=%{created:#{@creation_date}}
if ( @start_date ) : res+=%{,started:#{@start_date}} end
if ( @done_date ) : res+=%{,done:#{@done_date}} end
if ( @due_date ) : res+=%{,due:#{@due_date}} end
if ( @duration ) : res+=%{,duration:#{@duration}} end
if ( @max_duration ): res+=%{,max_duration:#{@duration}} end
if ( @min_duration ): res+=%{,min_duration:#{@duration}} end
res=%{created:"#{@creation_date}"}
if ( @start_date ) : res+=%{,started:"#{@start_date}"} end
if ( @done_date ) : res+=%{,done:"#{@done_date}"} end
if ( @due_date ) : res+=%{,due:"#{@due_date}"} end
if ( @duration ) : res+=%{,duration:"#{@duration}"} end
if ( @max_duration ): res+=%{,max_duration:"#{@duration}"} end
if ( @min_duration ): res+=%{,min_duration:"#{@duration}"} end
return res
end
@ -44,11 +47,20 @@ class TaskTime
end
def from_s( raw_input )
scanned_input=raw_input.scan( @@Time )
scanned_input=raw_input.scan( @@TimeShort )
str_of_start_date = scanned_input.map{ |x| x[3] }
@due_date = Chronic.parse(str_of_start_date)
str_of_due_date = scanned_input.map{ |x| x[1] }
@start_date = Chronic.parse(str_of_due_date)
raw_input.scan( @@TimeVerbose ).each do |x|
timeValue=Time.parse(x[1])
if ! timeValue:
timeValue=Chronic.parse(x[1])
end
puts %{@#{x[0]}=timeValue}
eval %{@#{x[0]}=timeValue}
end
end
def initialize ( raw_input=nil )

View file

@ -1,10 +1,79 @@
#!/usr/bin/env ruby
require "task.rb"
class TodoList
def initialize()
@todoList=[]
end
def addTask(task)
@todoList.append(task)
@todoList << task
end
def to_s
res=""
i=1
@todoList.each do |x|
res <<= '[' + i.to_s + '] ' + x.to_s + "\n"
i+=1
end
res
end
def save(filename)
f=File.open(filename, 'w')
f.write( to_s() )
end
def load(filename)
begin
f=File.open(filename, 'r')
while (line=f.readline)
puts "ajout de "+line
addTask( Task.new( line ) )
end
rescue Errno::ENOENT
puts "no such file #{filename}"
rescue IOError => e
puts e.exception
rescue EOFError
f.close
end
end
end
if __FILE__ == $0:
todoList=TodoList.new
defaultTaskFile=$0+".tasks"
print defaultTaskFile+"\n"
todoList.load defaultTaskFile
while true:
print "> "
entry=STDIN.gets.chomp
case entry
when /^(a|\+|add) / # ça commence par 'a ' '+ ' ou 'add '
todoList.addTask( Task.new(entry.sub(/^(a|\+|add) /,"")) )
when /^[@]/
todoList.addTask( Task.new(entry) )
when /^(l|list)( ?(\d*))?/
if $3.length>0: print "number "+$3 end
print todoList.to_s
when /^(s|save)( (.*))?/
if $3 and $3.length>0:
filename=$3
else
filename=defaultTaskFile
end
puts "saving to " + filename
todoList.save filename
when /^(load|=>) (.*)/
if $2.length>0:
filename = $2
else
filename = defaultTaskFile
end
todoList.load filename
when /^quit$/
break
else
print "/!\\ Commande inconnue /!\\\n"
end
end
end