class Tot::CLI

Constants

TTY

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/tot.rb, line 198
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

Public Instance Methods

add() click to toggle source
# File lib/tot.rb, line 229
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(["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
delete() click to toggle source
# File lib/tot.rb, line 263
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
edit() click to toggle source
# File lib/tot.rb, line 324
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
edit_todo(todo,options={}) click to toggle source
# File lib/tot.rb, line 358
def edit_todo(todo,options={})
  old_title = todo['title']
  if options['title']
    todo['title'] = Readline.readline('New Title> ').chomp('\n')
  elsif options['date']
    begin
      input = Readline.readline('date> ', true).chomp('\n').strip.split
      date = Utils.datetime_filter(input[0])
      h,m  = Utils.time_filter(input[1])
      todo['date'] = Time.mktime(date.year, date.month, date.day, h, m)
    rescue
      puts "Invalid input. Please retry."
      retry
    end
  elsif options['tag']
    todo['tag'] = Readline.readline("tag (old_value: #{todo['tag'].join(' ')})> ", true)
    .chomp('\n').split(' ')
  else
    #tmpfile = "/tmp/tot_" + Shellwords.shellescape(todo['title']) + ".markdown"
    tmpfile = "/tmp/tot.markdown"

    fileio = File.open(tmpfile,'w')
    fileio.write todo['text']
    fileio.flush
    fileio.close
    STDIN.reopen(TTY)
    system([ENV['EDITOR'], tmpfile, ">", TTY.path].join(" "))
    todo['text'] = File.readlines(tmpfile).join
    print_todo(todo)

    File.delete tmpfile
  end

  @todo_manager.refresh
  @todo_manager.delete_by_title(old_title)
  @todo_manager.add todo
  @todo_manager.save
end
list() click to toggle source
# File lib/tot.rb, line 212
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
print_todo(todo) click to toggle source
show() click to toggle source
# File lib/tot.rb, line 286
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