Class: Tot::CLI

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

Overview

}}}

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



151
152
153
154
# File 'lib/tot.rb', line 151

def initialize(*args)
  super
  @todo_manager = TodoManager.new
end

Instance Method Details

#addObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tot.rb', line 168

def add
  new_todo = {}
  new_todo['title'] = Readline.readline('title> ', true).chomp('\n')
  new_todo['date'] = Time.parse(Utils.datetime_filter(Readline.readline('date> ', true).chomp('\n')).to_s)
  new_todo['tag'] = Readline.readline('tag (separate by space)> ', true)
                      .chomp('\n').split(' ')
  tmpfile = "/tmp/tot.markdown"
  # File.open(tmpfile,"w"){|file|
  #   file.puts "\n\n# This line will be ignored."
  # }
  system([ENV['EDITOR'],tmpfile].join(' '))
  new_todo['text'] = File.readlines(tmpfile).join
  print new_todo['text']
  File.delete tmpfile
  @todo_manager.add new_todo
end

#deleteObject



186
187
188
189
190
# File 'lib/tot.rb', line 186

def delete
  @todo_manager.print_color(true)
  @todo_manager.delete_at Readline.readline('Which Task?> ',false).chomp('\n').to_i
  @todo_manager.dump
end

#edit(title) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/tot.rb', line 215

def edit(title)
  reg = Regexp.new(title,Regexp::IGNORECASE)
  todos = @todo_manager.find_all{|item| reg.match(item['title'])}
  if todos.size == 0
    puts 'No matched task.'
  elsif todos.size > 1
    puts todos.size.to_s + ' tasks matched.'
  else
    todo = todos[0]
    old_title = todo['title']
    if options['title']
      todo['title'] = Readline.readline('New Title> ').chomp('\n')
    elsif options['date']
      todo['date'] = Time.parse(Utils.datetime_filter(Readline.readline('date> ', true).chomp('\n')).to_s)
    elsif options['tag']
      todo['tag'] = Readline.readline("tag (old_value: #{todo['tag'].join(' ')})> ", true)
                      .chomp('\n').split(' ')
    else
      tmpfile = "/tmp/tot.markdown"
      File.open(tmpfile,'w'){|file| file.write todo['text']}
      system([ENV['EDITOR'],tmpfile].join(' '))
      todo['text'] = File.readlines(tmpfile).join
      File.delete tmpfile
    end

    puts 'Title: ' + todo['title']
    puts 'Date:  ' + todo['date'].strftime("%Y/%m/%d %H:%M")
    puts 'Tags:  ' + todo['tag'].to_s
    puts
    print todo['text']
    @todo_manager.delete_at(@todo_manager.find_index{|obj| obj['title'] == old_title})
    @todo_manager.add todo
  end
  
end

#listObject



158
159
160
161
162
163
164
165
# File 'lib/tot.rb', line 158

def list
  if options['tag']
    @todo_manager.find_all! do |todo|
      options[:tag].all?{|i| todo['tag'].include? i}
    end
  end
  @todo_manager.print_color(false)
end

#show(title) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/tot.rb', line 196

def show(title)
  reg = Regexp.new(title,Regexp::IGNORECASE)
  todos = @todo_manager.find_all{|item| reg.match(item['title'])}
  if todos.size == 0
    puts 'No matched task.'
  elsif todos.size > 1
    puts 'Several tasks matched.'
  else
    todo = todos[0]
    puts 'Title: ' + todo['title']
    puts 'Date:  ' + todo['date'].strftime("%Y/%m/%d %H:%M")
    puts
    print todo['text']
  end
  
end