Class: Qer::ToDo

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = File.expand_path("~/.qer-queue")) ⇒ ToDo

Returns a new instance of ToDo.



10
11
12
13
14
15
16
# File 'lib/qer/todo.rb', line 10

def initialize(filename = File.expand_path("~/.qer-queue"))
  @filename  = filename
  @history_filename = "#{filename}-history"

  file {|f| self.queue = Marshal.load(f) } rescue self.queue= []
  history_file {|f| self.history = Marshal.load(f) } rescue self.history= []
end

Class Attribute Details

.quietObject

Returns the value of attribute quiet.



4
5
6
# File 'lib/qer/todo.rb', line 4

def quiet
  @quiet
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



8
9
10
# File 'lib/qer/todo.rb', line 8

def history
  @history
end

#queueObject

Returns the value of attribute queue.



7
8
9
# File 'lib/qer/todo.rb', line 7

def queue
  @queue
end

Instance Method Details

#add(item) ⇒ Object



18
19
20
21
22
# File 'lib/qer/todo.rb', line 18

def add(item)
  self.queue << [Time.now.to_s, item]
  write
  print "Adding: "+item
end

#bump(index, new_index = 0) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/qer/todo.rb', line 59

def bump(index, new_index = 0)
  item = queue.delete_at(index.to_i)
  self.queue.insert(new_index.to_i, item)
  self.queue.delete_if {|i| i.nil? }
  write
  print "Bumped #{item.last} to position #{new_index}"
end

#clear(index = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/qer/todo.rb', line 36

def clear(index = nil)
  unless index.nil?
    item = self.queue.delete_at(index.to_i)
    write
    print "Removed #{item.last}"
    item
  else
    self.queue = []
    write
    print "ToDo list cleared"
  end
end

#command(args) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/qer/todo.rb', line 153

def command(args)
  case(args.shift)
  when /^a(dd)?/
    self.add(args.join(" "))     # qer add Some task 1
  when /^r(emove)?/
    self.remove(args.shift.to_i) # qer remove 0
  when /^pu(sh)?/
    self.push(args.join(" "))    # qer push Some task 2
  when /^po(p)?/
    self.pop                     # qer pop
  when /^b(ump)?/
    self.bump(*args.first(2))    # qer bump
  when /^clear/
    self.clear(args.shift)       # qer clear
  when /.*help/
    self.help                    # qer help
  when /^h(istory)?/
    self.print_history           # qer history
  else
    self.print                # qer
  end
end

#dump(queue, string, label = title) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/qer/todo.rb', line 137

def dump(queue, string, label = title)
  out = []
  out << string if(string)
  out << label
  out << hl
  unless queue.empty?
    queue.each_with_index do |item, index|
      out << process_line(index, item)
    end
  else
    out << "Nothing in this queue!"
  end
  out << hl
  puts out.join("\n") unless self.class.quiet
end

#file(mode = "r", &block) ⇒ Object



83
84
85
86
87
# File 'lib/qer/todo.rb', line 83

def file(mode = "r", &block)
  File.open(@filename, mode) do |f|
    yield f
  end
end

#helpObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/qer/todo.rb', line 176

def help
  string = <<-EOF
#{hl}
Help for Qer, the friendly easy todo list queueing system.
#{hl}
Commands:
  print - Prints out the task list
`qer`
  a(dd) - Adds a task to the end of the list
`qer add Stuff to do`
  r(emove) - Remove the given task number from the list
`qer remove 2`
  pu(sh) - Push a task onto the top of the list
`qer push Another boring thing`
  po(p) - Pops the top item off the list
`qer pop`
  b(ump) - Bumps the given index to the top of the list,
       or to the specified index
`qer bump 3`   -> bumps index 3 up to the top
`qer bump 5 2` -> bumps index five up to 2
  clear - Clears the entire list
`qer clear`
  clear - Clears the given index without writing to history file
`qer clear 3`
  h(istory) - displays list of completed tasks
`qer history`
  help - Prints this message
`qer help`
#{hl}
  EOF
  puts string unless self.class.quiet
end

#history_file(mode = "r", &block) ⇒ Object



89
90
91
92
93
# File 'lib/qer/todo.rb', line 89

def history_file(mode = "r", &block)
  File.open(@history_filename, mode) do |f|
    yield f
  end
end

#history_titleObject



107
108
109
# File 'lib/qer/todo.rb', line 107

def history_title
  "> Stuff Completed < ".center(width, '-')
end

#hlObject



111
112
113
# File 'lib/qer/todo.rb', line 111

def hl
  "".center(width, '-')
end

#popObject



49
50
51
# File 'lib/qer/todo.rb', line 49

def pop
  remove(0)
end


67
68
69
# File 'lib/qer/todo.rb', line 67

def print(string = nil)
  dump self.queue, string
end


71
72
73
# File 'lib/qer/todo.rb', line 71

def print_history(string = nil)
  dump self.history, string, history_title
end

#process_history_line(item) ⇒ Object



131
132
133
134
135
# File 'lib/qer/todo.rb', line 131

def process_history_line(item)
  end_time, time, task = item
  right     = "#{tf(time)} | #{tf(end_time)}".rjust(width-task.length)
  right.insert(0, task)
end

#process_line(index, item) ⇒ Object



119
120
121
122
# File 'lib/qer/todo.rb', line 119

def process_line(index, item)
  return unless item
  item.size == 2 ? process_queue_line(index,item) : process_history_line(item)
end

#process_queue_line(index, item) ⇒ Object



124
125
126
127
128
129
# File 'lib/qer/todo.rb', line 124

def process_queue_line(index, item)
  time, task = item
  left       = "(#{index}) #{task}"
  right      = tf(time).rjust(width - left.length)
  right.insert(0, left)
end

#push(item) ⇒ Object



53
54
55
56
57
# File 'lib/qer/todo.rb', line 53

def push(item)
  self.queue.unshift([Time.now.to_s, item])
  write
  print "Pushed to the top: #{item}"
end

#remove(index) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/qer/todo.rb', line 24

def remove(index)
  if item = self.queue.delete_at(index)
    self.history << [Time.now.to_s, item[0], item[1]]
    write_history
    write
    print "Removed: #{item.last}"
    item
  else
    print "Provided index does not exist."
  end
end

#sizeObject



95
96
97
# File 'lib/qer/todo.rb', line 95

def size
  self.queue.size
end

#tf(t) ⇒ Object



115
116
117
# File 'lib/qer/todo.rb', line 115

def tf(t)
  Time.time_ago(Time.parse(t))
end

#titleObject



103
104
105
# File 'lib/qer/todo.rb', line 103

def title
  "> Stuff on the Hopper < ".center(width, '-')
end

#widthObject



99
100
101
# File 'lib/qer/todo.rb', line 99

def width
  80
end

#writeObject



75
76
77
# File 'lib/qer/todo.rb', line 75

def write
  file("w+") {|f| Marshal.dump(self.queue, f) }
end

#write_historyObject



79
80
81
# File 'lib/qer/todo.rb', line 79

def write_history
  history_file("w+") {|f| Marshal.dump(self.history, f) }
end