Class: Reminders

Inherits:
Object
  • Object
show all
Defined in:
lib/wooden.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k) ⇒ Reminders

Returns a new instance of Reminders.



10
11
12
# File 'lib/wooden.rb', line 10

def initialize k
  @id = k
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/wooden.rb', line 9

def id
  @id
end

Instance Method Details

#<<(e) ⇒ Object



125
126
127
# File 'lib/wooden.rb', line 125

def << e
  entry(e)
end

#[](n) ⇒ Object



128
129
130
131
132
# File 'lib/wooden.rb', line 128

def [] n
  a = `remind -t#{n} rem/#{@id}.rem`.split("\n").compact.uniq.filter { |e| e.length > 0 }
  h = a.shift
  return [h, a.reverse].flatten
end

#entry(natural_language, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/wooden.rb', line 13

def entry(natural_language, options = {})
  # Parse natural language with Nickel
  parsed = Nickel.parse(natural_language)
  
  # Extract occurrence information
  occurrence = parsed.occurrences.first
  return false if occurrence.nil?
  
  # Build the REM command
  remind_parts = ["REM"]
  date_txt, time_txt = "", ""
  
  # Date specification
  if occurrence.start_date
    date = occurrence.start_date
    remind_parts << date.to_date.strftime("%d %b %Y")
    date_txt = %[ #{date.to_date.strftime("%d-%b-%Y")}]
  end
  
  # Add time if present
  if occurrence.start_time
    time = occurrence.start_time
    remind_parts << "AT #{time.to_time.strftime('%H:%M')}"
    time_txt = %[ #{time.to_time.strftime("%H:%M")}]
  end
  
  # Recurrence handling
  if occurrence.type == :daily
    remind_parts.insert(1, "*1")
  elsif occurrence.type == :weekly
    day_name = occurrence.start_date.to_date.strftime("%A") if occurrence.start_date
    remind_parts[1] = day_name if day_name
  elsif occurrence.type == :monthly
    # Keep the day number, make it monthly
    remind_parts.insert(1, "*1")
  elsif occurrence.type == :yearly
    # Annual reminder - just use the date without year
    if occurrence.start_date
      remind_parts[1] = occurrence.start_date.to_date.strftime("%d %b")
    end
  end
  
  # Duration (convert to DURATION keyword)
  if options[:duration]
    remind_parts << "DURATION #{options[:duration]}"
  end
  
  # Lead time (advance warning)
  if options[:lead]
    remind_parts.insert(1, "+#{options[:lead]}")
  end
  
  # Delta for repeat reminders
  if options[:delta]
    remind_parts << "*#{options[:delta]}"
  end
  
  # Priority
  if options[:priority]
    remind_parts << "PRIORITY #{options[:priority]}"
  end
  
  # Tag
  if options[:tag]
    remind_parts << "TAG #{options[:tag]}"
  end
  
  # Message body (from Nickel's message or custom)
  message = options[:message] || parsed.message || "Reminder"
  remind_parts << "MSG"
  dt = "#{date_txt}#{time_txt}"
  if dt.length > 0
    remind_parts << dt.strip
  end
  remind_parts << "%a"
  remind_parts << "%b"
  
  # Add special tokens
  msg_tokens = []
  msg_tokens << "%\"%" if options[:quiet]  # Run even if -q flag
  
  # Construct final message
  final_message = msg_tokens.join(" ") + " " + message
  remind_parts << final_message.strip
  
  # UNTIL clause for end date
  if options[:until]
    until_str = "UNTIL #{options[:until].strftime('%d %b %Y')}"
    # Insert before MSG
    msg_index = remind_parts.index("MSG")
    remind_parts.insert(msg_index, until_str)
  end
  
  # OMIT keyword for skipping certain days
  if options[:omit]
    omit_days = Array(options[:omit]).join(" ")
    msg_index = remind_parts.index("MSG")
    remind_parts.insert(msg_index, "OMIT #{omit_days}")
  end
  
  # SKIP keyword (how to handle omitted days)
  if options[:skip]
    msg_index = remind_parts.index("MSG")
    remind_parts.insert(msg_index, "SKIP")
  end
  
  File.open("rem/#{@id}.rem", 'a') { |f| f.puts(remind_parts.join(" ")) }
  ff = File.read("rem/#{@id}.rem").split("\n").uniq.filter { |e| e.length > 0 }
  File.open("rem/#{@id}.rem", 'w') { |f| f.write("") }
  ff.each { |e| File.open("rem/#{@id}.rem", 'a') { |f| f.puts(e) } }
  return nil
end