class Tot::TodoManager
Public Class Methods
new()
click to toggle source
# File lib/tot.rb, line 30 def initialize load_file end
Public Instance Methods
add(new_todo)
click to toggle source
# File lib/tot.rb, line 50 def add(new_todo) @tasks.push new_todo end
delete_at(at)
click to toggle source
# File lib/tot.rb, line 62 def delete_at(at) @tasks.delete_at at end
delete_by_title(title)
click to toggle source
# File lib/tot.rb, line 66 def delete_by_title(title) @tasks.delete_at(@tasks.find_index{|obj| obj['title'] == title}) @tasks end
each() { |todo| ... }
click to toggle source
# File lib/tot.rb, line 54 def each @tasks = load_file @tasks.each do |todo| yield todo end self end
find_all!(&block)
click to toggle source
# File lib/tot.rb, line 71 def find_all!(&block) @tasks = self.find_all(&block) end
load_file()
click to toggle source
# File lib/tot.rb, line 33 def load_file todo_path = Config.todo_path File.open(todo_path,'w'){|file| YAML.dump([],file)} unless File.exists? todo_path @tasks = YAML.load_file(todo_path).sort_by{|i| i['date']} end
print_color(with_index = false)
click to toggle source
# File lib/tot.rb, line 74 def print_color(with_index = false) #{{{ @tasks.each_with_index do |todo,idx| #https://github.com/flori/term-ansicolor/blob/master/examples/example.rb case (Date.parse(todo['date'].to_s) - Date.parse(Time.now.to_s)).to_i when -10 .. -1 print Term::ANSIColor.blue when 0..1 print Term::ANSIColor.bold, Term::ANSIColor.red when 2..3 print Term::ANSIColor.bold, Term::ANSIColor.yellow when 4..7 print Term::ANSIColor.bold, Term::ANSIColor.cyan when 7..30 print Term::ANSIColor.white else print Term::ANSIColor.magenta end puts [("<<#{idx}>>" if with_index), todo['date'].strftime("%Y/%m/%d %H:%M"), todo['title'], '['+todo['tag'].flatten.join(',')+']'].keep_if{|i| not i.nil?}.join(' | ') print Term::ANSIColor.reset end self end
refresh()
click to toggle source
# File lib/tot.rb, line 39 def refresh load_file self end
save()
click to toggle source
# File lib/tot.rb, line 44 def save #File.open(Config.todo_path,'w'){|file| file.puts todos.ya2yaml} #YAML.dump(todos, file)} # ya2yamlだとhashの順番が変わる File.open(Config.todo_path,'w'){|file| YAML.dump(@tasks, file)} end
stdin_parser(lines)
click to toggle source
This method is incomplete, returns array of title for now.
# File lib/tot.rb, line 102 def stdin_parser(lines) #{{{ lines = lines.split(/\n/) unless lines.class == Array lines.map {|line| line.chomp.gsub(/\e\[\d+m/,"").split('|').map(&:strip) }.keep_if{|i| i != []} .map {|l| task = {} task[:date] = Time.parse(l[0]) task[:title] = l[1] task[:tag] = YAML.load(l[2]) task } rescue raise RuntimeError, 'Stdin lines are invalid.' end