Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 03-command-line-todo-list/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gem 'thor', '~> 0.20.0'
13 changes: 13 additions & 0 deletions 03-command-line-todo-list/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
thor (0.20.0)

PLATFORMS
ruby

DEPENDENCIES
thor (~> 0.20.0)

BUNDLED WITH
1.16.1
15 changes: 15 additions & 0 deletions 03-command-line-todo-list/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Command line todo list

### Description

Write an command line application that helps you manage your tasks.

The information can be stored in any prefered way. The program should be launched from the console with different attributes:

```
-a or --add adds an item into the list
-l or --list displays the list
-r or --remove removes the last n in the list
-c or --clear clears the list
-h or --help shows list of available commands
```
9 changes: 9 additions & 0 deletions 03-command-line-todo-list/database.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
:tasks:
- Lick a car tire
- Eat a whole piece of paper
- Pick your friend’s nose
- Get into a debate with a wall
- Burp the alphabet
- Eat a spoonful of mustard
- Talk without closing your mouth
73 changes: 73 additions & 0 deletions 03-command-line-todo-list/lib/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require "thor"
require_relative "database"

module Todo
class CLI < Thor
def initialize(*args)
@store = Todo::Database.new

super
end

desc "-a or --add [TASK]", "Adds an item into the list of tasks"
map %w[-a --add] => :add
def add(task)
@store.tasks = @store.tasks << task
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe provide add_task or something in the store object so that the UI layer is kept as think as possible


pretty_print_tasks(@store.tasks)
end

desc "-d or --delete [NUMBER]", "Deletes task at index NUMBER from the list"
map %w[-d --delete] => :delete
def delete(item)
tasks = @store.tasks

if tasks.length < item.to_i
shell.say "Task at number #{item} does not exist\n\n"
return
end

shell.say "\n\"\e[1m#{tasks.delete_at(item.to_i - 1)}\"\e[0m [removed]\n\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should separate the business logic from showing messages to user to make the presentation logic less invasive

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also could be cool to provide some kind of helper that removes the noise from using color codes

@store.tasks = tasks
end

desc "-r or --remove [NUMBER]", "Removes the last NUMBER of items the list"
map %w[-r --remove] => :remove
def remove(items)
tasks = @store.tasks
clear and return if tasks.length <= items.to_i
@store.tasks = tasks - tasks.last(items.to_i)

tasks.last(items.to_i).each do |task|
shell.say "\"#{task}\" \e[1m[removed]\e[0m"
end
end

desc "-l or --list", "List the tasks"
map %w[-l --list] => :list
def list
pretty_print_tasks(@store.tasks)
end

desc "-c or --clear", "Clears all tasks"
map %w[-c --clear] => :clear
def clear
@store.tasks = []
pretty_print_tasks(@store.tasks)
end

desc "-h or --help", "Displays a list of possible commands"
map %w[-h --help] => :help
def help
super
end

protected

def pretty_print_tasks(tasks)
shell.say "\n\tThere are currently \e[1m#{tasks.length} tasks\e[0m\n\n"
tasks.each_with_index { |item, index| shell.say "\t%02d #{item}" % (index + 1) }
shell.say
end
end
end
39 changes: 39 additions & 0 deletions 03-command-line-todo-list/lib/database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "yaml/store"

module Todo
class Database
attr_accessor :store, :tasks

def initialize(db_name = 'database')
@store = YAML::Store.new("#{db_name}.yaml")
initialize_store
end

def initialize_store
@store.transaction do
@store[:tasks] ||= []
@store.commit
end
end

def tasks=(tasks)
@store.transaction do
@store[:tasks] = tasks
@tasks = tasks
@store.commit
end

@tasks
end

def tasks
tasks = []

@store.transaction do
tasks = @store[:tasks]
end

tasks
end
end
end
5 changes: 5 additions & 0 deletions 03-command-line-todo-list/todo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require_relative "lib/cli"

Todo::CLI.start(ARGV)