Permalink
Browse files

Refactored and simplified the contexts mechanism.

git-svn-id: http://svn.textmate.org/trunk/Bundles/GTDAlt.tmbundle@6116 dfb7d73b-c2ec-0310-8fea-fb051d288c6d
  • Loading branch information...
Charilaos Skiadas
Charilaos Skiadas committed Nov 14, 2006
1 parent 7c0ff89 commit 2b9412d175c05d68f0bfb3918bb838190c865689
View
@@ -8,7 +8,7 @@
<string>#!/usr/bin/env ruby
require ENV['TM_BUNDLE_SUPPORT'] + '/lib/GTDUtils.rb'
require ENV['TM_SUPPORT_PATH'] + '/lib/dialog.rb'
contextList = GTDLight.get_env_contexts
contextList = GTDContexts.contexts
choice = Dialog.menu(contextList)
if choice then
print "@#{contextList[choice]} "
@@ -8,10 +8,8 @@
<string>#!/usr/bin/env ruby
require ENV['TM_BUNDLE_SUPPORT'] + '/lib/GTDUtils.rb'
require ENV['TM_SUPPORT_PATH'] + '/lib/dialog.rb'
include GTDLight
line = STDIN.read
#GTD.process_directory
contextList = GTDLight.get_env_contexts
contextList = GTDContexts.contexts
choice = Dialog.menu(contextList)
if choice then
print line.sub(/@(\S+)($|\s+)/) {|text,space| "@" + e_sn(contextList[choice]) + ((space.nil? or space == "") ? " " : space)+"$0"}
@@ -21,7 +21,7 @@
<string>#!/usr/bin/env ruby
require ENV['TM_BUNDLE_SUPPORT'] + '/lib/GTDUtils.rb'
require ENV['TM_SUPPORT_PATH'] + '/lib/dialog.rb'
contextList = GTDLight.get_env_contexts
contextList = GTDContexts.contexts
choice = Dialog.menu(contextList)
if choice then
print "@#{contextList[choice]} "
View
@@ -10,7 +10,7 @@
# Module GTD. Used as a name-space for all other classes and methods.
module GTD
#--
include GTDLight
# include GTDLight
PROJECT_BEGIN_REGEXP = /^\s*(project)\s*(.*)$/
PROJECT_END_REGEXP = /^\s*(end)\s*$/
ACTION_REGEXP = /^\s*@(\S+)\s+((?:[^\[]+)(?:\s*\[\d+\])?)\s*((?:due|from|at):\[(\d{4}-\d{2}-\d{2})\])?\s*$/
@@ -45,7 +45,6 @@ def GTD::parse(data)
end
class << self
@@contexts = ["email"]
@@objects = Array.new
@@files = Array.new
def objects
@@ -58,25 +57,6 @@ def clear_objects
def add_object(object)
@@objects << object
end
# Add the array newContexts to the contexts.
def add_contexts(*newContexts)
# pp ENV['TM_GTD_CONTEXT']
@@contexts |= newContexts
@@contexts.uniq!
@@contexts.sort!
end
# Returns an array of all contexts, alphabetized.
def get_contexts
self.contexts
end
def contexts
GTD.add_contexts(*GTDLight.get_env_contexts) if @@contexts.empty?
@@contexts
end
def clear_contexts
@@contexts = []
end
# Returns an array of all gtd files in given the directory, or in ENV['TM_GTD_DIRECTORY'] if
# that is nil, or in the default directory otherwise.
def get_gtd_directory(directory = nil)
@@ -277,7 +257,7 @@ def process_instructions(instructions)
act.note = noteid
@notes[noteid] = act
end
GTD.add_contexts(context)
GTDContexts.contexts << context
@current_project << act
when :note
act = @notes[context]
View
@@ -1,13 +1,33 @@
require 'date'
require 'fileutils.rb'
# Useful methods that don't need to scan any files in order to work
module GTDLight
def GTDLight.get_env_contexts
contexts = ((ENV['TM_GTD_CONTEXT'] || "") + " " + (ENV['TM_GTD_CONTEXTS'] || "")).chomp.split(" ").compact.sort
contexts = ["email", "office", "online", "home", "call", "waiting"] if contexts.empty?
return contexts
class GTDContexts
class << self
def contexts
@@contexts ||= self.get_env_contexts
end
def contexts=(newContexts)
@@contexts = newContexts
unless @@contexts.nil?
@@contexts.uniq!
@@contexts.sort!
end
end
def get_env_contexts
contexts = ("#{ENV['TM_GTD_CONTEXT']} #{ENV['TM_GTD_CONTEXTS']}").chomp.split(" ").compact.sort
contexts = ["email", "office", "online", "home", "call", "waiting"] if contexts.empty?
return contexts.uniq.sort
end
end
end
# COMMENT TO BE REMOVED ONCE THINGS ARE CHECKED TO WORK
# module GTDLight
# def GTDLight.get_env_contexts
# contexts = ((ENV['TM_GTD_CONTEXT'] || "") + " " + (ENV['TM_GTD_CONTEXTS'] || "")).chomp.split(" ").compact.sort
# contexts = ["email", "office", "online", "home", "call", "waiting"] if contexts.empty?
# return contexts.uniq.sort
# end
# end
class Array
def next(item)
i = (self.index(item) || -1) + 1
@@ -18,65 +38,6 @@ def previous(item)
return self[i % length]
end
end
# Utilities for converting a date in various ways.
module DateUtils
require 'date'
# Attempts to convert the given string to a date. It can understand expressions like:
# * <em>today</em>
# * <em>next Tuesday</em>
# * <em>3 months</em>
def DateUtils::convert_date(string)
case string.downcase
when /^today$/
return Date.today
when /^tomorrow$/
return Date.today + 1
when /^(?:next )?(week|month|year)$/
tod = Date.today
case $1
when "week"
return tod + 7
when "month"
return tod + 30
when "year"
return tod + 365
end
when /^(?:next )?(\w+)$/
day = Date::ABBR_DAYS[$1] || Date::DAYS[$1]
raise unless day
tod = Date.today
tod=tod.succ while tod.wday != day
return tod
when /^(?:in\s*)?((?:[+-])?\d+)\s*(day|week|month|year)(s?)$/
tod = Date.today
no = $1.to_i
case $2
when "day"
return tod + no
when "week"
return tod + no * 7
when "month"
return tod >> no
when "year"
return tod + no * 365
end
else
return Date.parse(string.downcase)
end
end
# Adds +days+ to the date described by +dateString+. +days+ can be negative.
def DateUtils::addToDate(dateString,days)
return Date.parse(dateString) + days
end
# Adds a month to the date described by +dateString+.
def DateUtils::addMonth(dateString)
return Date.parse(dateString) >> 1
end
# Subtracts a month from the date described by +dateString+.
def DateUtils::subtractMonth(dateString)
return Date.parse(dateString) << 1
end
end
# The test <tt>date === DateLate</tt> returns true if +date+ is earlier than Date.today
class DateLate
def self.===(date)
@@ -1,11 +1,12 @@
require "test/unit"
$:.unshift File.join(File.dirname(__FILE__), "..", "bin")
$:.unshift "../lib"
ENV['TM_GTD_CONTEXT'] = ""
ENV['TM_GTD_CONTEXTS'] = "hello there"
require "GTDUtils.rb"
class TestUtils < Test::Unit::TestCase
def setup
@a = [6,2,5,3,4]
@today = Date.today
end
def test_array_next
assert_equal(6, @a.next(4))
@@ -16,19 +17,20 @@ def test_array_previous
assert_equal(6, @a.previous(2))
assert_equal(4, @a.previous(6))
end
def test_date_convert
assert_equal(DateUtils.convert_date("today"), @today)
assert_equal(DateUtils.convert_date("1 week"), @today + 7)
assert_equal(DateUtils.convert_date("1 weeks"), @today + 7)
assert_equal(DateUtils.convert_date("4 weeks"), @today + 28)
assert_equal(DateUtils.convert_date("-1 week"), @today - 7)
assert_equal(DateUtils.convert_date("2 month"), @today >> 2)
assert_equal(DateUtils.convert_date("In 5 days"), @today + 5)
nextWednesday = @today + ( 7 + 3 - @today.cwday ) % 7
assert_equal(DateUtils.convert_date("next wednesday"), nextWednesday)
assert_equal(DateUtils.convert_date("next wed"), nextWednesday)
assert_equal(DateUtils.addToDate("2005-06-07",5), Date.parse("2005-06-12"))
assert_equal(DateUtils.addMonth("2005-06-07"), Date.parse("2005-07-07"))
assert_equal(DateUtils.subtractMonth("2005-06-07"), Date.parse("2005-05-07"))
def test_contexts
puts GTDContexts.get_env_contexts
assert_equal(2, GTDContexts.get_env_contexts.length)
assert_equal(2, GTDContexts.contexts.length)
GTDContexts.contexts=["hi","you", "there"]
assert_equal(3, GTDContexts.contexts.length)
GTDContexts.contexts += ["you", "there", "foo"]
assert_equal(4, GTDContexts.contexts.length)
GTDContexts.contexts << "bar"
assert_equal(5, GTDContexts.contexts.length)
GTDContexts.contexts = nil
assert_equal(2, GTDContexts.contexts.length)
ENV['TM_GTD_CONTEXTS'] = nil
GTDContexts.contexts = nil
assert_equal(6, GTDContexts.contexts.length)
end
end
end

0 comments on commit 2b9412d

Please sign in to comment.