diff --git a/03-command-line-todo-list/Gemfile b/03-command-line-todo-list/Gemfile new file mode 100644 index 0000000..80b4e64 --- /dev/null +++ b/03-command-line-todo-list/Gemfile @@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gem 'thor', '~> 0.20.0' \ No newline at end of file diff --git a/03-command-line-todo-list/Gemfile.lock b/03-command-line-todo-list/Gemfile.lock new file mode 100644 index 0000000..a14899f --- /dev/null +++ b/03-command-line-todo-list/Gemfile.lock @@ -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 diff --git a/03-command-line-todo-list/README.md b/03-command-line-todo-list/README.md new file mode 100644 index 0000000..285bccb --- /dev/null +++ b/03-command-line-todo-list/README.md @@ -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 +``` \ No newline at end of file diff --git a/03-command-line-todo-list/database.yaml b/03-command-line-todo-list/database.yaml new file mode 100644 index 0000000..9399b95 --- /dev/null +++ b/03-command-line-todo-list/database.yaml @@ -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 diff --git a/03-command-line-todo-list/lib/cli.rb b/03-command-line-todo-list/lib/cli.rb new file mode 100644 index 0000000..e84a109 --- /dev/null +++ b/03-command-line-todo-list/lib/cli.rb @@ -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 + + 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" + @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 \ No newline at end of file diff --git a/03-command-line-todo-list/lib/database.rb b/03-command-line-todo-list/lib/database.rb new file mode 100644 index 0000000..6993aaf --- /dev/null +++ b/03-command-line-todo-list/lib/database.rb @@ -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 \ No newline at end of file diff --git a/03-command-line-todo-list/todo.rb b/03-command-line-todo-list/todo.rb new file mode 100755 index 0000000..c291400 --- /dev/null +++ b/03-command-line-todo-list/todo.rb @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +require_relative "lib/cli" + +Todo::CLI.start(ARGV) \ No newline at end of file