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.



181
182
183
184
185
186
187
188
189
190
# File 'lib/tot.rb', line 181

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



212
213
214
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
# File 'lib/tot.rb', line 212

def add
  new_todo = {}
  new_todo['title'] = Readline.readline('title> ', true).chomp('\n').strip
  begin
      new_todo['date'] = Time.parse(Utils.datetime_filter(Readline.readline('date> ', true).chomp('\n')).to_s)
  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



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/tot.rb', line 243

def delete
  if @stdin_tasks.empty?
    @todo_manager.print_color(true)
    @todo_manager.delete_at Readline.readline('Which Task?> ',false).chomp('\n').to_i
    @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



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/tot.rb', line 299

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[Readline.readline('Which Task?> ',false).chomp('\n').to_i]
  else
    todo = todos.first
  end
  
  edit_todo(todo,options)
end

#listObject



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

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



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/tot.rb', line 261

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