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



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

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



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

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/qer/todo.rb', line 168

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/qer/todo.rb', line 152

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



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

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

#filenameObject



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

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

#helpObject



191
192
193
194
195
196
197
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
# File 'lib/qer/todo.rb', line 191

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



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

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

#history_filenameObject



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

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

#history_limitObject



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

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

#history_titleObject



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

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

#hlObject



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

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

#load_config(path) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/qer/todo.rb', line 101

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

#popObject



51
52
53
# File 'lib/qer/todo.rb', line 51

def pop
  remove(0)
end


69
70
71
# File 'lib/qer/todo.rb', line 69

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


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

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


146
147
148
149
150
# File 'lib/qer/todo.rb', line 146

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


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

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


139
140
141
142
143
144
# File 'lib/qer/todo.rb', line 139

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



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

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
# File 'lib/qer/todo.rb', line 26

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

#tf(t) ⇒ Object



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

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

#titleObject



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

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

#widthObject



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

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

#writeObject



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

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

#write_historyObject



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

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