Class: Tot::CLI

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

Overview

}}}

Constant Summary collapse

TTY =
open("/dev/tty")

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



199
200
201
202
203
204
205
206
207
208
# File 'lib/tot.rb', line 199

def initialize(*args)
  super
  @todo_manager = TodoManager.new
  @stdin_tasks = []
  # The following lines needs to be fixed when I correct stdin_parser.
  if Utils.stdin_incoming?
    @stdin_lines = STDIN.readlines
    @stdin_tasks = @todo_manager.stdin_parser(@stdin_lines)
  end
end

Instance Method Details

#addObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/tot.rb', line 230

def add
  new_todo = {}
  new_todo['title'] = Readline.readline('title> ', true).chomp('\n').strip
  begin
    input = Readline.readline('date> ', true).chomp('\n').strip.split
    date = Utils.datetime_filter(input[0])
    h,m  = Utils.time_filter(input[1])
    new_todo['date'] = Time.mktime(date.year, date.month, date.day, h, m)
  rescue
    puts "Invalid input. Please retry."
    retry
  end
  new_todo['tag'] = Readline.readline('tag (separate by space)> ', true)
                      .chomp('\n').split(' ')
  # File.open(tmpfile,"w"){|file|
  #   file.puts "\n\n# This line will be ignored."
  # }

  #tmpfile = "/tmp/tot.markdown"
  #system([ENV['EDITOR'],tmpfile].join(' '))
  Tempfile.open(["/tmp/tot_",".markdown"]) do |t|
    IO.copy_stream(STDIN, t) unless STDIN.tty?
    STDIN.reopen(TTY)
    system([ENV['EDITOR'], t.path, ">", TTY.path].join(" "))
    new_todo['text'] = t.read
  end
  #new_todo['text'] = File.readlines(tmpfile).join
  print new_todo['text']
  #File.delete tmpfile
  @todo_manager.add new_todo
  @todo_manager.save
end

#deleteObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/tot.rb', line 264

def delete
  if @stdin_tasks.empty?
    @todo_manager.print_color(true)
    begin
      @todo_manager.delete_at Integer(Readline.readline('Which Task?> ',false).chomp('\n'))
    rescue
      puts 'Invalid input. Please retry.'
      retry
    end
    @todo_manager.save
  elsif #@stdin_tasks.size >= 1
    @stdin_tasks.each do |stdin_task|
      @todo_manager.delete_by_title(stdin_task[:title])
    end
    @todo_manager.save
  end
end

#editObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/tot.rb', line 325

def edit
  #### stdinあり
  if Utils.stdin_incoming?
    todos = []
    @stdin_tasks.each do |stdin_task|
      todos.push @todo_manager.find_all!{|item| stdin_task[:title] == item['title']}
    end
    todos.flatten.each { |todo| edit_todo(todo,options)}
    return
  end

  #### stdinなし
  reg = nil
  if options['filter']
    reg = Regexp.new(options['filter'].join('.*'),Regexp::IGNORECASE)
  else
    reg = /.*/
  end
  todo = nil
  todos = @todo_manager.find_all!{|item| reg.match(item['title'])}
  if todos.size == 0
    puts 'No matched task.'
    return
  elsif todos.size > 1
    @todo_manager.print_color(true)
    todo = todos[Integer(Readline.readline('Which Task?> ',false).chomp('\n'))]
  else
    todo = todos.first
  end
  
  edit_todo(todo,options)
end

#listObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/tot.rb', line 213

def list
  if options['tag']
    @todo_manager.find_all! do |todo|
      options[:tag].all?{|i| todo['tag'].include? i}
    end
  elsif options['filter']
    @todo_manager.find_all! do |todo|
       options['filter'].all?{|i|
         re = Regexp.new(i,Regexp::IGNORECASE)
         re.match(todo['title'])
       }
    end
  end
  @todo_manager.print_color(false)
end

#showObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/tot.rb', line 287

def show

  #### stdinあり
  if Utils.stdin_incoming?
    todos = []
    @stdin_tasks.each do |stdin_task|
      todos.push @todo_manager.find_all!{|item| stdin_task[:title].match(item['title'])}
    end
    todos.flatten.each { |todo| puts '-'*30;print_todo(todo)}
    return
  end

  #### stdinなし
  reg = nil
  if options['filter']
    reg = Regexp.new(options['filter'].join('.*'),Regexp::IGNORECASE)
  else
    reg = /.*/
  end

  todo = nil
  todos = @todo_manager.find_all!{|item| reg.match(item['title'])}
  if todos.size == 0
    puts 'No matched task.'
    return
  elsif todos.size > 1
    @todo_manager.print_color(true)
    todo = todos[Readline.readline('Which Task?> ',false).chomp('\n').to_i]
  else
    todo = todos.first
  end

  print_todo(todo)
end