Created
October 22, 2012 19:19
-
-
Save emberlzhang/3933493 to your computer and use it in GitHub Desktop.
Solution for Ruby Todos
This file contains 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 | |
require "yaml" | |
require 'term/ansicolor' | |
class String | |
include Term::ANSIColor | |
end | |
class List | |
def initialize | |
@tasks = [] | |
if File.exists?("todos.yml") | |
loaded_list | |
else | |
dumped_list | |
end | |
end | |
def loaded_list | |
@tasks = YAML.load_file("todos.yml") | |
end | |
def dumped_list | |
File.open("todos.yml", "w") { |file| YAML.dump(@tasks, file) } | |
end | |
def show_list | |
puts "\e[H\e[2J" | |
divider_line = "".ljust(100, "-") | |
puts "My Todo List\n#{divider_line}\n" | |
puts "Description".ljust(30) + "Priority".ljust(10) + "Tags".ljust(15) + "Completion".ljust(15) + "Creation Date".ljust(30) + "Completion Date" | |
@tasks.each_with_index do |task, index| | |
if task.priority_level == 1 | |
puts "#{(index + 1).to_s.ljust(5)} #{task.description.ljust(25)} #{task.priority_level.to_s.ljust(5)} #{task.tags.join(",").ljust(20)} #{task.completed.to_s.ljust(10)} #{task.creation_date.to_s.ljust(20)} #{task.completion_date.to_s.ljust(15)}".on_red | |
elsif task.priority_level == 2 | |
puts "#{(index + 1).to_s.ljust(5)} #{task.description.ljust(25)} #{task.priority_level.to_s.ljust(5)} #{task.tags.join(",").ljust(20)} #{task.completed.to_s.ljust(10)} #{task.creation_date.to_s.ljust(20)} #{task.completion_date.to_s.ljust(15)}".on_yellow | |
else | |
puts "#{(index + 1).to_s.ljust(5)} #{task.description.ljust(25)} #{task.priority_level.to_s.ljust(5)} #{task.tags.join(",").ljust(20)} #{task.completed.to_s.ljust(10)} #{task.creation_date.to_s.ljust(20)} #{task.completion_date.to_s.ljust(15)}".on_green | |
end | |
end | |
end | |
def priority_sorted_list | |
@tasks.sort! {|a, b| a.priority_level <=> b.priority_level} | |
show_list | |
dumped_list | |
end | |
def priority_only(level) | |
@tasks.keep_if {|t| t.priority_level == level} | |
puts "here are the priority level tasks only: #{@tasks}" | |
show_list | |
end | |
def completed_by_date | |
@tasks.keep_if {|t| t.completed == true } | |
@tasks.sort! {|a,b| a.completion_date <=> b.completion_date } | |
show_list | |
end | |
def outstanding_by_date | |
@tasks.keep_if {|t| t.completed == false } | |
@tasks.sort! {|a,b| a.creation_date <=> b.creation_date } | |
show_list | |
end | |
def filter_by_tag(tag) | |
@tasks.keep_if {|t| t.tags.include?(tag) } | |
show_list | |
end | |
def add_task(description, position = -1) | |
@tasks.insert(position, Task.new(description)) | |
dumped_list | |
end | |
def delete_task(position) | |
@tasks.delete_at(position) | |
dumped_list | |
end | |
def complete_task(position) | |
@tasks[position].complete! | |
dumped_list | |
end | |
def add_tags(position, new_tags) | |
old_tags = @tasks[position].tags | |
if old_tags = [] | |
updated_tags = new_tags | |
else | |
old_tags.keep_if do |tag| | |
new_tags.include?(tag) == false | |
new_tags << tag | |
updated_tags = new_tags | |
end | |
end | |
@tasks[position].update_tags(updated_tags) | |
dumped_list | |
end | |
def change_priority(position, new_level) | |
@tasks[position].change_priority(new_level) | |
dumped_list | |
end | |
end | |
##################################################################################################################### | |
##################################################################################################################### | |
class Task | |
attr_accessor :description, :completed, :completion_date, :priority_level, :tags | |
attr_reader :creation_date | |
def initialize(description) | |
@description, @completed, @creation_date, @completion_date, @tags, @priority_level = | |
description, false, Time.now, "", [], 3 | |
end | |
def complete! | |
@completed = true | |
@completion_date = Time.now | |
end | |
def update_tags(updated_tags) | |
@tags = updated_tags | |
end | |
def change_priority(new_level) | |
@priority_level = new_level.to_i | |
end | |
end | |
##################################################################################################################### | |
##################################################################################################################### | |
todo_list = List.new | |
##################################################################################################################### | |
##################################################################################################################### | |
case ARGV[0] | |
when "list" | |
if ARGV[1] == nil | |
todo_list.show_list | |
elsif ARGV[1] == "prioritized" | |
todo_list.priority_sorted_list | |
elsif ARGV[1] == "priority" | |
todo_list.priority_only(ARGV[2].to_i) | |
elsif ARGV[1] == "completed" | |
todo_list.completed_by_date | |
elsif ARGV[1] == "outstanding" | |
todo_list.outstanding_by_date | |
elsif ARGV[1] == "tag" | |
todo_list.filter_by_tag(ARGV[2]) | |
end | |
when "add" | |
####adding tasks | |
if ARGV[1] == "first" | |
position = 0 | |
ARGV.shift(2) | |
elsif ARGV[1] == "last" | |
position = -1 | |
ARGV.shift(2) | |
else | |
position = -1 | |
ARGV.shift(1) | |
end | |
content = ARGV.join(' ') | |
todo_list.add_task(content, position) | |
####adding tags | |
when "tagging" | |
item_pos = ARGV[1].to_i | |
ARGV.shift(2) | |
new_tags = ARGV | |
todo_list.add_tags(item_pos - 1, new_tags) | |
when "delete" | |
item_pos = ARGV[1].to_i | |
todo_list.delete_task(item_pos - 1) | |
when "complete" | |
item_pos = ARGV[1].to_i | |
todo_list.complete_task(item_pos - 1) | |
when /\d/ | |
item_pos = ARGV[0].to_i | |
if ARGV[1] == "prioritize" | |
todo_list.change_priority(item_pos - 1, ARGV[2]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment