Class: Qer::ToDo

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

Constant Summary collapse

CONFIG_PATH =
File.expand_path("~/.qer")

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = CONFIG_PATH) ⇒ ToDo

Returns a new instance of ToDo.



13
14
15
16
17
18
# File 'lib/qer/todo.rb', line 13

def initialize(config_file = CONFIG_PATH)
  load_config(config_file)

  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.



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

def quiet
  @quiet
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



11
12
13
# File 'lib/qer/todo.rb', line 11

def history
  @history
end

#queueObject

Returns the value of attribute queue.



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

def queue
  @queue
end

Instance Method Details

#add(item) ⇒ Object



20
21
22
23
24
# File 'lib/qer/todo.rb', line 20

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

#bump(index, new_index = 0) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/qer/todo.rb', line 65

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



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/qer/todo.rb', line 42

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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/qer/todo.rb', line 172

def command(args)
  cmd = args.shift
  case(cmd)
  when "add", "a"
    self.add(args.join(" "))     # qer add Some task 1
  when "remove", "r"
    self.remove(args.shift) # qer remove 0
  when "push", "pu"
    self.push(args.join(" "))    # qer push Some task 2
  when "pop", "po"
    self.pop                     # qer pop
  when "bump", "b"
    self.bump(*args.first(2))    # qer bump
  when "clear"
    self.clear(args.shift)       # qer clear
  when /.*help/
    self.help                    # qer help
  when "history", "h"
    self.print_history           # qer history
  when "", nil
    self.print                # qer
  else
    self.no_match cmd
  end
end

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/qer/todo.rb', line 156

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 << print_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



93
94
95
# File 'lib/qer/todo.rb', line 93

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

#filenameObject



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

def filename
  @filename ||= File.expand_path(@config["queue_file"] || "~/.qer-queue")
end

#helpObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/qer/todo.rb', line 198

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



97
98
99
# File 'lib/qer/todo.rb', line 97

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

#history_filenameObject



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

def history_filename
  @history_filename ||= "#{filename}-history"
end

#history_limitObject



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

def history_limit
  @config["history_limit"] || 30
end

#history_titleObject



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

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

#hlObject



130
131
132
# File 'lib/qer/todo.rb', line 130

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

#load_config(path) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/qer/todo.rb', line 105

def load_config(path)
  @config = {}
  return unless File.exist?(path)
  @config = YAML.load_file(path)
rescue StandardError => e
  puts "Error during config file loading."
  puts "Error: #{e.name} - #{e.message}"
end

#no_match(cmd) ⇒ Object



231
232
233
# File 'lib/qer/todo.rb', line 231

def no_match cmd
  puts "Did not recognize command: #{cmd}"
end

#popObject



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

def pop
  remove(0)
end


73
74
75
# File 'lib/qer/todo.rb', line 73

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


77
78
79
# File 'lib/qer/todo.rb', line 77

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


150
151
152
153
154
# File 'lib/qer/todo.rb', line 150

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


138
139
140
141
# File 'lib/qer/todo.rb', line 138

def print_line(index, item)
  return unless item
  item.size == 2 ? print_queue_line(index,item) : print_history_line(item)
end


143
144
145
146
147
148
# File 'lib/qer/todo.rb', line 143

def print_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



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

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

#remove(index) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/qer/todo.rb', line 26

def remove(index)
  if index.nil?
    print "Remove must have an index: ex. 'qer remove 3'"
    return
  end
  if item = self.queue.delete_at(index.to_i)
    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

#tf(t) ⇒ Object



134
135
136
# File 'lib/qer/todo.rb', line 134

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

#titleObject



122
123
124
# File 'lib/qer/todo.rb', line 122

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

#widthObject



101
102
103
# File 'lib/qer/todo.rb', line 101

def width
  @config["page_width"] || 80
end

#writeObject



81
82
83
# File 'lib/qer/todo.rb', line 81

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

#write_historyObject



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

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