Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
Merge branch 'feature/due' into develop
Browse files Browse the repository at this point in the history
* feature/due:
  Brighten the headers.
  Introduce due-feature.
  introduce before_date filter
  introduce on_date filter on todolist
  Introduce due attribute on Todo
  Feature to show todays date in output
  Add feature for "due"
  • Loading branch information
Bèr Kessels committed Apr 1, 2013
2 parents ee85ced + 5447ca5 commit 0699b11
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 0 deletions.
49 changes: 49 additions & 0 deletions features/due.feature
@@ -0,0 +1,49 @@
Feature: Due

So that I can see what needs to be done
As a user
I want to get a list of due items

Background:
Given a default config exists

Scenario: Show todays date
Given a todofile exists
When I run `todotxt due` interactively
Then it should pass with todays date

Scenario: List due items
Given the date is "2012-12-12"
And a todofile with the following items exists:
| todo |
| 2012-12-17 Install todotxt @cli +todotxt |
| Read documentation +todotxt |
| 2012-12-12 Buy GTD book @amazon +wishlist |
| 2012-12-19 Evaluate installation +todotxt |
When I run `todotxt due` interactively
Then it should pass with exactly:
"""
Due today (2012-12-12)
3. 2012-12-12 Buy GTD book @amazon +wishlist
Past-due items
Due 7 days in advance
1. 2012-12-17 Install todotxt @cli +todotxt
4. 2012-12-19 Evaluate installation +todotxt
"""

Scenario: list overdue items
Given the date is "2012-12-12"
And a todofile with the following items exists:
| todo |
| 2012-12-17 Install todotxt @cli +todotxt |
| Read documentation +todotxt |
| 2012-11-11 Buy GTD book @amazon +wishlist |
When I run `todotxt due` interactively
Then it should pass with:
"""
Past-due items
3. 2012-11-11 Buy GTD book @amazon +wishlist
"""
4 changes: 4 additions & 0 deletions features/step_definitions/environment_steps.rb
Expand Up @@ -41,3 +41,7 @@
Given /^the enviromnent variable "(.*?)" is set to "(.*?)"$/ do |name, value|
ENV[name] = value
end

Given /^the date is "(.*?)"$/ do |date|
step %{the enviromnent variable "date" is set to "#{date}"}
end
5 changes: 5 additions & 0 deletions features/step_definitions/list_steps.rb
Expand Up @@ -27,3 +27,8 @@
Then /^it should output "([^"]*)" in "([^"]*)"$/ do |string, color|
assert_partial_output(string.color(color.to_sym), all_output)
end

Then /^it should pass with todays date$/ do
today = DateTime.now.strftime("%Y-%m-%d")
step "it should pass with regex:", ".*#{today}.*"
end
19 changes: 19 additions & 0 deletions lib/todotxt/cli.rb
@@ -1,5 +1,6 @@
require "thor"
require "rainbow"
require "chronic"
require "parseconfig"

module Todotxt
Expand Down Expand Up @@ -71,6 +72,24 @@ def lscon
end
map "lsc" => :lscon

desc "due", "List due items"
def due
if ENV["date"] # Allow testing to "freeze" the date
today = DateTime.parse(ENV["date"]).to_date
else
today = DateTime.now.to_date
end

puts "Due today (#{today.strftime("%Y-%m-%d")})".bright
@list.on_date(today).each { |todo| puts format_todo(todo) }
puts "\nPast-due items".bright
@list.before_date(today).each { |todo| puts format_todo(todo) }
puts "\nDue 7 days in advance".bright
((today+1)..(today+7)).each do |day|
@list.on_date(day).each { |todo| puts format_todo(todo) }
end
end

#
# Todo management
#
Expand Down
1 change: 1 addition & 0 deletions lib/todotxt/regex.rb
Expand Up @@ -2,5 +2,6 @@ module Todotxt
PRIORITY_REGEX = /^\(([A-Z])\) /
PROJECT_REGEX = /(\+\w+)/
CONTEXT_REGEX = /(@\w+)/
DATE_REGEX = /^(\([A-Z]\) )?(x )?((\d{4}-)(\d{1,2}-)(\d{1,2}))\s?/
DONE_REGEX = /^(\([A-Z]\) )?x /
end
5 changes: 5 additions & 0 deletions lib/todotxt/todo.rb
Expand Up @@ -24,6 +24,11 @@ def create_from_text text
@done = !text.scan(DONE_REGEX).empty?
end

def due
date = Chronic.parse(text.scan(DATE_REGEX).flatten[2])
date.nil? ? nil : date.to_date
end

def do
unless done
@text = "x #{text}".strip
Expand Down
8 changes: 8 additions & 0 deletions lib/todotxt/todolist.rb
Expand Up @@ -77,6 +77,14 @@ def filter search="", opts={}
self
end

def on_date date
@todos.select { |t| t.due == date }
end

def before_date date
@todos.reject { |t| t.due.nil? || t.due >= date }
end

def to_txt
@todos.sort { |a,b| a.line <=> b.line }.map { |t| t.to_s.strip }.join("\n")
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -1,2 +1,3 @@
require 'chronic'
require 'simplecov'
SimpleCov.start
11 changes: 11 additions & 0 deletions spec/todo_spec.rb
Expand Up @@ -28,6 +28,17 @@
todo.done.should eql true
end

it "parses a due date" do
todo = Todotxt::Todo.new "(A) x 2012-12-12 an item +project1 +project2 @context1 @context2"
todo.due.should eql Chronic.parse("12 December 2012").to_date

todo = Todotxt::Todo.new "2012-1-2 an item +project1 +project2 @context1 @context2"
todo.due.should eql Chronic.parse("2 January 2012").to_date

todo = Todotxt::Todo.new "42 folders"
todo.due.should be_nil
end

it "stores line number when creating an item" do
todo = Todotxt::Todo.new "an item", "2"

Expand Down
15 changes: 15 additions & 0 deletions spec/todolist_spec.rb
@@ -1,5 +1,6 @@
require "spec_helper"
require "todotxt/todolist"
require "todotxt/todofile"

describe Todotxt::TodoList do

Expand Down Expand Up @@ -52,6 +53,20 @@
@list.todos.count.should eql 1
end

it "fetches items for a certain date" do
@list.add "2012-12-12 item"
date = DateTime.parse("2012-12-12")
@list.on_date(date).count.should eql 1
@list.on_date(date).should eql [@list.todos.last]
end

it "fetchs items before a cereain date" do
@list.add "2012-11-11 item"
@list.add "2012-12-12 item"
date = DateTime.parse("2012-12-12")
@list.before_date(date).count.should eql 1
end

it "includes done items in search when told to do so" do
@list.filter "first", :with_done => true

Expand Down
1 change: 1 addition & 0 deletions todotxt.gemspec
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.add_dependency "thor", "~> 0.15.4"
s.add_dependency "rainbow", "~> 1.1.4"
s.add_dependency "parseconfig", "~> 1.0.2"
s.add_dependency "chronic", "~> 0.9.1"

s.add_development_dependency "rake"
s.add_development_dependency "rspec", "~> 2.11.0"
Expand Down

0 comments on commit 0699b11

Please sign in to comment.