Skip to content

Instantly share code, notes, and snippets.

@duien
Created July 22, 2014 03:49
Show Gist options
  • Save duien/b21f99814d897d9d09c5 to your computer and use it in GitHub Desktop.
Save duien/b21f99814d897d9d09c5 to your computer and use it in GitHub Desktop.
require 'term/ansicolor'
class String
include Term::ANSIColor
end
class Task
attr_accessor :status, :text, :indent_level
def to_formatted_string
"#{indent}- #{formatted_status} #{text}"
end
def initialize(text, status=:todo)
self.text = text
self.status = status
self.indent_level = 0
end
def formatted_status
Status[status].to_formatted_string
end
def indent
' ' * indent_level
end
end
class Status
@status = {}
attr_accessor :text, :color, :active
def self.[](status)
@status[status.downcase.to_s]
end
def self.add_status(status)
@status[status.text.downcase.to_s] = status
end
def initialize(text, color, active=false)
self.text = text
self.color = color
self.active = active
self.class.add_status(self)
end
def to_formatted_string
colored = text.upcase.send(color)
active ? colored.negative : colored
end
end
# Set up statuses
Status.new("todo", :yellow, true)
Status.new("done", :green)
Status.new("later", :green, true)
Status.new("cancel", :yellow)
Status.new('waiting', :magenta)
Status.new("today", :red, true)
# Create a task list
tasks = [
Task.new('Do a thing'),
Task.new('Do another thing'),
Task.new('I\'ll get to it eventually', :later),
Task.new('Already did it', :done),
Task.new('Not gonna do it', :cancel),
Task.new('Do this now', :today)
]
tasks[2].indent_level += 1
puts tasks.map(&:to_formatted_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment