Class: Greeenboii::TodoList

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

Instance Method Summary collapse

Constructor Details

#initializeTodoList

Returns a new instance of TodoList.



338
339
340
# File 'lib/greeenboii.rb', line 338

def initialize
  @db = setup_database
end

Instance Method Details

#add_taskObject



355
356
357
358
359
360
361
# File 'lib/greeenboii.rb', line 355

def add_task
  title = CLI::UI.ask("Enter task title:")
  return if title.empty?

  @db.execute("INSERT INTO todos (title) VALUES (?)", [title])
  puts CLI::UI.fmt "{{green:✓}} Task added successfully!"
end

#delete_taskObject



387
388
389
390
391
392
393
394
# File 'lib/greeenboii.rb', line 387

def delete_task
  list_tasks
  id = CLI::UI.ask("Enter task ID to delete:")
  return if id.empty?

  @db.execute("DELETE FROM todos WHERE id = ?", [id])
  puts CLI::UI.fmt "{{green:✓}} Task deleted!"
end

#list_tasksObject



363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/greeenboii.rb', line 363

def list_tasks
  tasks = @db.execute("SELECT id, title, completed, created_at FROM todos ORDER BY created_at DESC")
  if tasks.empty?
    puts CLI::UI.fmt "{{yellow:⚠}} No tasks found"
    return
  end

  ConsoleTable.define(%w[ID Title Status Created]) do |table|
    tasks.each do |id, title, completed, created_at|
      status = completed == 1 ? "{{green:✓}} Done" : "{{red:✗}} Pending"
      table << [id, title, CLI::UI.fmt(status), created_at]
    end
  end
end

#mark_doneObject



378
379
380
381
382
383
384
385
# File 'lib/greeenboii.rb', line 378

def mark_done
  list_tasks
  id = CLI::UI.ask("Enter task ID to mark as done:")
  return if id.empty?

  @db.execute("UPDATE todos SET completed = 1 WHERE id = ?", [id])
  puts CLI::UI.fmt "{{green:✓}} Task marked as done!"
end

#show_menuObject



408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/greeenboii.rb', line 408

def show_menu
  CLI::UI::Frame.divider("{{v}} Todo List")
  loop do
    CLI::UI::Prompt.ask("Todo List Options:") do |handler|
      handler.option("List Tasks")   { list_tasks }
      handler.option("Add Task")     { add_task }
      handler.option("Mark Done")    { mark_done }
      handler.option("Update Task")  { update_task }
      handler.option("Delete Task")  { delete_task }
      handler.option("Exit")         { return }
    end
  end
end

#update_taskObject



396
397
398
399
400
401
402
403
404
405
406
# File 'lib/greeenboii.rb', line 396

def update_task
  list_tasks
  id = CLI::UI.ask("Enter task ID to update:")
  return if id.empty?

  title = CLI::UI.ask("Enter new title:")
  return if title.empty?

  @db.execute("UPDATE todos SET title = ? WHERE id = ?", [title, id])
  puts CLI::UI.fmt "{{green:✓}} Task updated!"
end