-
Notifications
You must be signed in to change notification settings - Fork 0
Command line todo list #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
30f336c
e7417ab
457beb7
8ba3c09
601d722
21cb516
7b8fc0b
47db7dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| source "https://rubygems.org" | ||
|
|
||
| gem 'thor', '~> 0.20.0' |
| 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 |
| 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 | ||
| ``` |
| 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 |
| 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 | ||
|
|
||
| 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" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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 |
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe provide
add_taskor something in the store object so that the UI layer is kept as think as possible